payment.mod.php 12 KB

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