Developer/UNITY

unibill에서 PHP를 이용해 Google In App Purchase Verification(구글 인앱 결제 검증)

블로blow 2014. 8. 13. 11:23
728x90

유니빌에서 결제를 정상적으로 완료하고 나면,


private void onPurchased(PurchasableItem item);


함수를 호출하게 됩니다.

그럼 이 함수 안에서,

json과 signature의 값을 다음과 같이 알아냅니다.



private void onPurchased(PurchasableItem item) {

string[] receiptList = Unibiller.GetAllPurchaseReceipts(item);
string receipt = receiptList[receiptList.Length-1];

Dictionary<stringobjectjsonFullDic = (Dictionary<stringobject>)CMiniJSON.Json.Deserialize(receipt);
foreach (KeyValuePair<stringobjectpair in jsonFullDic) {
    //Debug.Log(pair.Value);
    if(pair.Key == "json") {
        purchaseJson = pair.Value.ToString();
    }
    else if (pair.Key == "signature"{
        signature = pair.Value.ToString();
    }
}

}




알게된, purchaseJson과 signature를 서버로 보내 PHP로 확인합니다.



public function verifycheck($json = '', $signature = "") {

//json 확인.

 $data = json_decode($json,true);

        $data = json_encode($data);


//$key 만들기.

//이 값은, play.google.com/apps/publish에서, 자신의 앱에서 서비스및 API->라이선스 및 인앱결제 -> 이 애플리케이션용 라이선스 키에서 찾을수 있습니다.


$publicKey = "MIIBIjANBg5CLWp0IwIDAQAB"; 

        $key = "-----BEGIN PUBLIC KEY-----\n";

 $key .= chunk_split($publicKey, 64, "\n");

 $key .= "-----END PUBLIC KEY-----";

        $key = openssl_get_publickey($key);


//key 확인.

        if (false === $key) {

                exit("error openssl_get_publickey");

        }


//openssl_verify를 이용해 검수.

        $result = openssl_verify($data, base64_decode($signature), $key);

        

//확인.

        if ($result == 1) {

                //echo "good";

        } else if ($result == 0) {

                //echo "bad";

        } else {

                //echo "error";

        }

        

        return $result;

}



원본 : http://blog.evendanan.net/2012/03/In-App-Purchase-Verification-using-PHP-OR-Making-sure-you-ain-t-getting-ripped-off-PHP-style


728x90