Alipay.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. $trade_type = $this->getType($type);
  61. $order_id = $this->createOrder($uid, $username, $account_id, $project_id, $product_id, $name, $cash, $this->config['type'], $order_id);
  62. $aop = new \AopClient();
  63. $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
  64. if ($this->config['box'] == 2) {
  65. $aop->gatewayUrl = 'https://openapi.alipaydev.com/gateway.do';
  66. }
  67. $aop->appId = $this->config['appid'];
  68. $aop->rsaPrivateKey = $this->config['private_key'];
  69. $aop->alipayrsaPublicKey = $this->config['public_key'];
  70. $aop->signType = $this->signType;
  71. // 开启页面信息输出
  72. $aop->debugInfo = true;
  73. $request['out_trade_no'] = $order_id;
  74. $request['body'] = $name;
  75. $request['total_amount'] = $cash;
  76. $request['subject'] = $name;
  77. $request['timeout_express'] = $this->config['timeout'];
  78. $content = json_encode($request, JSON_UNESCAPED_UNICODE);
  79. $request = new \AlipayTradeWapPayRequest();
  80. $request->setNotifyUrl($this->config['notify']);
  81. $request->setReturnUrl($this->config['refer']);
  82. $request->setBizContent($content);
  83. if ($type == 1) {
  84. $result = $aop->pageExecute($request, 'post');
  85. } else {
  86. $result = $aop->Execute($request);
  87. }
  88. return $result;
  89. }
  90. /**
  91. * 获取二维码支付
  92. */
  93. public function qrcode($order, $refer)
  94. {
  95. $notify = new \NativePay();
  96. $result = $notify->GetPayUrl($order);
  97. $url = $result['code_url'];
  98. return $url;
  99. }
  100. /**
  101. * 获取小程序支付
  102. */
  103. public function applet($order)
  104. {
  105. $result = array();
  106. return $result;
  107. }
  108. /**
  109. * 获取页面支付
  110. */
  111. public function page($order, $refer)
  112. {
  113. return $order;
  114. }
  115. private function getType($type)
  116. {
  117. switch ($type) {
  118. case 1:
  119. $type = 'JSAPI';
  120. break;
  121. case 2:
  122. $type = 'NATIVE';
  123. break;
  124. }
  125. return $type;
  126. }
  127. private function check($data)
  128. {
  129. $aop = new \AopClient();
  130. $aop->alipayrsaPublicKey = $this->config['public_key'];
  131. $result = $aop->rsaCheckV1($data, $this->config['public_key'], $this->signType);
  132. return $result;
  133. }
  134. }