cash.ctrl.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  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. load()->model('activity');
  8. load()->model('module');
  9. load()->model('payment');
  10. load()->func('communication');
  11. if ($do == 'check_password') {
  12. $password = safe_gpc_string($_GPC['password']);
  13. $user_info = mc_fetch($_W['member']['uid']);
  14. $password = md5($password . $user_info['salt']);
  15. if ($password == $user_info['pay_password']) {
  16. message(0, '', 'ajax');
  17. } else {
  18. message(1, '', 'ajax');
  19. }
  20. }
  21. $moduels = uni_modules();
  22. $params = @json_decode(base64_decode($_GPC['params']), true);
  23. if(empty($params) || !array_key_exists($params['module'], $moduels)) {
  24. message('访问错误.');
  25. }
  26. $setting = uni_setting($_W['uniacid'], 'payment');
  27. $dos = array();
  28. if(!empty($setting['payment']['credit']['switch'])) {
  29. $dos[] = 'credit';
  30. }
  31. if(!empty($setting['payment']['alipay']['switch'])) {
  32. $dos[] = 'alipay';
  33. }
  34. if(!empty($setting['payment']['wechat']['switch'])) {
  35. $dos[] = 'wechat';
  36. }
  37. if(!empty($setting['payment']['delivery']['switch'])) {
  38. $dos[] = 'delivery';
  39. }
  40. if(!empty($setting['payment']['unionpay']['switch'])) {
  41. $dos[] = 'unionpay';
  42. }
  43. if(!empty($setting['payment']['baifubao']['switch'])) {
  44. $dos[] = 'baifubao';
  45. }
  46. if(!empty($setting['payment']['jueqiymf']['switch'])) {
  47. $dos[] = 'jueqiymf';
  48. }
  49. if(!empty($setting['payment']['mix']['switch'])) {
  50. $dos[] = 'mix';
  51. }
  52. $do = $_GPC['do'];
  53. $type = in_array($do, $dos) ? $do : '';
  54. if(empty($type)) {
  55. message('支付方式错误,请联系商家', '', 'error');
  56. }
  57. if(!empty($type)) {
  58. $log = pdo_get('core_paylog', array('uniacid' => $_W['uniacid'], 'module' => $params['module'], 'tid' => $params['tid']));
  59. if(!empty($log) && ($type != 'credit' && !empty($_GPC['notify'])) && $log['status'] != '0') {
  60. message('这个订单已经支付成功, 不需要重复支付.');
  61. }
  62. $update_card_log = array(
  63. 'is_usecard' => '0',
  64. 'card_type' => '0',
  65. 'card_id' => '0',
  66. 'card_fee' => $log['fee'],
  67. 'type' => $type,
  68. );
  69. pdo_update('core_paylog', $update_card_log, array('plid' => $log['plid']));
  70. $log['is_usecard'] = '0';
  71. $log['card_type'] = '0';
  72. $log['card_id'] = '0';
  73. $log['card_fee'] = $log['fee'];
  74. $moduleid = pdo_fetchcolumn("SELECT mid FROM ".tablename('modules')." WHERE name = :name", array(':name' => $params['module']));
  75. $moduleid = empty($moduleid) ? '000000' : sprintf("%06d", $moduleid);
  76. $record = array();
  77. $record['type'] = $type;
  78. if (empty($log['uniontid'])) {
  79. $record['uniontid'] = $log['uniontid'] = date('YmdHis').$moduleid.random(8,1);
  80. }
  81. if($type != 'delivery') {
  82. if ($_GPC['mix_pay']) {
  83. $setting = uni_setting($_W['uniacid'], array('creditbehaviors'));
  84. $credtis = mc_credit_fetch($_W['member']['uid']);
  85. if ($credtis[$setting['creditbehaviors']['currency']] > 0 && in_array('mix', $dos) && $credtis[$setting['creditbehaviors']['currency']] < $log['card_fee']) {
  86. $mix_credit_log = $log;
  87. unset($mix_credit_log['plid']);
  88. $mix_credit_log['uniontid'] = date('YmdHis') . $moduleid . random(8,1);
  89. $mix_credit_log['type'] = 'credit';
  90. $mix_credit_log['fee'] = $credtis[$setting['creditbehaviors']['currency']];
  91. $mix_credit_log['card_fee'] = $credtis[$setting['creditbehaviors']['currency']];
  92. pdo_update('core_paylog', array('fee' => $log['card_fee'] - $credtis[$setting['creditbehaviors']['currency']], 'card_fee' => $log['card_fee'] - $credtis[$setting['creditbehaviors']['currency']]), array('plid' => $log['plid']));
  93. pdo_insert('core_paylog', $mix_credit_log);
  94. }
  95. }
  96. $we7_coupon_info = module_fetch('we7_coupon');
  97. if (!empty($we7_coupon_info)) {
  98. $coupon_id = intval($_GPC['coupon_id']);
  99. $coupon_info = pdo_get('coupon', array('uniacid' => $_W['uniacid'], 'id' => $coupon_id));
  100. $coupon_info['fee'] = $log['card_fee'];
  101. if (!empty($coupon_info)) {
  102. $extra = iunserializer($coupon_info['extra']);
  103. if($coupon_info['type'] == COUPON_TYPE_DISCOUNT) {
  104. $coupon_info['fee'] = sprintf("%.2f", ($log['fee'] * ($extra['discount'] / 100)));
  105. } elseif ($coupon_info['type'] == COUPON_TYPE_CASH) {
  106. if($log['fee'] >= $extra['least_cost'] * 0.01) {
  107. $coupon_info['fee'] = sprintf("%.2f", ($log['fee'] - $extra['reduce_cost'] / 100));
  108. }
  109. }
  110. if (!empty($_GPC['code']) && !empty($_GPC['coupon_id'])) {
  111. $record['is_usecard'] = 1;
  112. $record['card_fee'] = $coupon_info['fee'];
  113. $record['encrypt_code'] = trim($_GPC['code']);
  114. activity_coupon_type_init();
  115. if (COUPON_TYPE == WECHAT_COUPON) {
  116. $record['card_type'] = 1;
  117. $record['card_id'] = $coupon_info['id'];
  118. } else {
  119. $record['card_type'] = 2;
  120. $record['card_id'] = $coupon_info['id'];
  121. }
  122. }
  123. }
  124. }
  125. }
  126. if (empty($log)) {
  127. message('系统支付错误, 请稍后重试.');
  128. } else {
  129. pdo_update('core_paylog', $record, array('plid' => $log['plid']));
  130. if (!empty($log['uniontid']) && $record['card_fee']) {
  131. $log['card_fee'] = $record['card_fee'];
  132. $log['card_id'] = $record['card_id'];
  133. $log['card_type'] = $record['card_type'];
  134. $log['is_usecard'] = $record['is_usecard'];
  135. }
  136. }
  137. $ps = array(
  138. 'tid' => $log['plid'],
  139. 'uniontid' => $log['uniontid'],
  140. 'user' => $_W['openid'],
  141. 'fee' => $log['card_fee'],
  142. 'title' => $params['title'],
  143. );
  144. if ($type == 'alipay') {
  145. if (!empty($log['plid'])) {
  146. pdo_update('core_paylog', array('openid' => $_W['member']['uid']), array('plid' => $log['plid']));
  147. }
  148. $ret = alipay_build($ps, $setting['payment']['alipay']);
  149. if($ret['url']) {
  150. echo '<script type="text/javascript" src="../payment/alipay/ap.js"></script><script type="text/javascript">_AP.pay("'.$ret['url'].'")</script>';
  151. exit();
  152. }
  153. }
  154. if ($type == 'wechat') {
  155. if(!empty($log['plid'])) {
  156. $tag = array();
  157. $tag['acid'] = $_W['acid'];
  158. $tag['uid'] = $_W['member']['uid'];
  159. pdo_update('core_paylog', array('openid' => $_W['openid'], 'tag' => iserializer($tag)), array('plid' => $log['plid']));
  160. }
  161. $ps['title'] = urlencode($params['title']);
  162. $sl = base64_encode(json_encode($ps));
  163. $auth = sha1($sl . $_W['uniacid'] . $_W['config']['setting']['authkey']);
  164. $callback = $_W['siteroot'] . "payment/wechat/pay.php?i={$_W['uniacid']}&auth={$auth}&ps={$sl}";
  165. $global_unisetting = uni_account_global_oauth();
  166. $unisetting['oauth']['host'] = !empty($unisetting['oauth']['host']) ? $unisetting['oauth']['host'] : $global_unisetting['oauth']['host'];
  167. if (!empty($unisetting['oauth']['host'])) {
  168. $callback = str_replace($_W['siteroot'], $unisetting['oauth']['host'].'/', $callback);
  169. }
  170. $proxy_pay_account = payment_proxy_pay_account();
  171. if (!is_error($proxy_pay_account)) {
  172. $forward = $proxy_pay_account->getOauthCodeUrl(urlencode($callback), 'we7sid-'.$_W['session_id']);
  173. header('Location: ' . $forward);
  174. exit;
  175. }
  176. header("Location: $callback");
  177. exit();
  178. }
  179. if ($type == 'jueqiymf') {
  180. $sl = base64_encode(json_encode($ps));
  181. $auth = sha1($sl . $_W['uniacid'] . $_W['config']['setting']['authkey']);
  182. header("location: ../payment/jueqiymf/pay.php?i={$_W['uniacid']}&auth={$auth}&ps={$sl}");
  183. exit();
  184. }
  185. if($type == 'credit') {
  186. $we7_coupon_info = module_fetch('we7_coupon');
  187. $setting = uni_setting($_W['uniacid'], array('creditbehaviors'));
  188. $credtis = mc_credit_fetch($_W['member']['uid']);
  189. $sql = 'SELECT * FROM ' . tablename('core_paylog') . ' WHERE `plid`=:plid';
  190. $pars = array();
  191. $pars[':plid'] = $ps['tid'];
  192. $log = pdo_fetch($sql, $pars);
  193. if($log['module'] == 'recharge') {
  194. message('不能使用余额支付', referer(), 'error');
  195. }
  196. if (!is_numeric($log['openid'])) {
  197. $uid = mc_openid2uid($log['openid']);
  198. if (empty($uid)) {
  199. $fans_info = mc_init_fans_info($log['openid']);
  200. $uid = $fans_info['uid'];
  201. }
  202. $log['openid'] = $uid;
  203. }
  204. if(empty($_GPC['notify'])) {
  205. if(!empty($log) && $log['status'] == '0') {
  206. if($credtis[$setting['creditbehaviors']['currency']] < $ps['fee']) {
  207. message("余额不足以支付, 需要 {$ps['fee']}, 当前 {$credtis[$setting['creditbehaviors']['currency']]}");
  208. }
  209. if (!empty($we7_coupon_info) && $log['is_usecard'] == 1 && !empty($log['encrypt_code'])) {
  210. $coupon_info = pdo_get('coupon', array('id' => $log['card_id']), array('id'));
  211. $coupon_record = pdo_get('coupon_record', array('couponid' => $log['card_id'], 'openid' => $_W['openid'], 'code' => $log['encrypt_code'], 'status' => '1'));
  212. $status = activity_coupon_use($coupon_info['id'], $coupon_record['id'], $params['module']);
  213. }
  214. $fee = floatval($ps['fee']);
  215. if (!empty($we7_coupon_info)) {
  216. load()->model('mc');
  217. $store_id = 0;
  218. if ($log['module'] == 'we7_coupon') {
  219. $paycenter_order = pdo_get('paycenter_order', array('id' => $log['tid']), array('store_id'));
  220. $store_id = $paycenter_order['store_id'];
  221. }
  222. $is_grant_credit = mc_card_grant_credit($log['openid'], $fee, $store_id, $log['module']);
  223. $result = mc_credit_update($log['openid'], 'credit2', -$fee, array(0, $tip, $log['module'], 0, $store_id, 3));
  224. } else {
  225. $result = mc_credit_update($_W['member']['uid'], $setting['creditbehaviors']['currency'], -$fee, array($_W['member']['uid'], '消费' . $setting['creditbehaviors']['currency'] . ':' . $fee));
  226. }
  227. if (is_error($result)) {
  228. message($result['message'], '', 'error');
  229. }
  230. pdo_update('core_paylog', array('status' => '1'), array('plid' => $log['plid']));
  231. if (!empty($_W['openid'])) {
  232. if (is_error($is_grant_credit)) {
  233. $grant_credit_nums = 0;
  234. } else {
  235. $grant_credit_nums = $is_grant_credit['message'];
  236. }
  237. mc_notice_credit2($_W['openid'], $_W['member']['uid'], $fee, $grant_credit_nums, '线上消费');
  238. }
  239. $site = WeUtility::createModuleSite($log['module']);
  240. if(!is_error($site)) {
  241. $site->weid = $_W['weid'];
  242. $site->uniacid = $_W['uniacid'];
  243. $site->inMobile = true;
  244. $method = 'payResult';
  245. if (method_exists($site, $method)) {
  246. $ret = array();
  247. $ret['result'] = 'success';
  248. $ret['type'] = $log['type'];
  249. $ret['from'] = 'return';
  250. $ret['tid'] = $log['tid'];
  251. $ret['user'] = $log['openid'];
  252. $ret['fee'] = $log['fee'];
  253. $ret['weid'] = $log['weid'];
  254. $ret['uniacid'] = $log['uniacid'];
  255. $ret['acid'] = $log['acid'];
  256. $ret['is_usecard'] = $log['is_usecard'];
  257. $ret['card_type'] = $log['card_type']; $ret['card_fee'] = $log['card_fee'];
  258. $ret['card_id'] = $log['card_id'];
  259. echo '<iframe style="display:none;" src="'.murl('mc/cash/credit', array('notify' => 'yes', 'params' => $_GPC['params'], 'code' => $_GPC['code'], 'coupon_id' => $_GPC['coupon_id']), true, true).'"></iframe>';
  260. $site->$method($ret);
  261. }
  262. }
  263. }
  264. } else {
  265. $site = WeUtility::createModuleSite($log['module']);
  266. if(!is_error($site)) {
  267. $site->weid = $_W['weid'];
  268. $site->uniacid = $_W['uniacid'];
  269. $site->inMobile = true;
  270. $method = 'payResult';
  271. if (method_exists($site, $method)) {
  272. $ret = array();
  273. $ret['result'] = 'success';
  274. $ret['type'] = $log['type'];
  275. $ret['from'] = 'notify';
  276. $ret['tid'] = $log['tid'];
  277. $ret['user'] = $log['openid'];
  278. $ret['fee'] = $log['fee'];
  279. $ret['weid'] = $log['weid'];
  280. $ret['uniacid'] = $log['uniacid'];
  281. $ret['acid'] = $log['acid'];
  282. $ret['is_usecard'] = $log['is_usecard'];
  283. $ret['card_type'] = $log['card_type']; $ret['card_fee'] = $log['card_fee'];
  284. $ret['card_id'] = $log['card_id'];
  285. $site->$method($ret);
  286. }
  287. }
  288. }
  289. }
  290. if ($type == 'delivery') {
  291. $we7_coupon_info = module_fetch('we7_coupon');
  292. $sql = 'SELECT * FROM ' . tablename('core_paylog') . ' WHERE `plid`=:plid';
  293. $pars = array();
  294. $pars[':plid'] = $ps['tid'];
  295. $log = pdo_fetch($sql, $pars);
  296. if(!empty($log) && $log['status'] == '0') {
  297. if (!empty($we7_coupon_info) && $log['is_usecard'] == 1) {
  298. $coupon_info = pdo_get('coupon', array('id' => $log['card_id']), array('id'));
  299. $coupon_record = pdo_get('coupon_record', array('couponid' => $log['card_id'], 'openid' => $_W['openid'], 'code' => $log['encrypt_code'], 'status' => '1'));
  300. $status = activity_coupon_use($coupon_info['id'], $coupon_record['id'], $params['module']);
  301. if (is_error($status)) {
  302. message($status['message']);
  303. }
  304. }
  305. $site = WeUtility::createModuleSite($log['module']);
  306. if(!is_error($site)) {
  307. $site->weid = $_W['weid'];
  308. $site->uniacid = $_W['uniacid'];
  309. $site->inMobile = true;
  310. $method = 'payResult';
  311. if (method_exists($site, $method)) {
  312. $ret = array();
  313. $ret['result'] = 'failed';
  314. $ret['type'] = $log['type'];
  315. $ret['from'] = 'return';
  316. $ret['tid'] = $log['tid'];
  317. $ret['user'] = $log['openid'];
  318. $ret['fee'] = $log['fee']; $ret['weid'] = $log['weid'];
  319. $ret['uniacid'] = $log['uniacid'];
  320. $ret['is_usecard'] = $log['is_usecard'];
  321. $ret['card_type'] = $log['card_type']; $ret['card_fee'] = $log['card_fee'];
  322. $ret['card_id'] = $log['card_id'];
  323. exit($site->$method($ret));
  324. }
  325. }
  326. }
  327. }
  328. if ($type == 'unionpay') {
  329. $sl = base64_encode(json_encode($ps));
  330. $auth = sha1($sl . $_W['uniacid'] . $_W['config']['setting']['authkey']);
  331. header("location: ../payment/unionpay/pay.php?i={$_W['uniacid']}&auth={$auth}&ps={$sl}");
  332. exit();
  333. }
  334. if ($type == 'baifubao') {
  335. $sl = base64_encode(json_encode($ps));
  336. $auth = sha1($sl . $_W['uniacid'] . $_W['config']['setting']['authkey']);
  337. header("location: ../payment/baifubao/pay.php?i={$_W['uniacid']}&auth={$auth}&ps={$sl}");
  338. exit();
  339. }
  340. }