google支付訂單查詢

官方文檔鏈接:https://developers.google.cn/android-publisher/

github鏈接:https://github.com/google/google-api-php-client

查詢google的支付訂單

public static function curl_google_product(){
    // include your composer dependencies
    require_once 'vendor/autoload.php';
    try{
        putenv('GOOGLE_APPLICATION_CREDENTIALS=/PATH/WEB/xx.json'); //配置系統(tǒng)環(huán)境變量
        $client = new \Google_Client(); //實例化鏈接
        $client->useApplicationDefaultCredentials();
        $client->addScope(\Google_Service_AndroidPublisher::ANDROIDPUBLISHER);//添加使用域
        // OR use environment variables (recommended)
        $service = new \Google_Service_AndroidPublisher($client );
        $packageName='XXX';  //App包名
        $productId='com.XXX'; //商品ID
        $token = 'XXX'; 
        $optps=array();
        $resp = $service->purchases_products->get( $packageName, $productId, $token, $optps );
        $resp = (array)$resp;
        return $resp;
    }catch(Exception $e){
        echo $e->getCode(),'|',$e->getMessage();
        exit;
    }
}

如果返回錯誤為The current user has insufficient permission...

Google_Service_Exception: {
    "error": {
        "errors": [{
            "domain": "androidpublisher",
            "reason": "permissionDenied",
            "message": "The current user has insufficient permissions to perform the requested operation."
        }],
        "code": 401,
        "message": "The current user has insufficient permissions to perform the requested operation."
   }
}

需要在谷歌控制臺中將xx.json中的client_email設(shè)置為管理員用戶

查詢google的訂閱訂單

private static function curl_google_subscription(){
    // include your composer dependencies
    require_once 'vendor/autoload.php';
    try{
        putenv('GOOGLE_APPLICATION_CREDENTIALS=/PATH/WEB/xx.json'); //配置系統(tǒng)環(huán)境變量
        $client = new \Google_Client(); //實例化鏈接
        $client->useApplicationDefaultCredentials();
        $client->addScope(\Google_Service_AndroidPublisher::ANDROIDPUBLISHER);//添加使用域
        // OR use environment variables (recommended)
        $service = new \Google_Service_AndroidPublisher($client );
        $packageName='XXX';  //App包名
        $subscriptionId='com.XXX'; //訂購ID
        $token = 'XXX'; 
        $optps=array();
        $resp = $service->purchases_subscriptions->get( $packageName, $subscriptionId, $token, $optps );
        $resp = (array)$resp;
        return $resp;
    }catch(Exception $e){
        echo $e->getCode(),'|',$e->getMessage();
        exit;
    }
}

使用公鑰進行算法驗證(安全性上不高,不推薦)

    public static function curl_post_google($inapp_purchase_data,$inapp_data_signature){
        //$inapp_purchase_data,$inapp_data_signature 為用戶付款后,谷歌返回值(或app端返回)
        $google_public_key='公鑰';
        $public_key ="-----BEGIN PUBLIC KEY-----\n".chunk_split($google_public_key, 64, "\n")."-----END PUBLIC KEY-----";
        $public_key_handle = openssl_get_publickey($public_key);
        $result = openssl_verify($inapp_purchase_data, base64_decode($inapp_data_signature), $public_key_handle, OPENSSL_ALGO_SHA1);
        if ($result !== 1) {
            return $result;
        }
        return true;
    }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

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

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