payment.mod.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. define('ALIPAY_GATEWAY', 'https://mapi.alipay.com/gateway.do');
  8. function alipay_build($params, $alipay = array()) {
  9. global $_W;
  10. $tid = $params['uniontid'];
  11. $set = array();
  12. $set['service'] = 'alipay.wap.create.direct.pay.by.user';
  13. $set['partner'] = $alipay['partner'];
  14. $set['_input_charset'] = 'utf-8';
  15. $set['sign_type'] = 'MD5';
  16. $set['notify_url'] = $_W['siteroot'] . 'payment/alipay/notify.php';
  17. $set['return_url'] = $_W['siteroot'] . 'payment/alipay/return.php';
  18. $set['out_trade_no'] = $tid;
  19. $set['subject'] = $params['title'];
  20. $set['total_fee'] = $params['fee'];
  21. $set['seller_id'] = $alipay['account'];
  22. $set['payment_type'] = 1;
  23. $set['body'] = $_W['uniacid'];
  24. if ($params['service'] == 'create_direct_pay_by_user') {
  25. $set['service'] = 'create_direct_pay_by_user';
  26. $set['seller_id'] = $alipay['partner'];
  27. $set['body'] = 'site_store';
  28. } else {
  29. $set['app_pay'] = 'Y';
  30. }
  31. $prepares = array();
  32. foreach($set as $key => $value) {
  33. if($key != 'sign' && $key != 'sign_type') {
  34. $prepares[] = "{$key}={$value}";
  35. }
  36. }
  37. sort($prepares);
  38. $string = implode('&', $prepares);
  39. $string .= $alipay['secret'];
  40. $set['sign'] = md5($string);
  41. $response = ihttp_request(ALIPAY_GATEWAY . '?' . http_build_query($set, '', '&'), array(), array('CURLOPT_FOLLOWLOCATION' => 0));
  42. if (empty($response['headers']['Location'])) {
  43. exit(iconv('gbk', 'utf-8', $response['content']));
  44. return;
  45. }
  46. return array('url' => $response['headers']['Location']);
  47. }
  48. function wechat_proxy_build($params, $wechat) {
  49. global $_W;
  50. $uniacid = !empty($wechat['service']) ? $wechat['service'] : $wechat['borrow'];
  51. $oauth_account = uni_setting($uniacid, array('payment'));
  52. if (intval($wechat['switch']) == '2') {
  53. $_W['uniacid'] = $uniacid;
  54. $wechat['signkey'] = $oauth_account['payment']['wechat']['signkey'];
  55. $wechat['mchid'] = $oauth_account['payment']['wechat']['mchid'];
  56. unset($wechat['sub_mch_id']);
  57. } else {
  58. $wechat['signkey'] = $oauth_account['payment']['wechat_facilitator']['signkey'];
  59. $wechat['mchid'] = $oauth_account['payment']['wechat_facilitator']['mchid'];
  60. }
  61. $acid = pdo_getcolumn('uni_account', array('uniacid' => $uniacid), 'default_acid');
  62. $wechat['appid'] = pdo_getcolumn('account_wechats', array('acid' => $acid), 'key');
  63. $wechat['version'] = 2;
  64. return wechat_build($params, $wechat);
  65. }
  66. function wechat_build($params, $wechat) {
  67. global $_W;
  68. load()->func('communication');
  69. if (empty($wechat['version']) && !empty($wechat['signkey'])) {
  70. $wechat['version'] = 1;
  71. }
  72. $wOpt = array();
  73. if ($wechat['version'] == 1) {
  74. $wOpt['appId'] = $wechat['appid'];
  75. $wOpt['timeStamp'] = strval(TIMESTAMP);
  76. $wOpt['nonceStr'] = random(8);
  77. $package = array();
  78. $package['bank_type'] = 'WX';
  79. $package['body'] = $params['title'];
  80. $package['attach'] = $_W['uniacid'];
  81. $package['partner'] = $wechat['partner'];
  82. $package['out_trade_no'] = $params['uniontid'];
  83. $package['total_fee'] = $params['fee'] * 100;
  84. $package['fee_type'] = '1';
  85. $package['notify_url'] = $_W['siteroot'] . 'payment/wechat/notify.php';
  86. $package['spbill_create_ip'] = CLIENT_IP;
  87. $package['time_start'] = date('YmdHis', TIMESTAMP);
  88. $package['time_expire'] = date('YmdHis', TIMESTAMP + 600);
  89. $package['input_charset'] = 'UTF-8';
  90. if (!empty($wechat['sub_mch_id'])) {
  91. $package['sub_mch_id'] = $wechat['sub_mch_id'];
  92. }
  93. ksort($package);
  94. $string1 = '';
  95. foreach($package as $key => $v) {
  96. if (empty($v)) {
  97. continue;
  98. }
  99. $string1 .= "{$key}={$v}&";
  100. }
  101. $string1 .= "key={$wechat['key']}";
  102. $sign = strtoupper(md5($string1));
  103. $string2 = '';
  104. foreach($package as $key => $v) {
  105. $v = urlencode($v);
  106. $string2 .= "{$key}={$v}&";
  107. }
  108. $string2 .= "sign={$sign}";
  109. $wOpt['package'] = $string2;
  110. $string = '';
  111. $keys = array('appId', 'timeStamp', 'nonceStr', 'package', 'appKey');
  112. sort($keys);
  113. foreach($keys as $key) {
  114. $v = $wOpt[$key];
  115. if($key == 'appKey') {
  116. $v = $wechat['signkey'];
  117. }
  118. $key = strtolower($key);
  119. $string .= "{$key}={$v}&";
  120. }
  121. $string = rtrim($string, '&');
  122. $wOpt['signType'] = 'SHA1';
  123. $wOpt['paySign'] = sha1($string);
  124. return $wOpt;
  125. } else {
  126. if (!empty($params['user']) && is_numeric($params['user'])) {
  127. $params['user'] = mc_uid2openid($params['user']);
  128. }
  129. $package = array();
  130. $package['appid'] = $wechat['appid'];
  131. $package['mch_id'] = $wechat['mchid'];
  132. $package['nonce_str'] = random(8);
  133. $package['body'] = cutstr($params['title'], 26);
  134. $package['attach'] = $_W['uniacid'];
  135. $package['out_trade_no'] = $params['uniontid'];
  136. $package['total_fee'] = $params['fee'] * 100;
  137. $package['spbill_create_ip'] = CLIENT_IP;
  138. $package['time_start'] = date('YmdHis', TIMESTAMP);
  139. $package['time_expire'] = date('YmdHis', TIMESTAMP + 600);
  140. $package['notify_url'] = $_W['siteroot'] . 'payment/wechat/notify.php';
  141. $package['trade_type'] = 'JSAPI';
  142. if ($params['pay_way'] == 'web') {
  143. $package['trade_type'] = 'NATIVE';
  144. $package['product_id'] = $params['goodsid'];
  145. } else {
  146. $package['openid'] = empty($params['user']) ? $_W['fans']['from_user'] : $params['user'];
  147. if (!empty($wechat['sub_mch_id'])) {
  148. $package['sub_mch_id'] = $wechat['sub_mch_id'];
  149. }
  150. if (!empty($params['sub_user'])) {
  151. $package['sub_openid'] = $params['sub_user'];
  152. unset($package['openid']);
  153. }
  154. }
  155. ksort($package, SORT_STRING);
  156. $string1 = '';
  157. foreach($package as $key => $v) {
  158. if (empty($v)) {
  159. continue;
  160. }
  161. $string1 .= "{$key}={$v}&";
  162. }
  163. $string1 .= "key={$wechat['signkey']}";
  164. $package['sign'] = strtoupper(md5($string1));
  165. $dat = array2xml($package);
  166. $response = ihttp_request('https://api.mch.weixin.qq.com/pay/unifiedorder', $dat);
  167. if (is_error($response)) {
  168. return $response;
  169. }
  170. $xml = @isimplexml_load_string($response['content'], 'SimpleXMLElement', LIBXML_NOCDATA);
  171. if (strval($xml->return_code) == 'FAIL') {
  172. return error(-1, strval($xml->return_msg));
  173. }
  174. if (strval($xml->result_code) == 'FAIL') {
  175. return error(-1, strval($xml->err_code).': '.strval($xml->err_code_des));
  176. }
  177. $prepayid = $xml->prepay_id;
  178. $wOpt['appId'] = $wechat['appid'];
  179. $wOpt['timeStamp'] = strval(TIMESTAMP);
  180. $wOpt['nonceStr'] = random(8);
  181. $wOpt['package'] = 'prepay_id='.$prepayid;
  182. $wOpt['signType'] = 'MD5';
  183. if ($xml->trade_type == 'NATIVE') {
  184. $code_url = $xml->code_url;
  185. $wOpt['code_url'] = strval($code_url);
  186. }
  187. ksort($wOpt, SORT_STRING);
  188. foreach($wOpt as $key => $v) {
  189. $string .= "{$key}={$v}&";
  190. }
  191. $string .= "key={$wechat['signkey']}";
  192. $wOpt['paySign'] = strtoupper(md5($string));
  193. return $wOpt;
  194. }
  195. }
  196. function payment_proxy_pay_account() {
  197. global $_W;
  198. $setting = uni_setting($_W['uniacid'], array('payment'));
  199. $setting['payment']['wechat']['switch'] = intval($setting['payment']['wechat']['switch']);
  200. if ($setting['payment']['wechat']['switch'] == PAYMENT_WECHAT_TYPE_SERVICE) {
  201. $uniacid = intval($setting['payment']['wechat']['service']);
  202. } elseif ($setting['payment']['wechat']['switch'] == PAYMENT_WECHAT_TYPE_BORROW) {
  203. $uniacid = intval($setting['payment']['wechat']['borrow']);
  204. } else {
  205. $uniacid = 0;
  206. }
  207. $pay_account = uni_fetch($uniacid);
  208. if (empty($uniacid) || empty($pay_account)) {
  209. return error(1);
  210. }
  211. return WeAccount::create($pay_account);
  212. }