人人商城

payment.mod.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. }
  213. function payment_types($type = '') {
  214. $pay_types= array(
  215. 'delivery' => '货到支付',
  216. 'credit' => '余额支付',
  217. 'mix' => '混合支付',
  218. 'alipay' =>'支付宝支付',
  219. 'wechat' => '微信支付',
  220. 'wechat_facilitator' => '服务商支付',
  221. 'unionpay' => '银联支付',
  222. 'baifubao' => '百度钱包支付',
  223. 'line' => '汇款支付',
  224. 'jueqiymf' => '一码支付'
  225. );
  226. return !empty($pay_types[$type]) ? $pay_types[$type] : $pay_types;
  227. }
  228. function payment_setting() {
  229. global $_W;
  230. $setting = uni_setting_load('payment', $_W['uniacid']);
  231. $pay_setting = is_array($setting['payment']) ? $setting['payment'] : array();
  232. if (empty($pay_setting['delivery'])) {
  233. $pay_setting['delivery'] = array(
  234. 'recharge_switch' => false,
  235. 'pay_switch' => false,
  236. );
  237. }
  238. if (empty($pay_setting['mix'])) {
  239. $pay_setting['mix'] = array(
  240. 'recharge_switch' => false,
  241. 'pay_switch' => false,
  242. );
  243. }
  244. if (empty($pay_setting['credit'])) {
  245. $pay_setting['credit'] = array(
  246. 'recharge_switch' => false,
  247. 'pay_switch' => false,
  248. );
  249. }
  250. if (empty($pay_setting['alipay'])) {
  251. $pay_setting['alipay'] = array(
  252. 'recharge_switch' => false,
  253. 'pay_switch' => false,
  254. 'partner' => '',
  255. 'secret' => '',
  256. );
  257. }
  258. if (empty($pay_setting['wechat'])) {
  259. $pay_setting['wechat'] = array(
  260. 'recharge_switch' => false,
  261. 'pay_switch' => false,
  262. 'switch' => false,
  263. );
  264. } else {
  265. if (!in_array($pay_setting['wechat']['switch'], array('1'))) {
  266. unset($pay_setting['wechat']['signkey']);
  267. }
  268. }
  269. if (empty($pay_setting['unionpay'])) {
  270. $pay_setting['unionpay'] = array(
  271. 'recharge_switch' => false,
  272. 'pay_switch' => false,
  273. 'merid' => '',
  274. 'signcertpwd' => '',
  275. );
  276. }
  277. if (empty($pay_setting['baifubao'])) {
  278. $pay_setting['baifubao'] = array(
  279. 'recharge_switch' => false,
  280. 'pay_switch' => false,
  281. 'mchid' => '',
  282. 'signkey' => '',
  283. );
  284. }
  285. if (empty($pay_setting['line'])) {
  286. $pay_setting['line'] = array(
  287. 'recharge_switch' => false,
  288. 'pay_switch' => false,
  289. 'message' => '',
  290. );
  291. }
  292. if (empty($pay_setting['jueqiymf'])) {
  293. $pay_setting['jueqiymf'] = array(
  294. 'recharge_switch' => false,
  295. 'pay_switch' => false,
  296. 'url' => '',
  297. 'mchid' => '',
  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_'.$_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. $value['has_config'] = true;
  331. $value['recharge_set'] = true;
  332. $value['support_set'] = true;
  333. if (in_array($type, $no_recharge_types)) {
  334. $value['recharge_set'] = false;
  335. }
  336. if (!empty($value['pay_switch']) || !empty($value['recharge_switch'])) {
  337. $value['support_set'] = false;
  338. }
  339. foreach ($value as $key => $val) {
  340. if (!in_array($key, $has_config_keys) && empty($val)) {
  341. $value['has_config'] = false;
  342. continue;
  343. }
  344. }
  345. }
  346. unset($value);
  347. return $pay_setting;
  348. }