问题
A lot of people ask how to write server side code for in-app billing verificartion. Can anybody publish such code? Or know where such code is. How to install in on the server? There are similar subjects
I could not understand it. I don't know php. Is it the next nightmare which I must study?
Thanks for help and advices.
回答1:
Actually it's pretty easy, you just need a small function like this in PHP:
function checkPayment($data, $signature)
{
$base64EncodedPublicKey = "yourBase64PublicKey";
$openSslFriendlyKey = "-----BEGIN PUBLIC KEY-----\n" . chunk_split($base64EncodedPublicKey, 64, "\n") . "-----END PUBLIC KEY-----";
$publicKeyId = openssl_get_publickey($openSslFriendlyKey);
$result = openssl_verify ($data, base64_decode($signature), $publicKeyId, OPENSSL_ALGO_SHA1);
/*
if ($result == 1) {
echo "Success";
} elseif ($result == 0) {
echo "Verification Failed";
}
*/
return $result;
}
回答2:
Here is (uncompleted) python example:
from M2Crypto import BIO, RSA, EVP
ANDROID_PK = """-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAgmZW0GxWr0v1ndLfxHbV2ruWcmQ
<some lines skipped>
cwWjx5sWSahVp0M5aYRysSkGGjSxe1wIDAQAB
-----END PUBLIC KEY-----"""
def validate_google_play_signature(signed_data, signature_base64, public_key):
# http://stackoverflow.com/a/546476/227024
bio = BIO.MemoryBuffer(public_key)
rsa = RSA.load_pub_key_bio(bio)
pubkey = EVP.PKey()
pubkey.assign_rsa(rsa)
pubkey.reset_context(md="sha1")
pubkey.verify_init()
pubkey.verify_update(signed_data)
signature = base64.b64decode(signature_base64)
return bool(pubkey.verify_final(signature))
来源:https://stackoverflow.com/questions/12215586/in-app-billing-verification-does-server-side-code-example-exist-somewhere