苹果订阅服务器端开发

有时候我们想做一个苹果订阅功能,需要在苹果开发者后台添加订阅商品productid/

订阅需要增加一个参数: password: 秘钥, 就可以了, 但是官方文档说秘钥仅仅用在自动续订上面

大家叫后台加个验证,如果苹果验证返回21004的话(21004 你提供的共享密钥和账户的共享密钥不一致),就加上password字段去验证,可以成功。 秘钥去https://itunesconnect.apple.com/ 里面对应的APP里创建


如果没有开通此功能,需要点击下开启共享密钥,拿到password后,服务器端支付验证和普通内购相差不大,只不过验证内购时(即使是消耗型商品)必须带上password字段。

//苹果支付验证

publicfunctiongetReceiptData($receipt, $isSandbox =false,$password='')

    {

//沙盒验证地址

if($isSandbox)

        {

$endpoint ='https://sandbox.itunes.apple.com/verifyReceipt';

        }

else

        {

$endpoint ='https://buy.itunes.apple.com/verifyReceipt';

        }

$postarray =array('receipt-data'=> $receipt,'password'=>$password);

if($password==''){

unset($postarray['password']);

        }

        $postData = json_encode($postarray);

        $ch = curl_init($endpoint);

curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);

curl_setopt($ch, CURLOPT_POST,true);

        curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER,0);

curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST,0);

        $response = curl_exec($ch);

        $errno    = curl_errno($ch);

        $errmsg  = curl_error($ch);

        curl_close($ch);


if($errno !=0)

        {

thrownewException($errmsg, $errno);

        }

$data = json_decode($response,true);

if(!is_array($data))

        {

thrownewException('Invalid response data');

        }

if(!isset($data['status']) || $data['status'] !=0)

        {

thrownewException('Invalid receipt');

        }

return$data['receipt']['in_app'];

}

代码里的password就是后台看到的共享密钥。

这里提供一个demo来验证

functiontest()

//防止网络不稳定验证失败

$Payload="MIIXHQYJKoZIhvcNAQcCoIIXDjCCFwoCAQExCzAJBgUrDgMCGgUAMIIGvgYJKoZIhvcNAQcBoIIGrwSCBqsxgganMAoCAQgCAQEEAhYAMAoCARQCAQEEAgwAMAsCAQECAQ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LXypbtUbLCnwdD3y0jApxA+Ie/073MeHtTqJ+O5sN1yu5IDlb8xSouOt0Wm6gpKU3ZsYvaS4Wge9KvKgV7t+/wlBFR8cWVuCs3tqHwdsFvlFXS44e7wI4HJVfxrxZJgqpAIYotRRA1SeyiQAaAH5qx2IrOYazTAs7J9oiemVsTqArEQsuRG6Rk3OLeigXa1H5QC1he6QMyqgJA1ztyzFXld0wQZ07mpcaKLRdB7S+Q6Rca/kwIWrHtJstbZcCu8+igkJz88l9Z4RbO1gIE79Dg1/jZ53OPZhLCfIo=";

//开始执行验证 

try

//getReceiptData($receipt, $isSandbox = false,$password='')

$info = getReceiptData($Payload,false,'hello123456');

}

//捕获异常 

catch(Exception$e)

var_dump(serialize($e->getMessage())); 

//$info = getReceiptData($Payload, true);   

}

test();

?>

你可能感兴趣的:(苹果订阅服务器端开发)