Alipay.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <?php namespace Pay\Lib;
  2. use Dever;
  3. Dever::apply('sdk/alipay', 'pay');
  4. class Alipay extends Core
  5. {
  6. private $signType = 'RSA2';
  7. public function __construct($config)
  8. {
  9. $project = Dever::project('pay');
  10. # 通知接口
  11. $config['notify'] = $this->url($config['type'], $config['id']);
  12. $this->config = $config;
  13. }
  14. /**
  15. * 通知
  16. */
  17. public function notify()
  18. {
  19. $type = Dever::input('type');
  20. if ($type == 1) {
  21. $input = $_GET;
  22. unset($input['account_id']);
  23. $result = $this->check($input);
  24. if ($result) {
  25. $status = 1;
  26. } else {
  27. $status = 2;
  28. }
  29. $refer = Dever::input('refer');
  30. $refer = urldecode($refer) . '&status=' . $status;
  31. Dever::location($refer);
  32. } else {
  33. $input = $_POST;
  34. if (!$input) {
  35. echo 'fail';die;
  36. }
  37. unset($input['account_id']);
  38. $this->log('支付回调-初始化', $input);
  39. $result = $this->check($input);
  40. if ($result) {
  41. $this->log('支付回调-获取数据', $result);
  42. if($input['trade_status'] == 'TRADE_FINISHED') {
  43. #退款日期超过可退款期限后(如三个月可退款),支付宝系统发送该交易状态通知
  44. $this->updateOrder($result['out_trade_no'], $result['total_amount'], '已超过退款期限');
  45. } elseif ($input['trade_status'] == 'TRADE_SUCCESS') {
  46. #付款完成后,支付宝系统发送该交易状态通知
  47. $this->updateOrder($result['out_trade_no'], $result['total_amount']);
  48. }
  49. echo 'success';die;
  50. } else {
  51. echo 'fail';die;
  52. }
  53. }
  54. }
  55. /**
  56. * 获取统一下单的基本信息
  57. */
  58. public function order($account_id, $project_id, $uid, $username, $product_id, $name, $cash, $openid = false, $type = 1, $order_id = false)
  59. {
  60. $order_id = $this->createOrder($uid, $username, $account_id, $project_id, $product_id, $name, $cash, $this->config['type'], $order_id);
  61. $aop = new \AopClient();
  62. $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
  63. if ($this->config['box'] == 2) {
  64. $aop->gatewayUrl = 'https://openapi.alipaydev.com/gateway.do';
  65. }
  66. $aop->appId = $this->config['appid'];
  67. $aop->rsaPrivateKey = $this->config['private_key'];
  68. $aop->alipayrsaPublicKey = $this->config['public_key'];
  69. $aop->signType = $this->signType;
  70. // 开启页面信息输出
  71. $aop->debugInfo = true;
  72. $request['out_trade_no'] = $order_id;
  73. $request['body'] = $name;
  74. $request['total_amount'] = $cash;
  75. $request['subject'] = $name;
  76. $request['timeout_express'] = $this->config['timeout'];
  77. $content = json_encode($request, JSON_UNESCAPED_UNICODE);
  78. $request = new \AlipayTradeWapPayRequest();
  79. $request->setNotifyUrl($this->config['notify']);
  80. $request->setReturnUrl($this->config['refer']);
  81. $request->setBizContent($content);
  82. if ($type == 1) {
  83. $result = $aop->pageExecute($request, 'post');
  84. } else {
  85. $result = $aop->Execute($request);
  86. }
  87. return $result;
  88. }
  89. /**
  90. * 获取二维码支付
  91. */
  92. public function qrcode($order, $refer)
  93. {
  94. $notify = new \NativePay();
  95. $result = $notify->GetPayUrl($order);
  96. $url = $result['code_url'];
  97. return $url;
  98. }
  99. /**
  100. * 获取小程序支付
  101. */
  102. public function applet($order)
  103. {
  104. $result = array();
  105. return $result;
  106. }
  107. /**
  108. * 获取页面支付
  109. */
  110. public function page($order, $refer)
  111. {
  112. return $order;
  113. }
  114. private function check($data)
  115. {
  116. $aop = new \AopClient();
  117. $aop->alipayrsaPublicKey = $this->config['public_key'];
  118. $result = $aop->rsaCheckV1($data, $this->config['public_key'], $this->signType);
  119. return $result;
  120. }
  121. }