PHPIOS支付回调

<?php


namespace app\api\controller\v1;


use app\api\controller\Controller;

class IosNotify extends Controller
{

	/**
	 * 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环境的验证服务
	 */
	public function acurl($receipt_data, $sandbox = 0)
	{
		//小票信息
		$POSTFIELDS = ["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;

		//简单的curl
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $POSTFIELDS);
		$result = curl_exec($ch);
		curl_close($ch);

		return $result;
	}

	/**
	 * 验证AppStore内付
	 *
	 * @param  string  $receipt_data  付款后凭证
	 *
	 * @return array                验证是否成功
	 */
	public function validate_apple_pay()
	{

		$receipt_data = request()->post()['receipt'];
		$order_no = request()->post()['order_no'];

		// 验证参数
		if (strlen($receipt_data) < 20) {
			return $this->renderError('购买失败,非法参数!');
		}

		// 请求验证
		$html = $this->acurl($receipt_data);
		$data = json_decode($html, true);

		// 如果是沙盒数据 则验证沙盒模式
		if ($data['status'] == '21007') {
			// 请求验证
			$html = $this->acurl($receipt_data, 1);
			$data = json_decode($html, true);
			$data['sandbox'] = '1';
		}

		if (isset($_GET['debug'])) {
			exit(json_encode($data));
		}

		// 判断是否购买成功
		if (intval($data['status']) === 0) {

			$model = new \app\common\model\RechargeOrderIntegral();

			$res = $model->orderNotify([
				'order_no'=>$order_no,
				'pay_type'=>3,
			]);

			if($res){
				return $this->renderSuccess([],"支付成功");
			}else{
				return $this->renderError("支付失败");
			}

		} else {
			return $this->renderError('购买失败 status:'.$data['status']);
		}

	}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值