payment.mod.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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. } else {
  28. $set['app_pay'] = 'Y';
  29. }
  30. $prepares = array();
  31. foreach ($set as $key => $value) {
  32. if ($key != 'sign' && $key != 'sign_type') {
  33. $prepares[] = "{$key}={$value}";
  34. }
  35. }
  36. sort($prepares);
  37. $string = implode('&', $prepares);
  38. $string .= $alipay['secret'];
  39. $set['sign'] = md5($string);
  40. $response = ihttp_request(ALIPAY_GATEWAY . '?' . http_build_query($set, '', '&'), array(), array('CURLOPT_FOLLOWLOCATION' => 0));
  41. if (empty($response['headers']['Location']) && empty($_W['isajax'])) {
  42. exit(iconv('gbk', 'utf-8', $response['content']));
  43. return;
  44. }
  45. return array('url' => $response['headers']['Location']);
  46. }
  47. function wechat_proxy_build($params, $wechat) {
  48. global $_W;
  49. $uniacid = !empty($wechat['service']) ? $wechat['service'] : $wechat['borrow'];
  50. $oauth_account = uni_setting($uniacid, array('payment'));
  51. if (intval($wechat['switch']) == '2') {
  52. $_W['uniacid'] = $uniacid;
  53. $wechat['signkey'] = $oauth_account['payment']['wechat']['signkey'];
  54. $wechat['mchid'] = $oauth_account['payment']['wechat']['mchid'];
  55. unset($wechat['sub_mch_id']);
  56. } else {
  57. $wechat['signkey'] = $oauth_account['payment']['wechat_facilitator']['signkey'];
  58. $wechat['mchid'] = $oauth_account['payment']['wechat_facilitator']['mchid'];
  59. }
  60. $wechat['appid'] = pdo_getcolumn('account_wechats', array('uniacid' => $uniacid), 'key');
  61. $wechat['version'] = 2;
  62. return wechat_build($params, $wechat);
  63. }
  64. function wechat_build($params, $wechat) {
  65. global $_W;
  66. load()->func('communication');
  67. if (empty($wechat['version']) && !empty($wechat['signkey'])) {
  68. $wechat['version'] = 1;
  69. }
  70. $wOpt = array();
  71. if ($wechat['version'] == 1) {
  72. $wOpt['appId'] = $wechat['appid'];
  73. $wOpt['timeStamp'] = strval(TIMESTAMP);
  74. $wOpt['nonceStr'] = random(8);
  75. $package = array();
  76. $package['bank_type'] = 'WX';
  77. $package['body'] = $params['title'];
  78. $package['attach'] = $_W['uniacid'];
  79. $package['partner'] = $wechat['partner'];
  80. $package['out_trade_no'] = $params['uniontid'];
  81. $package['total_fee'] = $params['fee'] * 100;
  82. $package['fee_type'] = '1';
  83. $package['notify_url'] = $_W['siteroot'] . 'payment/wechat/notify.php';
  84. $package['spbill_create_ip'] = $_W['clientip'];
  85. $package['time_start'] = date('YmdHis', TIMESTAMP);
  86. $package['time_expire'] = date('YmdHis', TIMESTAMP + 600);
  87. $package['input_charset'] = 'UTF-8';
  88. if (!empty($wechat['sub_mch_id'])) {
  89. $package['sub_mch_id'] = $wechat['sub_mch_id'];
  90. }
  91. if (!empty($params['goods_tag'])) {
  92. $package['goods_tag'] = $params['goods_tag'];
  93. }
  94. ksort($package);
  95. $string1 = '';
  96. foreach ($package as $key => $v) {
  97. if (empty($v)) {
  98. unset($package[$key]);
  99. continue;
  100. }
  101. $string1 .= "{$key}={$v}&";
  102. }
  103. $string1 .= "key={$wechat['key']}";
  104. $sign = strtoupper(md5($string1));
  105. $string2 = '';
  106. foreach ($package as $key => $v) {
  107. $v = urlencode($v);
  108. $string2 .= "{$key}={$v}&";
  109. }
  110. $string2 .= "sign={$sign}";
  111. $wOpt['package'] = $string2;
  112. $string = '';
  113. $keys = array('appId', 'timeStamp', 'nonceStr', 'package', 'appKey');
  114. sort($keys);
  115. foreach ($keys as $key) {
  116. $v = $wOpt[$key];
  117. if ($key == 'appKey') {
  118. $v = $wechat['signkey'];
  119. }
  120. $key = strtolower($key);
  121. $string .= "{$key}={$v}&";
  122. }
  123. $string = rtrim($string, '&');
  124. $wOpt['signType'] = 'SHA1';
  125. $wOpt['paySign'] = sha1($string);
  126. return $wOpt;
  127. } else {
  128. if (!empty($params['user']) && is_numeric($params['user'])) {
  129. $params['user'] = mc_uid2openid($params['user']);
  130. }
  131. $package = array();
  132. $package['appid'] = $wechat['appid'];
  133. $package['mch_id'] = $wechat['mchid'];
  134. $package['nonce_str'] = random(8);
  135. $package['body'] = cutstr($params['title'], 26);
  136. $package['attach'] = $_W['uniacid'];
  137. $package['out_trade_no'] = $params['uniontid'];
  138. $package['total_fee'] = $params['fee'] * 100;
  139. $package['spbill_create_ip'] = $_W['clientip'];
  140. $package['time_start'] = date('YmdHis', TIMESTAMP);
  141. $package['time_expire'] = date('YmdHis', TIMESTAMP + 600);
  142. $package['notify_url'] = $_W['siteroot'] . 'payment/wechat/notify.php';
  143. $package['trade_type'] = 'JSAPI';
  144. if ($params['pay_way'] == 'web') {
  145. $package['trade_type'] = 'NATIVE';
  146. $package['product_id'] = $params['goodsid'];
  147. } else {
  148. $package['openid'] = empty($params['user']) ? $_W['fans']['from_user'] : $params['user'];
  149. if (!empty($wechat['sub_appid'])) {
  150. $package['sub_appid'] = $wechat['sub_appid'];
  151. }
  152. if (!empty($wechat['sub_mch_id'])) {
  153. $package['sub_mch_id'] = $wechat['sub_mch_id'];
  154. }
  155. if (!empty($params['sub_user'])) {
  156. $package['sub_openid'] = $params['sub_user'];
  157. unset($package['openid']);
  158. }
  159. }
  160. if (!empty($params['goods_tag'])) {
  161. $package['goods_tag'] = $params['goods_tag'];
  162. }
  163. ksort($package, SORT_STRING);
  164. $string1 = '';
  165. foreach ($package as $key => $v) {
  166. if (empty($v)) {
  167. unset($package[$key]);
  168. continue;
  169. }
  170. $string1 .= "{$key}={$v}&";
  171. }
  172. $string1 .= "key={$wechat['signkey']}";
  173. $package['sign'] = strtoupper(md5($string1));
  174. $dat = array2xml($package);
  175. $response = ihttp_request('https://api.mch.weixin.qq.com/pay/unifiedorder', $dat);
  176. if (is_error($response)) {
  177. return $response;
  178. }
  179. $xml = @isimplexml_load_string($response['content'], 'SimpleXMLElement', LIBXML_NOCDATA);
  180. if (strval($xml->return_code) == 'FAIL') {
  181. return error(-1, strval($xml->return_msg));
  182. }
  183. if (strval($xml->result_code) == 'FAIL') {
  184. return error(-1, strval($xml->err_code) . ': ' . strval($xml->err_code_des));
  185. }
  186. $prepayid = $xml->prepay_id;
  187. $wOpt['appId'] = isset($wechat['sub_appid']) ? $wechat['sub_appid'] : $wechat['appid'];
  188. $wOpt['timeStamp'] = strval(TIMESTAMP);
  189. $wOpt['nonceStr'] = random(8);
  190. $wOpt['package'] = 'prepay_id=' . $prepayid;
  191. $wOpt['signType'] = 'MD5';
  192. if ($xml->trade_type == 'NATIVE') {
  193. $code_url = $xml->code_url;
  194. $wOpt['code_url'] = strval($code_url);
  195. }
  196. ksort($wOpt, SORT_STRING);
  197. foreach ($wOpt as $key => $v) {
  198. $string .= "{$key}={$v}&";
  199. }
  200. $string .= "key={$wechat['signkey']}";
  201. $wOpt['paySign'] = strtoupper(md5($string));
  202. return $wOpt;
  203. }
  204. }
  205. function payment_proxy_pay_account() {
  206. global $_W;
  207. $setting = uni_setting($_W['uniacid'], array('payment'));
  208. $setting['payment']['wechat']['switch'] = intval($setting['payment']['wechat']['switch']);
  209. if ($setting['payment']['wechat']['switch'] == PAYMENT_WECHAT_TYPE_SERVICE) {
  210. $uniacid = intval($setting['payment']['wechat']['service']);
  211. } elseif ($setting['payment']['wechat']['switch'] == PAYMENT_WECHAT_TYPE_BORROW) {
  212. $uniacid = intval($setting['payment']['wechat']['borrow']);
  213. } else {
  214. $uniacid = 0;
  215. }
  216. $pay_account = uni_fetch($uniacid);
  217. if (empty($uniacid) || empty($pay_account)) {
  218. return error(1);
  219. }
  220. return WeAccount::createByUniacid($uniacid);
  221. }
  222. function payment_types($type = '') {
  223. $pay_types = array(
  224. 'delivery' => '货到支付',
  225. 'credit' => '余额支付',
  226. 'mix' => '混合支付',
  227. 'alipay' => '支付宝支付',
  228. 'wechat' => '微信支付',
  229. 'wechat_facilitator' => '服务商支付',
  230. 'unionpay' => '银联支付',
  231. 'baifubao' => '百度钱包支付',
  232. 'line' => '汇款支付',
  233. );
  234. return !empty($pay_types[$type]) ? $pay_types[$type] : $pay_types;
  235. }
  236. function payment_setting() {
  237. global $_W;
  238. $setting = uni_setting_load('payment', $_W['uniacid']);
  239. $pay_setting = is_array($setting['payment']) ? $setting['payment'] : array();
  240. if (empty($pay_setting['delivery'])) {
  241. $pay_setting['delivery'] = array(
  242. 'recharge_switch' => false,
  243. 'pay_switch' => false,
  244. );
  245. }
  246. if (empty($pay_setting['mix'])) {
  247. $pay_setting['mix'] = array(
  248. 'recharge_switch' => false,
  249. 'pay_switch' => false,
  250. );
  251. }
  252. if (empty($pay_setting['credit'])) {
  253. $pay_setting['credit'] = array(
  254. 'recharge_switch' => false,
  255. 'pay_switch' => false,
  256. );
  257. }
  258. if (empty($pay_setting['alipay'])) {
  259. $pay_setting['alipay'] = array(
  260. 'recharge_switch' => false,
  261. 'pay_switch' => false,
  262. 'partner' => '',
  263. 'secret' => '',
  264. );
  265. }
  266. if (empty($pay_setting['wechat'])) {
  267. $pay_setting['wechat'] = array(
  268. 'recharge_switch' => false,
  269. 'pay_switch' => false,
  270. 'switch' => false,
  271. );
  272. } else {
  273. if (!in_array($pay_setting['wechat']['switch'], array('1'))) {
  274. unset($pay_setting['wechat']['signkey']);
  275. }
  276. }
  277. if (empty($pay_setting['unionpay'])) {
  278. $pay_setting['unionpay'] = array(
  279. 'recharge_switch' => false,
  280. 'pay_switch' => false,
  281. 'merid' => '',
  282. 'signcertpwd' => '',
  283. );
  284. }
  285. if (empty($pay_setting['baifubao'])) {
  286. $pay_setting['baifubao'] = array(
  287. 'recharge_switch' => false,
  288. 'pay_switch' => false,
  289. 'mchid' => '',
  290. 'signkey' => '',
  291. );
  292. }
  293. if (empty($pay_setting['line'])) {
  294. $pay_setting['line'] = array(
  295. 'recharge_switch' => false,
  296. 'pay_switch' => false,
  297. 'message' => '',
  298. );
  299. }
  300. if (empty($pay_setting['wechat_facilitator'])) {
  301. $pay_setting['wechat_facilitator'] = array(
  302. 'switch' => false,
  303. 'mchid' => '',
  304. 'signkey' => '',
  305. );
  306. }
  307. if (empty($_W['isfounder'])) {
  308. $user_account_list = pdo_getall('uni_account_users', array('uid' => $_W['uid']), array(), 'uniacid');
  309. $param['uniacid'] = array_keys($user_account_list);
  310. }
  311. $pay_setting['unionpay']['signcertexists'] = file_exists(IA_ROOT . '/attachment/unionpay/PM_' . md5(complex_authkey() . $_W['uniacid']) . '_acp.pfx');
  312. $no_recharge_types = array('delivery', 'credit', 'mix', 'line');
  313. $has_config_keys = array('pay_switch', 'recharge_switch', 'has_config', 'recharge_set', 'signcertexists', 'support_set');
  314. if ($pay_setting['wechat']['switch'] == 1) {
  315. if ($pay_setting['wechat']['version'] == 1) {
  316. unset($pay_setting['wechat']['mchid'], $pay_setting['wechat']['apikey']);
  317. } elseif ($pay_setting['wechat']['version'] == 2) {
  318. unset($pay_setting['wechat']['partner'], $pay_setting['wechat']['key'], $pay_setting['wechat']['signkey']);
  319. }
  320. unset($pay_setting['wechat']['borrow'], $pay_setting['wechat']['sub_mch_id'], $pay_setting['wechat']['service']);
  321. } elseif ($pay_setting['wechat']['switch'] == 2) {
  322. unset($pay_setting['wechat']['mchid'], $pay_setting['wechat']['apikey'], $pay_setting['wechat']['partner'], $pay_setting['wechat']['key'], $pay_setting['wechat']['signkey'], $pay_setting['wechat']['sub_mch_id'], $pay_setting['wechat']['service']);
  323. } elseif ($pay_setting['wechat']['switch'] == 3) {
  324. unset($pay_setting['wechat']['mchid'], $pay_setting['wechat']['apikey'], $pay_setting['wechat']['partner'], $pay_setting['wechat']['key'], $pay_setting['wechat']['signkey'], $pay_setting['wechat']['borrow']);
  325. }
  326. foreach ($pay_setting as $type => &$value) {
  327. if (empty($value) || !is_array($value)) {
  328. continue;
  329. }
  330. if (isset($value['recharge_switch'])) {
  331. $value['recharge_switch'] = false == $value['recharge_switch'] ? false : true;
  332. }
  333. if (isset($value['pay_switch'])) {
  334. $value['pay_switch'] = false == $value['pay_switch'] ? false : true;
  335. }
  336. $value['has_config'] = true;
  337. $value['recharge_set'] = true;
  338. $value['support_set'] = true;
  339. if (in_array($type, $no_recharge_types)) {
  340. $value['recharge_set'] = false;
  341. }
  342. if (!empty($value['pay_switch']) || !empty($value['recharge_switch'])) {
  343. $value['support_set'] = false;
  344. }
  345. foreach ($value as $key => $val) {
  346. if (!in_array($key, $has_config_keys) && empty($val)) {
  347. $value['has_config'] = false;
  348. continue;
  349. }
  350. }
  351. }
  352. unset($value);
  353. return $pay_setting;
  354. }