steam游戲內(nèi)置商店購買

文檔地址:https://partner.steamgames.com/doc/features/microtransactions/implementation

1.服務(wù)器需要訂單信息:
①用戶信息

'steamid' => '',
'language' => '客戶端游戲語言代碼 – ISO 639-1 語言代碼,客戶端游戲正在使用的語言',
'currency' => 'SO 4217 貨幣代碼,對客戶端扣款所使用的幣種'

②訂單信息

'itemcount' => '購買總數(shù)(int)',
'orderid' => '由您為此次購買指定的唯一的 64 位數(shù)字。 此數(shù)字也是此次交易的關(guān)鍵, 在 Steam 系統(tǒng)中引用該交易時(shí)使用(int)',
'itemid[0]' => '商品id(int)',
'qty[0]' =>'商品數(shù)量(int)',
'amount[0]' => '商品總價(jià)值(單位:分)(int)',
'description[0]' => '商品描述',

③游戲信息

'key' => '網(wǎng)頁 API 密鑰',
'appid' => '您的 Steam 游戲的唯一標(biāo)識符'

注意事項(xiàng):
currency字段由服務(wù)器獲取,orderid由服務(wù)器生成外,其他信息均由游戲服務(wù)器提供。
language雖由客戶端提供,但客戶端接口返回的language并不是iso 639-1標(biāo)準(zhǔn)的,轉(zhuǎn)換如下:(鍵為客戶端使用語言,值為web服務(wù)器使用語言)

private function languages($language)
    {
        $languages = [
            'arabic' => 'ar',
            'bulgarian' => 'bg',
            'schinese' => 'zh-CN',
            'tchinese' => 'zh-TW',
            'czech' => 'cs',
            'danish' => 'da',
            'dutch' => 'nl',
            'english' => 'en',
            'finnish' => 'fi',
            'french' => 'fr',
            'german' => 'de',
            'greek' => 'el',
            'hungarian' => 'hu',
            'italian' => 'it',
            'japanese' => 'ja',
            'koreana' => 'ko',
            'norwegian' => 'no',
            'polish' => 'pl',
            'portuguese' => 'pt',
            'brazilian' => 'pt-BR',
            'romanian' => 'ro',
            'russian' => 'ru',
            'spanish' => 'es',
            'swedish' => 'sv',
            'thai' => 'th',
            'turkish' => 'tr',
            'ukrainian' => 'uk',
        ];

        return $languages[$language];
    }
正式地址:
$baseUri = 'https://partner.steam-api.com/ISteamMicroTxn/';
測試地址:
$basrUriSandBox = 'https://partner.steam-api.com/ISteamMicroTxnSandbox/';

curl https

function curl_get_https($url){
    $curl = curl_init(); // 啟動一個(gè)CURL會話
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_HEADER, 0);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // 跳過證書檢查
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, true);  // 從證書中檢查SSL加密算法是否存在
    $tmpInfo = curl_exec($curl);     //返回api的json對象
    //關(guān)閉URL請求
    curl_close($curl);
    return $tmpInfo;    //返回json對象
}

function curl_post_https($url,$data){ // 模擬提交數(shù)據(jù)函數(shù)
    $curl = curl_init(); // 啟動一個(gè)CURL會話
    curl_setopt($curl, CURLOPT_URL, $url); // 要訪問的地址
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); // 對認(rèn)證證書來源的檢查
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 1); // 從證書中檢查SSL加密算法是否存在
    curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); // 模擬用戶使用的瀏覽器
    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自動跳轉(zhuǎn)
    curl_setopt($curl, CURLOPT_AUTOREFERER, 1); // 自動設(shè)置Referer
    curl_setopt($curl, CURLOPT_POST, 1); // 發(fā)送一個(gè)常規(guī)的Post請求
    curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Post提交的數(shù)據(jù)包
    curl_setopt($curl, CURLOPT_TIMEOUT, 30); // 設(shè)置超時(shí)限制防止死循環(huán)
    curl_setopt($curl, CURLOPT_HEADER, 0); // 顯示返回的Header區(qū)域內(nèi)容
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); // 獲取的信息以文件流的形式返回
    $tmpInfo = curl_exec($curl); // 執(zhí)行操作
    if (curl_errno($curl)) {
        echo 'Errno'.curl_error($curl);//捕抓異常
    }
    curl_close($curl); // 關(guān)閉CURL會話
    return $tmpInfo; // 返回?cái)?shù)據(jù),json格式
}

①獲取用戶currency: https://partner.steamgames.com/doc/webapi/ISteamMicroTxn#GetUserInfo

private function getSteamUserInfo($steamId)
    {
        //GET https://partner.steam-api.com/ISteamMicroTxn/GetUserInfo/v2/
        $steamInfoUrl = $this->baseUri . 'GetUserInfo/v2/?key=%s&steamid=%s';
        $url = sprintf($steamInfoUrl, $this->apiKey, $steamId);

        $response = curl_post_https($url);
        $json = json_decode($response, true);

        if ($json['response']['result'] == 'Failure') {
            \Log::info('steam_user_info', $json);
            throw new \Exception('Steam Response Error');
        }

        return $json['response']['params'];
    }

②向steam發(fā)起購買: https://partner.steamgames.com/doc/webapi/ISteamMicroTxn#InitTxn

public function initOrder(Request $request)
    {
        $attributes = $request->only([
            'steamid', 'appid', 'language', 'itemid'
        ]);
       
        $userInfo = $this->getSteamUserInfo($attributes['steamid']);
        $attributes['currency'] = $userInfo['currency'];
        
         //生成唯一訂單號
        $order = SteamOrders::query()->orderByDesc('created_at')->first(['orderid']);
        $attributes['orderid'] = $order ? $order->orderid + 1 : 10000000;

        $goods = $this->goods($attributes['itemid']);
         //訂單信息
        $data = [
            'key' => $this->apiKey,
            'steamid' => $attributes['steamid'],
            'appid' => $attributes['appid'],
            'language' => $this->languages($attributes['language']),
            'itemcount' => 1,
            'orderid' => (int)$attributes['orderid'],
            'currency' => $attributes['currency'],
            'itemid[0]' => $goods['itemid'],
            'qty[0]' => 1,//(int)$attributes['qty'],
            'amount[0]' => $goods['amount'],
            'description[0]' => $goods['description'],
        ];

        //POST https://partner.steam-api.com/ISteamMicroTxn/InitTxn/v3/
        $initTxnUrl = $this->baseUri . 'InitTxn/v3/';
        $response = $this->curlPostHttps($initTxnUrl, $data);       
        $result = json_decode($response, true);
        $result = $result['response'];

        if ($result['result'] == 'Failure') {
            \Log::info('steam_init_order', $result);

            return ['code' => 500, 'message' => 'Steam Response Error'];
        }

        $attributes['amount'] = $goods['amount'];
        $attributes['description'] = $goods['description'];
        SteamOrders::create($attributes);

        return [
            'code' => 0,
            'data' => [
                'orderid' => $attributes['orderid'],
                'appid' => $attributes['appid']
            ]
        ];
    }

③完成訂單:https://partner.steamgames.com/doc/webapi/ISteamMicroTxn#FinalizeTxn

public function finishOrder(Request $request)
    {
        $userId = '';

        $attributes = $request->only(['appid', 'orderid']);
        $attributes['key'] = $this->apiKey;

        //POST https://partner.steam-api.com/ISteamMicroTxn/FinalizeTxn/v2/
        $finishTxnUrl = $this->baseUri . 'FinalizeTxn/v2/';
        //$response = $this->client->post($initTxnUrl, ['form_params' => $attributes, 'verify' => false]);
        $response = $this->curlPostHttps($finishTxnUrl, $attributes);
        $result = json_decode($response, true);
        $result = $result['response'];

        if ($result['result'] == 'Failure') {
            \Log::info('steam_finish_order', $result);

            return ['code' => 500, 'message' => 'Steam Response Error'];
        }

        $order = SteamOrders::query()->where('orderid', $attributes['orderid'])->first();
        $itemId = $order->itemid;

        $result = $this->sendUserGoods($userId, $itemId);
        $status = 2;
        if ($result) {
            $status = 3;
        }

        $order->user_id = $userId;
        $order->transid = $result['params']['transid'];
        $order->pay_state = $status;
        $order->save();

        return ['code' => 0];
    }

后記:數(shù)據(jù)庫設(shè)計(jì)

Schema::create('steam_orders', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('user_id')->nullable();
            $table->integer('orderid');
            $table->integer('transid')->nullable()->comment('steam交易id');
            $table->integer('steamid');
            $table->string('currency')->comment('貨幣類型');
            $table->string('appid');
            $table->string('language');
            $table->string('itemid', 50)->comment('商品id');
            $table->tinyInteger('qty')->default(1)->comment('購買數(shù)量');
            $table->bigInteger('amount')->default(0)->comment('總價(jià):分');
            $table->string('description', 255)->comment('商品描述');
            $table->tinyInteger('pay_state')->default(1)->comment('交易狀態(tài):1未支付,2支付完成,3已發(fā)貨');
            $table->timestamps();
        });
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容