PHP

GooglePay验证支付成功是否可以发货PHP代码

验证支付成功是否可以发货代码如下:

/**
 * 验证支付成功是否可以发货
 * 
 * @param string $signedData
 * @param string $signature
 * @return boolean
 */
public function checkGooglePayByPublickey( $signedData, $signature)
{
	$signature = base64_decode( $signature, true);
	$publicKey = 'publicKey';
	$packageName = 'packageName';
	$publicKey = "-----BEGIN PUBLIC KEY-----\n".chunk_split( $publicKey, 64, "\n" ).'-----END PUBLIC KEY-----';
	$key = openssl_get_publickey( $publicKey );
	
	if ( empty( $key ) )
	{
		//Please pass a Base64-encoded public key from the Market portal
		return false;
	}
	$jsonSignedData = base64_decode( $signedData, true );
	$array = json_decode( $jsonSignedData, true);
	
	if ( empty( $array ) )
	{
		//fail to decode signedData
		return false;
	}

	if ( empty( $array['packageName'] ) )
	{
		//packageName is null
		return false;
	}

	if ( $array['packageName'] != $packageName )
	{
		//packageName is not match
		return false;
	}
	$result = openssl_verify( $jsonSignedData, base64_decode( $signature ), $key, OPENSSL_ALGO_SHA1 );
	if ( 0 === $result )
	{
		//fail to verify
		return false;
	}
	else if ( 1 !== $result )
	{
		//Unknown error verifying the signature in openssl_verify
		return false;
	}
	return true;
}