Apple.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php namespace Pay\Lib;
  2. use Dever;
  3. # 苹果内购支付验证接口 代码摘自网络
  4. class Apple extends Core
  5. {
  6. /**
  7. * 检测
  8. */
  9. public function check($receipt_data)
  10. {
  11. if (strlen($receipt_data) < 20) {
  12. Dever::alert('参数非法');
  13. }
  14. $result = $this->curl($receipt_data);
  15. //如果是沙盒数据 则验证沙盒模式
  16. if ($result['status'] == 21007) {
  17. $result = $this->curl($receipt_data, true);
  18. $result['sandbox'] = 1;
  19. }
  20. if (Dever::input('debug')) {
  21. exit(json_encode($result));
  22. }
  23. // 判断是否购买成功
  24. if (intval($result['status']) ===0 ) {
  25. } else {
  26. Dever::alert('购买失败 status:'.$data['status']);
  27. }
  28. $this->log('支付回调-初始化', $input);
  29. $tools = new \Cmbc\Handle();
  30. $callback = $tools->get('notify', $this->config);
  31. $result = $callback->request($input, $this);
  32. if ($result) {
  33. $this->log('支付回调-获取数据', $result);
  34. $this->updateOrder($result['mhtOrderNo'], $result['mhtOrderAmt']);
  35. echo 'success=Y';die;
  36. } else {
  37. echo 'success=N';die;
  38. }
  39. return;
  40. }
  41. private function curl($receipt_data, $sandbox = false)
  42. {
  43. //小票信息
  44. $input = array("receipt-data" => $receipt_data);
  45. $post = json_encode($input);
  46. //正式购买地址 沙盒购买地址
  47. $url_buy = "https://buy.itunes.apple.com/verifyReceipt";
  48. $url_sandbox = "https://sandbox.itunes.apple.com/verifyReceipt";
  49. $url = $sandbox ? $url_sandbox : $url_buy;
  50. //简单的curl
  51. $ch = curl_init($url);
  52. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  53. curl_setopt($ch, CURLOPT_POST, 1);
  54. curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  55. $result = curl_exec($ch);
  56. curl_close($ch);
  57. $input['url'] = $url;
  58. $this->log('苹果内购支付-调用苹果接口', $input);
  59. return Dever::json_decode($result);
  60. }
  61. }