一、订阅购买验证
android端采用google service account进行校验
1、打开https://cloud.google.com/console创建一个project;
2、打开project->APIs & auth->Credentials点击CREATE NEW CLIENT ID创建一个SERVICE ACCOUNT;
3、打开project->APIs & auth->APIs Google Play Android Developer API状态打开;
4、打开https://play.google.com/apps/publish/添加SERVICE ACCOUNT的查询权限;
5、打开Settings->User account & rights->Invite new user submit 添加刚才创建的SERVICE ACCOUNT;
6、然后勾选 查看财务报表 选项,增加查看订单的权限;
以上设定好后才能有权限验证课金信息,同一个google play账号下只需设定一次。
7、借助三方库
svn checkout http://google-api-php-client.googlecode.com/svn/trunk/ google-api-php-client-read-only
或者 https://github.com/zhoutingze/adtuu.git
require_once 'service/Google_Model.php'; require_once 'service/Google_Service.php'; require_once 'service/Google_ServiceResource.php'; require_once 'auth/Google_AssertionCredentials.php'; require_once 'auth/Google_Signer.php'; require_once 'auth/Google_P12Signer.php'; require_once 'service/Google_BatchRequest.php'; require_once 'external/URITemplateParser.php'; require_once 'auth/Google_Auth.php'; require_once 'cache/Google_Cache.php'; require_once 'io/Google_IO.php'; require_once('service/Google_MediaFileUpload.php'); $client_id = '175160586549-4vlcm5piovihb4jt3sd35cqi4tfxxxxx.apps.googleusercontent.com'; $account_name = '175160586549-4vlcm5piovihb4jt3sd35cqi4txxxxx@developer.gserviceaccount.com'; $key_file_name = 'MaTuRi-2b0228176bc8.p12'; $receipt = '{"orderId":"12999763169054705758.1343513511797676","packageName":"jp.maturi.android.hsb","productId":"m_member_highspeedboyz_2.99","purchaseTime":1410833767578,"purchaseState":0,"developerPayload":"CAQEAhaYH9MrPxYOPrLJAyFCdpYC76V6MPgu0v9","purchaseToken":"hnmmfkliefklbeeenooidabh.AO-J1OwRIVqhDtAElccXHbeEARLgpdaDgGFRfixj97lQyb-mOhnZydhgohqhnPgd0k8BD0InF7weqGcKhRWF6cfGEAQDxEZGv_JiDk3bCIS1hDOMUf8hCHQOOwowEYOBm6BmERQKazeV3xUNWU-cnVk0CZAdvtTlYA"}'; $receipt = json_decode($receipt, true); try { $client = new Google_Client(); $client->setApplicationName($receipt['packageName']); $client->setClientId($client_id); $key_file = APPPATH . 'config/' . ENVIRONMENT . '/android_purchase_file/' . $key_file_name; $key = file_get_contents($key_file); $auth = new Google_AssertionCredentials( $account_name, array('https://www.googleapis.com/auth/androidpublisher'), $key ); $client->setAssertionCredentials($auth); $AndroidPublisherService = new Google_AndroidPublisherService($client); $res = $AndroidPublisherService->purchases->get( $receipt['packageName'], $receipt['productId'], $receipt['purchaseToken'] ); $status = 0; if (isset($res['validUntilTimestampMsec'])) { $current_time = time(); $valid_time = $res['validUntilTimestampMsec'] / 1000; if ($current_time < $valid_time) { $status = 1; } } else { throw new Exception('Invalid response data'); } // 其他操作 当然这里还需要验证包名是否一致 } catch (Exception $e) { // 异常 }
二、一次性永久购买(比如金币购买)
// rsa_key这个是google购买中配置的 $rsa_key = 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmqt0a6kpHbqAdBJWLnxtBesukwJQ2SP/20XXIMqKYdBay3llLxZyGanzyrnq8+90DMAT40JJJq7pDNZd3vtJU/eRZBN4vUA4VscoF5GrX/7gQ3ThxPvypTkzxmcrCKPv/SWRoXhpA1Vjh+U7VSL6LJYqOhR6IT5UrcgWkgdf6EOvJ4KGDC1M5nVqFFhz6/lXc+uASC9pVxh6V+8Eh4ZvqDkWyBKybsbUKTOgDViMJWXujygRu3iTTJcRT2rzTrWXj8hHWkZpoU1hPg64M1QK1PSeGU7+lKgCvkHcO0ZO+VqrZ+ZUnBiVghcHz/QnxeSFLsX0mOHWDAsNNtFVDRkFjwIDAQAB'; // signature 和 signed_data是购买后google返回的,好像在测试环境中取不到其中的某一个值,android包要打阿尔法版本才有 $receipt = '{"signature":"ZkF1AAbUon1HI0K54dLlRYd7c+j1cKkaWiYuJzGyAZtYIbZgrgTJXb/Xu0XgwrS0SN4sFuvmseYy4gBXM+Iljs6JySLblQSAqIqZFhhWutmAyO13c17ZJautGgePmti9o9+qxa92X1Pt4BKuV/zDPTbgBHaVgCE1T1sKaHhOdvppZgYxFN1HSqe3E4c57A4duLK0f7prdmA/k0pqZe9Pjv3I5Lf9HhS6QHFPL3vhaN8LMHUbHp/02UFXDrIgJGK6V3JFSTnYrHsnWKMs8Z6qzAFiEELvvdHoHOG42e1J7dZhnUlzhmheTbtePkwUlRRzq0lU7WP6foyc5uVBLT01vQ==","signed_data":"{"orderId":"12999763169054705758.1378495280758690","packageName":"jp.maturi.android.xxxx","productId":"inapppurchase_xxxx_4.99","purchaseTime":1418299229881,"purchaseState":0,"developerPayload":"PraisePurchase_CAQEAhaYH9MrPxYOPrLJAyFCdpYC76V6MPgu0v9","purchaseToken":"odepcbhgckffgokgcdcfaiei.AO-J1Oz8Ex89Uw_urARZhhyP04vPI18xYd_0Gs1YY6PF9EIkgRk3wPGFba6oOER-Yel-M7A7fcBc-LR3ZQdLTYFcyKy5TQHWUyagyMWXjh2gf7SIEiv02JzGQbSUkOXlr5KYc9vc4KSZmAkjqyeCuu2DBLxAwN6rb8DcK-Y_VCyGioGkricYgmE"}"}'; $receipt = json_decode($receipt, true); $signed_data = $receipt->signed_data; $signature = $receipt->signature; _openssl_verify($signed_data, $signature, $rsa_key); /** * google play in-app-billing verification * 「購入情報」を$signed_data * 「暗号化された署名」を$signatureとします。 * 「公開鍵」を使用するために用意するものは、PEM形式の証明書です。 * まずDeveloper Console上で取得したDER(Distinguished Encoding Rules)形式を、 * PEM(Privacy Enhanced Mail)形式に変換したものを用います。 * PEMはBase64エンコードされたデータを含み、 * ヘッダーとフッターを付加したフォーマットですので、 * ヘッダーとフッターの間にBase64エンコードされた文字列が入っていればOKです。 * ヘッダーとフッターは次のものを利用します。 */ private function _openssl_verify($signed_data, $signature, $rsa_key) { // RSA(PEM形式)公開鍵の生成 $cert = "-----BEGIN PUBLIC KEY-----" . PHP_EOL . chunk_split($rsa_key, 64, PHP_EOL) . "-----END PUBLIC KEY-----"; // openssl_get_publickey()はopenssl_pkey_get_public()のエイリアス $pubkey = openssl_get_publickey($cert); if (false === $pubkey) { exit('error'); } // 署名をBase64デコードする(バイナリになる) $signature = base64_decode($signature); // openssl_verify()のデフォルトアルゴリズムはSHA1なので、 // OPENSSL_ALGO_SHA1は渡さなくてもよい $result = openssl_verify($signed_data, $signature, $pubkey, OPENSSL_ALGO_SHA1); // キーをメモリから解放 openssl_free_key($pubkey); var_dump($result); return $result; }