PHP

苹果支付PHP代码

代码如下:

<?php
/**
 * 苹果支付
 * 
 * 
 * @name kevinxie
 * @date: 2020-04-24
 */
class Lib_Applepay
{
	protected static $_instance = array();
	public function __construct()
	{
		
	}
	/**
	 * 对像生产器
	 *
	 * @return object
	 */
	public static function factory()
	{
		if (( ! isset( self::$_instance[__METHOD__])) || ( ! is_object( self::$_instance[__METHOD__])))
		{
			self::$_instance[__METHOD__] = new self();
		}
		return self::$_instance[__METHOD__];
	}
	/**
	 * 验证AppStore内付
	 * 
	 * @param  string $receipt_data 付款后凭证
	 * @param  int $sandbox 是否沙箱模式,1沙箱0正式
	 * @return array                验证是否成功
	 */
	public function validateApplePay( $receipt_data, $sandbox=0)
	{
	    // 验证参数
	    if ( strlen( $receipt_data) < 20 )
	    {
	        $result=array(
	            'status'=>false,
	            'message'=>'非法参数'
	        );
	        return $result;
	    }
	    // 请求验证
	    $html = Lib_Applepay::factory()->acurl($receipt_data);

	    // 如果是沙盒数据 则验证沙盒模式
	    if( $html['status'] == '21007')
	    {
	        // 请求验证
	        $html = Lib_Applepay::factory()->acurl( $receipt_data, 1);
	        $html['sandbox'] = '1';
	    }
	    // 判断是否购买成功
	    if( $html['status'] === 0)
	    {
	        $result=array(
	            'status'=>true,
	            'message'=>'购买成功',
	            'detail'=>$html['receipt']
	            );
	    }
	    else
	    {
	        $result=array(
	            'status'=>false,
	            'message'=>'购买失败 status:'.$html['status']
	        );
	    }
	    
	    return $result;
	}
	/**
     * 21000 App Store不能读取你提供的JSON对象
     * 21002 receipt-data域的数据有问题
     * 21003 receipt无法通过验证
     * 21004 提供的shared secret不匹配你账号中的shared secret
     * 21005 receipt服务器当前不可用
     * 21006 receipt合法,但是订阅已过期。服务器接收到这个状态码时,receipt数据仍然会解码并一起发送
     * 21007 receipt是Sandbox receipt,但却发送至生产系统的验证服务
     * 21008 receipt是生产receipt,但却发送至Sandbox环境的验证服务
     * 
     * https://developer.apple.com/cn/documentation/storekit/in-app_purchase/validating_receipts_with_the_app_store/
     */
    public function acurl( $receipt_data, $sandbox=0)
    {
        //小票信息
        $POSTFIELDS = array("receipt-data" => $receipt_data);
        $POSTFIELDS = json_encode($POSTFIELDS);
        //正式购买地址 沙盒购买地址
        $url_buy     = "https://buy.itunes.apple.com/verifyReceipt";
        $url_sandbox = "https://sandbox.itunes.apple.com/verifyReceipt";
        $url = $sandbox ? $url_sandbox : $url_buy;
        $result = $this->curl( $url, $POSTFIELDS);
        return $result;
    }
	/**
	 * 获取网页数据
	 * @param string $url
	 * @param array $post_data post的数据,为空时表示get请求
	 * @param string $json 返回数据格式,0表示json 1原数据返回
	 * @return array/int
	 */
	public function curl( $url, $post_data=array(), $json=1, $timeout=3)
	{

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_HEADER, 0);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);

		if( ! empty( $post_data))
		{
			curl_setopt($ch, CURLOPT_POST, true);
			if( is_array( $post_data))
			{
				curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
			}
			else
			{
				curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
			}
		}
		$result = curl_exec( $ch);
		curl_close($ch);
		if ( $_REQUEST['debug'] == 1) {
			echo $result;exit;
		}
		return $data = empty($json) ? $result : json_decode($result, true);
	}
}//end Lib_Applepay