人人商城

site.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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. class RechargeModuleSite extends WeModuleSite {
  8. public function doMobilePay() {
  9. global $_W, $_GPC;
  10. checkauth();
  11. $type = trim($_GPC['type']) ? trim($_GPC['type']) : 'credit';
  12. if($type == 'credit') {
  13. load() -> model('card');
  14. $recharge_settings = card_params_setting('cardRecharge');
  15. if(checksubmit()) {
  16. $fee = floatval($_GPC['fee']);
  17. $backtype = trim($_GPC['backtype']);
  18. $back= floatval($_GPC['back']);
  19. if (empty($fee) || $fee <= 0) {
  20. message('请选择充值金额', referer(), 'error');
  21. }
  22. $chargerecord = array(
  23. 'uid' => $_W['member']['uid'],
  24. 'openid' => $_W['openid'],
  25. 'uniacid' => $_W['uniacid'],
  26. 'tid' => date('YmdHi').random(8, 1),
  27. 'fee' => $fee,
  28. 'type' => 'credit',
  29. 'tag' => $back,
  30. 'backtype' => $backtype,
  31. 'status' => 0,
  32. 'createtime' => TIMESTAMP,
  33. );
  34. if (!pdo_insert('mc_credits_recharge', $chargerecord)) {
  35. message('创建充值订单失败,请重试!', url('entry', array('m' => 'recharge', 'do' => 'pay')), 'error');
  36. }
  37. $params = array(
  38. 'tid' => $chargerecord['tid'],
  39. 'ordersn' => $chargerecord['tid'],
  40. 'title' => '会员余额充值',
  41. 'fee' => $chargerecord['fee'],
  42. 'user' => $_W['member']['uid'],
  43. );
  44. $mine = array();
  45. if (empty($backtype)) {
  46. $condition = $fee;
  47. $mine = array(
  48. 'name' => "充{$condition}送{$back}元",
  49. 'value' => $fee
  50. );
  51. } elseif ($backtype == '1') {
  52. $condition = $fee;
  53. $mine = array(
  54. 'name' => "充{$condition}送{$back}积分",
  55. 'value' => $fee
  56. );
  57. } elseif ($backtype == '2') {
  58. $condition = $fee;
  59. }
  60. $this->pay($params, $mine);
  61. exit();
  62. }
  63. $member = mc_fetch($_W['member']['uid']);
  64. $name = $member['mobile'];
  65. if(empty($name)) {
  66. $name = $member['realname'];
  67. }
  68. if(empty($name)) {
  69. $name = $member['uid'];
  70. }
  71. include $this->template('recharge');
  72. } else {
  73. $fee = floatval($_GPC['fee']);
  74. if(!$fee) {
  75. message('充值金额不能为0', referer(), 'error');
  76. }
  77. if($fee <= 0) {
  78. message('请输入充值的金额', referer(), 'error');
  79. }
  80. $setting = pdo_get('mc_card', array('uniacid' => $_W['uniacid'], 'status' => 1));
  81. if(empty($setting)) {
  82. message('会员卡未开启,请联系商家', referer(), 'error');
  83. }
  84. if($type == 'card_nums') {
  85. if(!$setting['nums_status']) {
  86. message("会员卡未开启{$setting['nums_text']}充值,请联系商家", referer(), 'error');
  87. }
  88. $setting['nums'] = iunserializer($setting['nums']);
  89. $num_keys = array_keys($setting['nums']);
  90. if (!in_array($fee, $num_keys)) {
  91. message('充值金额错误,请联系商家', referer(), 'error');
  92. }
  93. foreach ($setting['nums'] as $key => $val) {
  94. if ($fee == $val['recharge']) {
  95. $num_back = $val['num'];
  96. }
  97. }
  98. $mine = array(
  99. 'name' => "充{$fee}送{$num_back}次",
  100. 'value' => "送{$num_back}次"
  101. );
  102. $tag = $num_back;
  103. }
  104. if($type == 'card_times') {
  105. if(!$setting['times_status']) {
  106. message("会员卡未开启{$setting['times_text']}充值,请联系商家", referer(), 'error');
  107. }
  108. $setting['times'] = iunserializer($setting['times']);
  109. $time_keys = array_keys($setting['times']);
  110. if (!in_array($fee, $time_keys)) {
  111. message('充值金额错误,请联系商家', referer(), 'error');
  112. }
  113. foreach ($setting['times'] as $key => $val) {
  114. if ($fee == $val['recharge']) {
  115. $time_back = $val['time'];
  116. }
  117. }
  118. $member_card = pdo_get('mc_card_members', array('uniacid' => $_W['uniacid'], 'uid' => $_W['member']['uid']));
  119. if($member_card['endtime'] > TIMESTAMP) {
  120. $endtime = $member_card['endtime'] + time_back * 86400;
  121. } else {
  122. $endtime = strtotime($time_back . 'days');
  123. }
  124. $mine = array(
  125. 'name' => "充{$fee}送{$time_back}天",
  126. 'value' => date('Y-m-d', $endtime) . '到期'
  127. );
  128. $tag = $time_back;
  129. }
  130. $chargerecord = pdo_fetch("SELECT * FROM ".tablename('mc_credits_recharge')." WHERE uniacid = :uniacid AND uid = :uid AND fee = :fee AND type = :type AND status = 0 AND tag = :tag", array(
  131. ':uniacid' => $_W['uniacid'],
  132. ':uid' => $_W['member']['uid'],
  133. ':fee' => $fee,
  134. ':type' => $type,
  135. ':tag' => $tag,
  136. ));
  137. if (empty($chargerecord)) {
  138. $chargerecord = array(
  139. 'uid' => $_W['member']['uid'],
  140. 'openid' => $_W['openid'],
  141. 'uniacid' => $_W['uniacid'],
  142. 'tid' => date('YmdHi').random(8, 1),
  143. 'fee' => $fee,
  144. 'type' => $type,
  145. 'tag' => $tag,
  146. 'status' => 0,
  147. 'createtime' => TIMESTAMP,
  148. );
  149. if (!pdo_insert('mc_credits_recharge', $chargerecord)) {
  150. message('创建充值订单失败,请重试!', url('mc/card/mycard'), 'error');
  151. }
  152. }
  153. $types = array(
  154. 'card_nums' => $setting['nums_text'],
  155. 'card_times' => $setting['times_text'],
  156. );
  157. $params = array(
  158. 'tid' => $chargerecord['tid'],
  159. 'ordersn' => $chargerecord['tid'],
  160. 'title' => "会员卡{$types[$type]}充值",
  161. 'fee' => $chargerecord['fee'],
  162. 'user' => $_W['member']['uid'],
  163. );
  164. $this->pay($params, $mine);
  165. exit();
  166. }
  167. }
  168. public function payResult($params) {
  169. global $_W;
  170. load()-> model('mc');
  171. load() -> model('card');
  172. $order = pdo_fetch("SELECT * FROM ".tablename('mc_credits_recharge')." WHERE tid = :tid", array(':tid' => $params['tid']));
  173. if ($params['result'] == 'success' && $params['from'] == 'notify') {
  174. $fee = $params['fee'];
  175. $total_fee = $fee;
  176. $data = array('status' => $params['result'] == 'success' ? 1 : -1);
  177. if ($params['type'] == 'wechat') {
  178. $data['transid'] = $params['tag']['transaction_id'];
  179. $params['user'] = mc_openid2uid($params['user']);
  180. }
  181. pdo_update('mc_credits_recharge', $data, array('tid' => $params['tid']));
  182. $paydata = array('wechat' => '微信', 'alipay' => '支付宝', 'baifubao' => '百付宝', 'unionpay' => '银联');
  183. if(empty($order['type']) || $order['type'] == 'credit') {
  184. $setting = uni_setting($_W['uniacid'], array('creditbehaviors', 'recharge'));
  185. $credit = $setting['creditbehaviors']['currency'];
  186. $recharge_settings = card_params_setting('cardRecharge');
  187. $recharge_params = $recharge_settings['params'];
  188. if(empty($credit)) {
  189. message('站点积分行为参数配置错误,请联系服务商', '', 'error');
  190. } else {
  191. if ($recharge_params['recharge_type'] == '1') {
  192. $recharges = $recharge_params['recharges'];
  193. }
  194. if ($order['backtype'] == '2') {
  195. $total_fee = $fee;
  196. } else {
  197. foreach ($recharges as $key => $recharge) {
  198. if ($recharge['backtype'] == $order['backtype'] && $recharge['condition'] == $order['fee']) {
  199. if ($order['backtype'] == '1') {
  200. $total_fee = $fee;
  201. $add_credit = $recharge['back'];
  202. } else {
  203. $total_fee = $fee + $recharge['back'];
  204. }
  205. }
  206. }
  207. }
  208. if ($order['backtype'] == '1') {
  209. $add_str = ",充值成功,返积分{$add_credit}分,本次操作共增加余额{$total_fee}元,积分{$add_credit}分";
  210. $remark = '用户通过' . $paydata[$params['type']] . '充值' . $fee . $add_str;
  211. $record[] = $params['user'];
  212. $record[] = $remark;
  213. mc_credit_update($order['uid'], 'credit1', $add_credit, $record);
  214. mc_credit_update($order['uid'], 'credit2', $total_fee, $record);
  215. mc_notice_recharge($order['openid'], $order['uid'], $total_fee, '', $remark);
  216. } else {
  217. $add_str = ",充值成功,本次操作共增加余额{$total_fee}元";
  218. $remark = '用户通过' . $paydata[$params['type']] . '充值' . $fee . $add_str;
  219. $record[] = $params['user'];
  220. $record[] = $remark;
  221. mc_credit_update($order['uid'], 'credit2', $total_fee, $record);
  222. mc_notice_recharge($order['openid'], $order['uid'], $total_fee, '', $remark);
  223. }
  224. }
  225. }
  226. if($order['type'] == 'card_nums') {
  227. $member_card = pdo_get('mc_card_members', array('uniacid' => $order['uniacid'], 'uid' => $order['uid']));
  228. $total_num = $member_card['nums'] + $order['tag'];
  229. pdo_update('mc_card_members', array('nums' => $total_num), array('uniacid' => $order['uniacid'], 'uid' => $order['uid']));
  230. $log = array(
  231. 'uniacid' => $order['uniacid'],
  232. 'uid' => $order['uid'],
  233. 'type' => 'nums',
  234. 'fee' => $params['fee'],
  235. 'model' => '1',
  236. 'tag' => $order['tag'], 'note' => date('Y-m-d H:i') . "通过{$paydata[$params['type']]}充值{$params['fee']}元,返{$order['tag']}次,总共剩余{$total_num}次",
  237. 'addtime' => TIMESTAMP
  238. );
  239. pdo_insert('mc_card_record', $log);
  240. $type = pdo_fetchcolumn('SELECT nums_text FROM ' . tablename('mc_card') . ' WHERE uniacid = :uniacid', array(':uniacid' => $order['uniacid']));
  241. $total_num = $member_card['nums'] + $order['tag'];
  242. mc_notice_nums_plus($order['openid'], $type, $order['tag'], $total_num);
  243. }
  244. if($order['type'] == 'card_times') {
  245. $member_card = pdo_get('mc_card_members', array('uniacid' => $order['uniacid'], 'uid' => $order['uid']));
  246. if($member_card['endtime'] > TIMESTAMP) {
  247. $endtime = $member_card['endtime'] + $order['tag'] * 86400;
  248. } else {
  249. $endtime = strtotime($order['tag'] . 'days');
  250. }
  251. pdo_update('mc_card_members', array('endtime' => $endtime), array('uniacid' => $order['uniacid'], 'uid' => $order['uid']));
  252. $log = array(
  253. 'uniacid' => $order['uniacid'],
  254. 'uid' => $order['uid'],
  255. 'type' => 'times',
  256. 'model' => '1',
  257. 'fee' => $params['fee'],
  258. 'tag' => $order['tag'], 'note' => date('Y-m-d H:i') . "通过{$paydata[$params['type']]}充值{$params['fee']}元,返{$order['tag']}天,充值后到期时间:". date('Y-m-d', $endtime),
  259. 'addtime' => TIMESTAMP
  260. );
  261. pdo_insert('mc_card_record', $log);
  262. $type = pdo_fetchcolumn('SELECT times_text FROM ' . tablename('mc_card') . ' WHERE uniacid = :uniacid', array(':uniacid' => $order['uniacid']));
  263. $endtime = date('Y-m-d', $endtime);
  264. mc_notice_times_plus($order['openid'], $member_card['cardsn'], $type, $fee, $order['tag'], $endtime);
  265. }
  266. }
  267. if($order['type'] == 'credit' || $order['type'] == '') {
  268. $url = murl('mc/home');
  269. } else {
  270. $url = murl('mc/card/mycard');
  271. }
  272. if ($params['from'] == 'return') {
  273. if ($params['result'] == 'success') {
  274. message('支付成功!', $_W['siteroot'] . 'app/' . $url, 'success');
  275. } else {
  276. message('支付失败!', $_W['siteroot'] . 'app/' . $url, 'error');
  277. }
  278. }
  279. }
  280. protected function pay($params = array(), $mine = array()) {
  281. global $_W;
  282. $params['module'] = $this->module['name'];
  283. $sql = 'SELECT * FROM ' . tablename('core_paylog') . ' WHERE `uniacid`=:uniacid AND `module`=:module AND `tid`=:tid';
  284. $pars = array();
  285. $pars[':uniacid'] = $_W['uniacid'];
  286. $pars[':module'] = $params['module'];
  287. $pars[':tid'] = $params['tid'];
  288. $log = pdo_fetch($sql, $pars);
  289. if(!empty($log) && $log['status'] == '1') {
  290. itoast('这个订单已经支付成功, 不需要重复支付.', '', 'info');
  291. }
  292. $setting = uni_setting($_W['uniacid'], array('payment', 'creditbehaviors'));
  293. if(!is_array($setting['payment'])) {
  294. itoast('没有有效的支付方式, 请联系网站管理员.', '', 'error');
  295. }
  296. $log = pdo_get('core_paylog', array('uniacid' => $_W['uniacid'], 'module' => $params['module'], 'tid' => $params['tid']));
  297. if (empty($log)) {
  298. $log = array(
  299. 'uniacid' => $_W['uniacid'],
  300. 'acid' => $_W['acid'],
  301. 'openid' => $_W['member']['uid'],
  302. 'module' => $this->module['name'], 'tid' => $params['tid'],
  303. 'fee' => $params['fee'],
  304. 'card_fee' => $params['fee'],
  305. 'status' => '0',
  306. 'is_usecard' => '0',
  307. );
  308. pdo_insert('core_paylog', $log);
  309. }
  310. $pay = $setting['payment'];
  311. foreach ($pay as &$value) {
  312. $value['switch'] = $value['recharge_switch'];
  313. }
  314. unset($value);
  315. $pay['credit']['switch'] = false;
  316. $pay['delivery']['switch'] = false;
  317. include $this->template('common/paycenter');
  318. }
  319. }