Apple.php 1.7 KB

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