wxmicro.ctrl.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. uni_user_permission_check('paycenter_wxmicro_pay');
  8. $_W['page']['title'] = '刷卡支付-微信收款';
  9. $dos = array('pay', 'query', 'checkpay');
  10. $do = in_array($do, $dos) ? $do : 'pay';
  11. load()->model('paycenter');
  12. if($do == 'pay') {
  13. if($_W['isajax']) {
  14. $post = $_GPC['__input'];
  15. $fee = trim($post['fee']) ? trim($post['fee']) : message(error(-1, '订单金额不能为空'), '', 'ajax');
  16. $body = trim($post['body']) ? trim($post['body']) : message(error(-1, '商品名称不能为空'), '', 'ajax');
  17. $code = trim($post['code']);
  18. if($post['cash'] > 0 && empty($post['code'])) {
  19. message(error(-1, '授权码不能为空'), '', 'ajax');
  20. }
  21. $total = $money = floatval($post['fee']);
  22. if(!$total) {
  23. message(error(-1, '消费金额不能为空'), '', 'ajax');
  24. }
  25. $log = "系统日志:会员消费【{$total}】元";
  26. if($post['member']['uid'] > 0) {
  27. $user = pdo_get('mc_members', array('uniacid' => $_W['uniacid'], 'uid' => intval($post['member']['uid']) ));;
  28. if(empty($user)) {
  29. message(error(-1, '用户不存在'), '', 'ajax');
  30. }
  31. $user['groupname'] = $_W['account']['groups'][$user['groupid']]['title'];
  32. load()->model('card');
  33. $card = card_setting();
  34. load()->model('card');
  35. $member = pdo_get('mc_card_members', array('uniacid' => $_W['uniacid'], 'uid' => $user['uid']));
  36. if(!empty($card) && $card['status'] == 1 && !empty($member)) {
  37. $user['discount'] = $card['discount'][$user['groupid']];
  38. if(!empty($user['discount']) && !empty($user['discount']['discount'])) {
  39. if($total >= $user['discount']['condition']) {
  40. $log .= ",所在会员组【{$user['groupname']}】,可享受满【{$user['discount']['condition']}】元";
  41. if($card['discount_type'] == 1) {
  42. $log .= "减【{$user['discount']['discount']}】元";
  43. $money = $total - $user['discount']['discount'];
  44. } else {
  45. $discount = $user['discount']['discount'] * 10;
  46. $log .= "打【{$discount}】折";
  47. $money = $total * $user['discount']['discount'];
  48. }
  49. if($money < 0) {
  50. $money = 0;
  51. }
  52. $log .= ",实收金额【{$money}】元";
  53. }
  54. }
  55. $post_money = strval($post['fact_fee']);
  56. if($post_money != $money) {
  57. message(error(-1, '实收金额错误'), '', 'ajax');
  58. }
  59. $post_credit1 = intval($post['credit1']);
  60. if($post_credit1 > 0) {
  61. if($post_credit1 > $user['credit1']) {
  62. message(error(-1, '超过会员账户可用积分'), '', 'ajax');
  63. }
  64. }
  65. $post_offset_money = trim($post['offset_money']);
  66. $offset_money = 0;
  67. if($post_credit1 && $card['offset_rate'] > 0 && $card['offset_max'] >= 0) {
  68. if ($card['offset_max'] == '0') {
  69. $offset_money = $post_credit1/$card['offset_rate'];
  70. } else {
  71. $offset_money = min($card['offset_max'], $post_credit1/$card['offset_rate']);
  72. }
  73. if($offset_money != $post_offset_money) {
  74. message(error(-1, '积分抵消金额错误'), '', 'ajax');
  75. }
  76. $credit1 = $post_credit1;
  77. $log .= ",使用【{$post_credit1}】积分抵消【{$offset_money}】元";
  78. }
  79. }
  80. $credit2 = floatval($post['credit2']);
  81. if($credit2 > 0) {
  82. if($credit2 > $user['credit2']) {
  83. message(error(-1, '超过会员账户可用余额'), '', 'ajax');
  84. }
  85. $log .= ",使用余额支付【{$credit2}】元";
  86. }
  87. } else {
  88. $post['cash'] = $post['fee'];
  89. }
  90. $cash = floatval($post['cash']);
  91. $sum = strval($credit2 + $cash + $offset_money);
  92. $money = strval($money);
  93. if($sum != $money) {
  94. message(error(-1, '支付金额不等于实收金额'), '', 'ajax');
  95. }
  96. $realname = $post['member']['realname'] ? $post['member']['realname'] :$post['member']['realname'];
  97. if($cash <= 0) {
  98. $data = array(
  99. 'uniacid' => $_W['uniacid'],
  100. 'uid' => $member['uid'],
  101. 'status' => 0,
  102. 'type' => 'wechat',
  103. 'trade_type' => 'micropay',
  104. 'fee' => $total,
  105. 'final_fee' => $money,
  106. 'credit1' => $post_credit1,
  107. 'credit1_fee' => $offset_money,
  108. 'credit2' => $credit2,
  109. 'cash' => $cash,
  110. 'body' => $body,
  111. 'nickname' => $realname,
  112. 'remark' => $log,
  113. 'clerk_id' => $_W['user']['clerk_id'],
  114. 'store_id' => $_W['user']['store_id'],
  115. 'clerk_type' => $_W['user']['clerk_type'],
  116. 'createtime' => TIMESTAMP,
  117. 'status' => 1,
  118. 'paytime' => TIMESTAMP,
  119. 'credit_status' => 1,
  120. );
  121. pdo_insert('paycenter_order', $data);
  122. load()->model('mc');
  123. if($post_credit1 > 0) {
  124. $status = mc_credit_update($member['uid'], 'credit1', -$post_credit1, array(0, "会员刷卡消费,使用积分抵现,扣除{$post_credit1积分}", 'system', $_W['user']['clerk_id'], $_W['user']['store_id'], $_W['user']['clerk_type']));
  125. }
  126. if($credit2 > 0) {
  127. $status = mc_credit_update($member['uid'], 'credit2', -$credit2, array(0, "会员刷卡消费,使用余额支付,扣除{$credit2}余额", 'system', $_W['user']['clerk_id'], $_W['user']['store_id'], $_W['user']['clerk_type']));
  128. }
  129. message(error(0, '支付成功'), url('paycenter/wxmicro'), 'ajax');
  130. } else {
  131. $log .= ",使用刷卡支付【{$cash}】元";
  132. if(!empty($_GPC['remark'])) {
  133. $note = "店员备注:{$_GPC['remark']}";
  134. }
  135. $log = $note.$log;
  136. $isexist = pdo_get('paycenter_order', array('uniacid' => $_W['uniacid'], 'auth_code' => $code));
  137. if($isexist) {
  138. message(error(-1, '每个二维码仅限使用一次,请刷新再试'), '', 'ajax');
  139. }
  140. $data = array(
  141. 'uniacid' => $_W['uniacid'],
  142. 'uid' => $member['uid'],
  143. 'status' => 0,
  144. 'type' => 'wechat',
  145. 'trade_type' => 'micropay',
  146. 'fee' => $total,
  147. 'final_fee' => $money,
  148. 'credit1' => $post_credit1,
  149. 'credit1_fee' => $offset_money,
  150. 'credit2' => $credit2,
  151. 'cash' => $cash,
  152. 'remark' => $log,
  153. 'body' => $body,
  154. 'nickname' => $realname,
  155. 'auth_code' => $code,
  156. 'clerk_id' => $_W['user']['clerk_id'],
  157. 'store_id' => $_W['user']['store_id'],
  158. 'clerk_type' => $_W['user']['clerk_type'],
  159. 'createtime' => TIMESTAMP,
  160. );
  161. pdo_insert('paycenter_order', $data);
  162. $id = pdo_insertid();
  163. load()->classs('pay');
  164. $pay = Pay::create();
  165. $params = array(
  166. 'tid' => $id,
  167. 'module' => 'paycenter',
  168. 'type' => 'wechat',
  169. 'fee' => $cash,
  170. 'body' => $body,
  171. 'auth_code' => $code,
  172. );
  173. $pid = $pay->buildPayLog($params);
  174. if(is_error($pid)) {
  175. message($pid, '', 'ajax');
  176. }
  177. $log = pdo_get('core_paylog', array('plid' => $pid));
  178. pdo_update('paycenter_order', array('pid' => $pid, 'uniontid' => $log['uniontid']), array('id' => $id));
  179. $data = array(
  180. 'out_trade_no' => $log['uniontid'],
  181. 'body' => $body,
  182. 'total_fee' => $log['fee'] * 100,
  183. 'auth_code' => $code,
  184. 'uniontid' => $log['uniontid']
  185. );
  186. $result = $pay->buildMicroOrder($data);
  187. if ($result['result_code'] == 'SUCCESS') {
  188. if(is_error($result)) {
  189. message($result, '', 'ajax');
  190. } else {
  191. $status = $pay->NoticeMicroSuccessOrder($result);
  192. if(is_error($status)) {
  193. message($status, '', 'ajax');
  194. }
  195. message(error(0, '支付成功'), url('paycenter/wxmicro'), 'ajax');
  196. }
  197. } else {
  198. message($result, '', 'ajax');
  199. }
  200. }
  201. exit();
  202. }
  203. $paycenter_records = pdo_fetchall("SELECT * FROM " .tablename('paycenter_order') . " WHERE uniacid = :uniacid AND clerk_id = :clerk_id ORDER BY id DESC LIMIT 0,10", array(':uniacid' => $_W['uniacid'], ':clerk_id' => $_W['user']['clerk_id']));
  204. $today_credit_total = pdo_fetchall("SELECT credit2 FROM " . tablename('paycenter_order') . " WHERE uniacid = :uniacid AND clerk_id = :clerk_id AND paytime > :starttime AND paytime < :endtime AND credit2 <> ''", array(':uniacid' => $_W['uniacid'], ':clerk_id' => trim($_W['user']['clerk_id']), ':starttime' => strtotime(date('Ymd')), ':endtime' => time()));
  205. $today_wechat_total = pdo_fetchall("SELECT cash FROM " . tablename('paycenter_order') . " WHERE uniacid = :uniacid AND clerk_id = :clerk_id AND paytime > :starttime AND paytime < :endtime AND cash <> ''", array(':uniacid' => $_W['uniacid'], ':clerk_id' => trim($_W['user']['clerk_id']), ':starttime' => strtotime(date('Ymd')), ':endtime' => time()));
  206. foreach ($today_wechat_total as $val) {
  207. $wechat_total += $val['cash'];
  208. }
  209. foreach ($today_credit_total as $val) {
  210. $credit_total += $val['credit2'];
  211. }
  212. $wechat_total = $wechat_total ? $wechat_total : '0';
  213. $credit_total = $credit_total ? $credit_total : '0';
  214. load()->model('card');
  215. $card_set = card_setting();
  216. $card_params = json_decode($card_set['params'], true);
  217. $grant_rate = $card_set['grant_rate'];
  218. unset($card_set['params'], $card_set['nums'], $card_set['times'], $card_set['business'], $card_set['html'], $card_set['description'], $card_set['card_id']);
  219. $card_set_str = json_encode($card_set);
  220. }
  221. if($do == 'query') {
  222. if($_W['isajax']) {
  223. $post = $_GPC['__input'];
  224. $uniontid = trim($post['uniontid']);
  225. load()->classs('pay');
  226. $pay = Pay::create();
  227. $result = $pay->queryOrder($uniontid, 2);
  228. if(is_error($result)) {
  229. message($result, '', 'ajax');
  230. }
  231. if($result['trade_state'] == 'SUCCESS') {
  232. $status = $pay->NoticeMicroSuccessOrder($result);
  233. if(is_error($status)) {
  234. message($status, '', 'ajax');
  235. }
  236. message(error(0, '支付成功'), '', 'ajax');
  237. }
  238. message(error(-1, '支付失败,当前订单状态:' . $result['trade_state']), '', 'ajax');
  239. }
  240. }
  241. if ($do == 'checkpay') {
  242. if($_W['isajax']) {
  243. $post = $_GPC['__input'];
  244. $uniontid = trim($post['uniontid']);
  245. load()->classs('pay');
  246. $pay = Pay::create();
  247. $result = $pay->queryOrder($uniontid, 2);
  248. if(is_error($result)) {
  249. message($result, '', 'ajax');
  250. }
  251. if($result['trade_state'] == 'SUCCESS') {
  252. $status = $pay->NoticeMicroSuccessOrder($result);
  253. if(is_error($status)) {
  254. message($status, '', 'ajax');
  255. }
  256. message($result, '', 'ajax');
  257. }
  258. message($result, '', 'ajax');
  259. }
  260. }
  261. template('paycenter/wxmicro');