当前位置:网站首页>[ByteDance] ByteDance access (including login and payment)

[ByteDance] ByteDance access (including login and payment)

2022-07-24 08:12:00 sirria1

Environmental Science :

client :cocos creator2.4.3

The server :openresty

Official website :https://microapp.bytedance.com/docs/zh-CN/mini-game/develop/open-capacity/log-in/tt-login

1. Sign in . Anonymous login cannot be used at present, and authorization must be enforced .

client : Use the official sample code .

tt.login({
  force: true,
  success(res) {
    console.log(`login  Successful call ${res.code} ${res.anonymousCode}`);
  },
  fail(res) {
    console.log(`login  Call failed `);
  },
});

obtain code Log in .

The server : Login in exchange for openid

function utils.request_verify_session_byte(code, anonymous_code)
	local httpc = http.new()
	local params = "appid="..byteapppid.."&secret="..bytesecret.."&code="..code
	return httpc:request_uri("https://developer.toutiao.com/api/apps/jscode2session?"..params, {
		method = "GET",
	})	
end

-- byteappid  Bytes of app id
-- bytesecret  Bytes of secret id
-- code  Obtained by client login 
--  Successfully returns  open id

2. payment

preparation : Set the payment parameters in the byte management background

Before the client calls up the payment interface , You need to create an order from your server , Order obtained ID:order_id.

Client process :

1. Request the development backend to create an order order_id.

2. Transfer payment :

let payData = {
     mode: 'game', //  Payment type 
     env: 0, // Payment environment 
     currencyType: "CNY", //  currency : At present, it's only for  "CNY"
     platform: "android", //  The platform when applying for access : At present, it's only for "android"
     buyQuantity: buyData.productPrice, //  Purchase quantity , Must satisfy : Number of gold coins * The unit price of gold coins  =  Limit the price level ( For details, please refer to the limited level of gold coins )
     zoneId: "1",
     customId: orderID, // Developer defined unique order number . If not filled , The payment result callback will not contain this field , Game developers will not be able to distribute game props ,  The basic library version is lower than 1.55.0 There is no such field 
     extraInfo: JSON.stringify({
           userId: userID,   // User defined additional information , The payment result callback information contains this field ,  The basic library version is lower than 1.55.0 There is no such field 
           version: "v0.0.0",
           price: productPrice,
     }),//extraInfo To convert to a string 
     success(res) {
           console.log(" ByteDance payment call succeeded :", res);
     },
     fail(res) {
           console.log(" ByteDance payment call failed :", res);                                       
     },
}
tt.requestGamePayment(payData)

 3. After successful payment , The byte server will call back the callback address configured in the previous page . By the way , After the callback address is configured , Need to verify on the page , verification req The parameters of are placed in query Inside , The processing code looks like this (openresty)

pay_router:get("/cb", function(req, res, next)
	local signature = ngx.escape_uri(req.query["signature"])
	local msg = ngx.escape_uri(req.query["msg"])
	local echostr = ngx.escape_uri(req.query["echostr"])
	local nonce = ngx.escape_uri(req.query["nonce"])
	local timestamp = ngx.escape_uri(req.query["timestamp"])	

	local token = cfg.bytetoken
    local params = {token, timestamp, nonce, msg}
    table.sort(params)
    local str = table.concat(params)
    local sign = string.lower(utils.bin2hex(ngx.sha1_bin(str)))
    if sign == signature then
        ngx.log(ngx.ERR, "byte  get cb success")
    else
        ngx.log(ngx.ERR, "byte  get cb failed.data=",  utils.json_encode(req),".our sign:", sign, ",byte sign:", signature)
    end

	ngx.print(echostr)
end)

When paying formally , The callback is initiated by POST Method , The interface name remains unchanged , The processing code looks like this :

pay_router:post("/cb", function(req, res, next)
    local body = req.body
	local token = cfg.bytetoken
	ngx.log(ngx.ERR, "bytepay cb: req:", utils.json_encode(req))
    local params = {token, body.timestamp, body.nonce, body.msg}
    table.sort(params)
    local str = table.concat(params)
    local sign =string.lower(utils.bin2hex(ngx.sha1_bin(str)))
	if body.signature == sign then
		ngx.log(ngx.ERR, "bytepay check signature ok")
		local status = 'success'
		local msg = utils.json_decode(body.msg)
        --  Recharge OK, Issue recharge items .
		local ret = game_pay(utils.json_decode(msg.cp_extra).userId, msg.cp_orderno)
		if ret.errCode ~= 0 then
			res:json({''})
		else
			res:json({body.echostr})
		end
	else
		ngx.log(ngx.ERR, "bytepay pay cb sign check err. own sign= ", sign, ".byte sign=", body.signature)
		res:json({''})
	end
end)

Other interfaces can be supplemented by referring to official documents , For example, get player recharge currency , Poll for supplement, etc .

原网站

版权声明
本文为[sirria1]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/204/202207221751368147.html