Alipay.class.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. <?php
  2. defined('IN_PHPCMS') or exit('No permission resources.');
  3. if (isset($set_modules) && $set_modules == TRUE)
  4. {
  5. $i = isset($modules) ? count($modules) : 0;
  6. $modules[$i]['code'] = basename(__FILE__, '.class.php');
  7. $modules[$i]['name'] = L('alipay', '', 'pay');
  8. $modules[$i]['desc'] = L('alipay_tip', '', 'pay');
  9. $modules[$i]['is_cod'] = '0';
  10. $modules[$i]['is_online'] = '1';
  11. $modules[$i]['author'] = 'PHPCMS开发团队';
  12. $modules[$i]['website'] = 'http://www.alipay.com';
  13. $modules[$i]['version'] = '1.0.0';
  14. $modules[$i]['config'] = array(
  15. array('name' => 'alipay_account','type' => 'text','value' => ''),
  16. array('name' => 'alipay_key','type' => 'text','value' => ''),
  17. array('name' => 'alipay_partner','type' => 'text','value' => ''),
  18. array('name' => 'service_type','type' => 'select','value' => '0'),
  19. );
  20. return;
  21. }
  22. pc_base::load_app_class('pay_abstract','','0');
  23. class Alipay extends paymentabstract{
  24. public function __construct($config = array()) {
  25. if (!empty($config)) $this->set_config($config);
  26. if ($this->config['service_type']==1) $this->config['service'] = 'trade_create_by_buyer';
  27. elseif($this->config['service_type']==2) $this->config['service'] = 'create_direct_pay_by_user';
  28. else $this->config['service'] = 'create_partner_trade_by_buyer';
  29. $this->config['gateway_url'] = 'https://www.alipay.com/cooperate/gateway.do?_input_charset='.CHARSET;
  30. $this->config['gateway_method'] = 'POST';
  31. $this->config['notify_url'] = return_url('alipay',1);
  32. $this->config['return_url'] = return_url('alipay');
  33. pc_base::load_app_func('alipay');
  34. }
  35. public function getpreparedata() {
  36. $prepare_data['service'] = $this->config['service'];
  37. $prepare_data['payment_type'] = '1';
  38. $prepare_data['seller_email'] = $this->config['alipay_account'];
  39. $prepare_data['partner'] = $this->config['alipay_partner'];
  40. $prepare_data['_input_charset'] = CHARSET;
  41. $prepare_data['notify_url'] = $this->config['notify_url'];
  42. $prepare_data['return_url'] = $this->config['return_url'];
  43. // 商品信息
  44. $prepare_data['subject'] = $this->product_info['name'];
  45. $prepare_data['price'] = $this->product_info['price'];
  46. if (array_key_exists('url', $this->product_info)) $prepare_data['show_url'] = $this->product_info['url'];
  47. $prepare_data['body'] = $this->product_info['body'];
  48. //订单信息
  49. $prepare_data['out_trade_no'] = $this->order_info['id'];
  50. $prepare_data['quantity'] = $this->order_info['quantity'];
  51. // 物流信息
  52. if($this->config['service'] == 'create_partner_trade_by_buyer' || $this->config['service'] == 'trade_create_by_buyer') {
  53. $prepare_data['logistics_type'] = 'EXPRESS';
  54. $prepare_data['logistics_fee'] = '0.00';
  55. $prepare_data['logistics_payment'] = 'SELLER_PAY';
  56. }
  57. //买家信息
  58. $prepare_data['buyer_email'] = $this->order_info['buyer_email'];
  59. $prepare_data = arg_sort($prepare_data);
  60. // 数字签名
  61. $prepare_data['sign'] = build_mysign($prepare_data,$this->config['alipay_key'],'MD5');
  62. return $prepare_data;
  63. }
  64. /**
  65. * GET接收数据
  66. * 状态码说明 (0 交易完成 1 交易失败 2 交易超时 3 交易处理中 4 交易未支付5交易取消6交易发生错误)
  67. */
  68. public function receive() {
  69. $receive_sign = $_GET['sign'];
  70. $receive_data = $this->filterParameter($_GET);
  71. $receive_data = arg_sort($receive_data);
  72. if ($receive_data) {
  73. $verify_result = $this->get_verify('http://notify.alipay.com/trade/notify_query.do?partner=' . $this->config['alipay_partner'] . '&notify_id=' . $receive_data['notify_id']);
  74. if (preg_match('/true$/i', $verify_result))
  75. {
  76. $sign = '';
  77. $sign = build_mysign($receive_data,$this->config['alipay_key'],'MD5');
  78. if ($sign != $receive_sign)
  79. {
  80. error_log(date('m-d H:i:s',SYS_TIME).'| GET: signature is bad |'."\r\n", 3, CACHE_PATH.'pay_error_log.php');
  81. showmessage(L('illegal_sign'));
  82. return false;
  83. }
  84. else
  85. {
  86. $return_data['order_id'] = $receive_data['out_trade_no'];
  87. $return_data['order_total'] = $receive_data['total_fee'];
  88. $return_data['price'] = $receive_data['price'];
  89. switch ($receive_data['trade_status'])
  90. {
  91. case 'WAIT_BUYER_PAY': $return_data['order_status'] = 3; break;
  92. case 'WAIT_SELLER_SEND_GOODS': $return_data['order_status'] = 3; break;
  93. case 'WAIT_BUYER_CONFIRM_GOODS': $return_data['order_status'] = 3; break;
  94. case 'TRADE_CLOSED': $return_data['order_status'] = 5; break;
  95. case 'TRADE_FINISHED': $return_data['order_status'] = 0; break;
  96. case 'TRADE_SUCCESS': $return_data['order_status'] = 0; break;
  97. default:
  98. $return_data['order_status'] = 5;
  99. }
  100. return $return_data;
  101. }
  102. }
  103. else
  104. {
  105. error_log(date('m-d H:i:s',SYS_TIME).'| GET: illegality notice : flase |'."\r\n", 3, CACHE_PATH.'pay_error_log.php');
  106. showmessage(L('illegal_notice'));
  107. return false;
  108. }
  109. } else {
  110. error_log(date('m-d H:i:s',SYS_TIME).'| GET: no return |'."\r\n", 3, CACHE_PATH.'pay_error_log.php');
  111. showmessage(L('illegal_return'));
  112. return false;
  113. }
  114. }
  115. /**
  116. * POST接收数据
  117. * 状态码说明 (0 交易完成 1 交易失败 2 交易超时 3 交易处理中 4 交易未支付 5交易取消6交易发生错误)
  118. */
  119. public function notify() {
  120. $receive_sign = $_POST['sign'];
  121. $receive_data = $this->filterParameter($_POST);
  122. $receive_data = arg_sort($receive_data);
  123. if ($receive_data) {
  124. $verify_result = $this->get_verify('http://notify.alipay.com/trade/notify_query.do?service=notify_verify&partner=' . $this->config['alipay_partner'] . '&notify_id=' . $receive_data['notify_id']);
  125. if (preg_match('/true$/i', $verify_result))
  126. {
  127. $sign = '';
  128. $sign = build_mysign($receive_data,$this->config['alipay_key'],'MD5');
  129. if ($sign != $receive_sign)
  130. {
  131. error_log(date('m-d H:i:s',SYS_TIME).'| POST: signature is bad |'."\r\n", 3, CACHE_PATH.'pay_error_log.php');
  132. return false;
  133. }
  134. else
  135. {
  136. $return_data['order_id'] = $receive_data['out_trade_no'];
  137. $return_data['order_total'] = $receive_data['total_fee'];
  138. $return_data['price'] = $receive_data['price'];
  139. switch ($receive_data['trade_status']) {
  140. case 'WAIT_BUYER_PAY': $return_data['order_status'] = 3; break;
  141. case 'WAIT_SELLER_SEND_GOODS': $return_data['order_status'] = 3; break;
  142. case 'WAIT_BUYER_CONFIRM_GOODS': $return_data['order_status'] = 3; break;
  143. case 'TRADE_CLOSED': $return_data['order_status'] = 5; break;
  144. case 'TRADE_FINISHED': $return_data['order_status'] = 0; break;
  145. case 'TRADE_SUCCESS': $return_data['order_status'] = 0; break;
  146. default:
  147. $return_data['order_status'] = 5;
  148. }
  149. return $return_data;
  150. }
  151. }
  152. else
  153. {
  154. error_log(date('m-d H:i:s',SYS_TIME).'| POST: illegality notice : flase |'."\r\n", 3, CACHE_PATH.'pay_error_log.php');
  155. return false;
  156. }
  157. } else {
  158. error_log(date('m-d H:i:s',SYS_TIME).'| POST: no post return |'."\r\n", 3, CACHE_PATH.'pay_error_log.php');
  159. return false;
  160. }
  161. }
  162. /**
  163. * 相应服务器应答状态
  164. * @param $result
  165. */
  166. public function response($result) {
  167. if (FALSE == $result) echo 'fail';
  168. else echo 'success';
  169. }
  170. /**
  171. * 返回字符过滤
  172. * @param $parameter
  173. */
  174. private function filterParameter($parameter)
  175. {
  176. $para = array();
  177. foreach ($parameter as $key => $value)
  178. {
  179. if ('sign' == $key || 'sign_type' == $key || '' == $value || 'm' == $key || 'a' == $key || 'c' == $key || 'code' == $key ) continue;
  180. else $para[$key] = $value;
  181. }
  182. return $para;
  183. }
  184. }
  185. ?>