Apple.php 1.9 KB

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