Apple.php 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. $type = 'wechat';
  13. $order_id = $this->createOrder($uid, $username, $account_id, $project_id, $product_id, $name, $cash, $type, $order_id);
  14. if (strlen($receipt_data) < 20) {
  15. $this->updateOrder($order_id, $cash, '参数非法');
  16. Dever::alert('参数非法');
  17. }
  18. $result = $this->curl($receipt_data);
  19. //如果是沙盒数据 则验证沙盒模式
  20. if ($result['status'] == 21007) {
  21. $result = $this->curl($receipt_data, true);
  22. $result['sandbox'] = 1;
  23. }
  24. // 判断是否购买成功
  25. if (intval($result['status']) === 0) {
  26. $this->updateOrder($order_id, $cash);
  27. $result['yes'] = 1;
  28. } else {
  29. $this->updateOrder($order_id, $cash, 'error');
  30. $result['yes'] = 2;
  31. }
  32. return $result;
  33. }
  34. private function curl($receipt_data, $sandbox = false)
  35. {
  36. //小票信息
  37. $input = array("receipt-data" => $receipt_data);
  38. $post = json_encode($input);
  39. //正式购买地址 沙盒购买地址
  40. $url_buy = "https://buy.itunes.apple.com/verifyReceipt";
  41. $url_sandbox = "https://sandbox.itunes.apple.com/verifyReceipt";
  42. $url = $sandbox ? $url_sandbox : $url_buy;
  43. //简单的curl
  44. $ch = curl_init($url);
  45. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  46. curl_setopt($ch, CURLOPT_POST, 1);
  47. curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
  48. $result = curl_exec($ch);
  49. curl_close($ch);
  50. $input['url'] = $url;
  51. $this->log('苹果支付-调用苹果接口', $input);
  52. return Dever::json_decode($result);
  53. }
  54. }