PayLogic.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <?php
  2. namespace app\mobile\logic;
  3. use app\model\AccountModel;
  4. use app\model\AddrModel;
  5. use app\model\CommonModel;
  6. use app\model\GoodModel;
  7. use app\model\PayInfoModel;
  8. use app\model\ServiceModel;
  9. use think\facade\Db;
  10. use think\response\Json;
  11. class PayLogic extends BaseLogic
  12. {
  13. //微信支付预下单
  14. public static function getPrepayId(array $data = []): json
  15. {
  16. $openId = AccountModel::where(['id' => self::$aid, 'is_del' => CommonModel::$del_normal])
  17. ->value('wx_openId', '');
  18. if ($openId == '') return json_show(CommonModel::$error_token, '获取账户openId失败,请重新登录');
  19. Db::startTrans();
  20. try {
  21. $rs = AddrModel::field('id')
  22. ->where(['is_del' => CommonModel::$del_normal, 'id' => $data['addr_id'], 'uid' => self::$aid])
  23. ->findOrEmpty()
  24. ->isEmpty();
  25. if ($rs) throw new Exception('该地址不存在');
  26. $amount = '0';//支付金额
  27. $num = array_column($data['list'], 'num', 'id');//
  28. $date = date('Y-m-d H:i:s');
  29. $pay_code = make_no('ZF');
  30. //支付信息表
  31. $pay_info_id = Db::name('pay_info')->insertGetId([
  32. 'pay_code' => $pay_code,
  33. 'wx_openId' => $openId,
  34. 'type' => $data['type'],
  35. 'ids' => json_encode($data['list']),
  36. 'status' => CommonModel::$order_status_wait_pay,
  37. 'createrid' => self::$aid,
  38. 'creater' => self::$aname,
  39. 'addtime' => $date,
  40. 'updaterid' => self::$aid,
  41. 'updater' => self::$aname,
  42. 'updatetime' => $date,
  43. ]);
  44. if ($data['type'] == CommonModel::$pay_type_service) {
  45. //购买服务
  46. $rs = ServiceModel::field('id,original_price,activity_price,title,activity_status')
  47. ->whereIn('id', array_column($data['list'], 'id'))
  48. ->where(['is_del' => CommonModel::$del_normal, 'status' => CommonModel::$status_normal])
  49. ->select()
  50. ->toArray();
  51. if (empty($rs)) throw new Exception('购买的服务不存在或已下架');
  52. $insert_service = [];
  53. foreach ($rs as $item) {
  54. //单价
  55. if ($item['activity_status'] == ServiceModel::$activity_status_ing) $price = $rs['activity_price'];
  56. else $price = $rs['original_price'];
  57. $total_price = bcmul($price, $num[$item['id']] ?? 0, 2);//总价
  58. $amount = bcadd($amount, $total_price, 2);//总金额
  59. $insert_service[] = [
  60. 'orderCode' => make_no('FW'),
  61. 'pay_info_id' => $pay_info_id,
  62. 'uid' => self::$aid,
  63. 'service_id' => $item['id'],
  64. 'num' => $num[$item['id']] ?? 0,
  65. 'addr_id' => $data['addr_id'],
  66. 'price' => $price,
  67. 'total_price' => $total_price,
  68. 'status' => CommonModel::$order_status_wait_pay,
  69. 'is_del' => CommonModel::$del_normal,
  70. 'createrid' => self::$aid,
  71. 'creater' => self::$aname,
  72. 'addtime' => $date,
  73. 'updaterid' => self::$aid,
  74. 'updater' => self::$aname,
  75. 'updatetime' => $date,
  76. ];
  77. }
  78. //向服务订单表写数据
  79. Db::name('order_service')->insertAll($insert_service);
  80. } else {
  81. //购买商城商品
  82. $order_insert = [];
  83. $rs = GoodModel::field('id,price,good_name')
  84. ->whereIn('id', array_column($data['list'], 'id'))
  85. ->where(['is_del' => CommonModel::$del_normal, 'status' => CommonModel::$status_normal])
  86. ->select()
  87. ->toArray();
  88. if (empty($rs)) throw new Exception('购买的商品不存在或已禁用');
  89. $i = 0;
  90. $orderCode = make_no('QR');
  91. $orderCode = substr($orderCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  92. foreach ($rs as $item) {
  93. $total_price = bcmul($item['price'], $num[$item['id']] ?? 0, 2);//总价
  94. $order_insert[] = [
  95. 'orderCode' => $orderCode,
  96. 'pay_info_id' => $pay_info_id,
  97. 'company_id' => self::$company_id,
  98. 'card_id' => self::$card_id,
  99. 'uid' => self::$aid,
  100. 'good_id' => $item['id'],
  101. 'num' => $num[$item['id']],
  102. 'price' => $item['price'],
  103. 'total_price' => $total_price,
  104. 'addr_id' => $data['addr_id'],
  105. 'type' => CommonModel::$pay_type_shopping_good,
  106. 'status' => CommonModel::$order_status_wait_pay,
  107. 'is_del' => CommonModel::$del_normal,
  108. 'createrid' => self::$aid,
  109. 'creater' => self::$aname,
  110. 'addtime' => $date,
  111. 'updaterid' => self::$aid,
  112. 'updater' => self::$aname,
  113. 'updatetime' => $date,
  114. ];
  115. $amount = bcadd($amount, $total_price, 2);
  116. $i++;
  117. }
  118. }
  119. //获取预支付信息,
  120. //@todo body要写什么????????
  121. $result = WechatLogic::getPrepayId($openId, '', $pay_code, $amount);
  122. //更新pay_info表
  123. Db::name('pay_info')
  124. ->where('id', $pay_info_id)
  125. ->update([
  126. 'prepay_info' => $result,
  127. 'amount' => $amount,
  128. 'expiretime' => date('Y-m-d H:i:s', time() + 5 * 60),
  129. ]);
  130. //@todo 要给前端返回什么信息?
  131. $res = ['pay_code' => $pay_code];
  132. Db::commit();
  133. return json_show(CommonModel::$success, '微信支付预下单成功', $res);
  134. } catch (Exception $exception) {
  135. Db::rollback();
  136. return json_show(CommonModel::$success, '微信支付预下单失败' . $exception->getMessage());
  137. }
  138. }
  139. //微信支付成功后的异步通知及订单处理
  140. public static function Notify()
  141. {
  142. Db::startTrans();
  143. try {
  144. $notifyInfo = WechatLogic::Notify();
  145. $pay_info = PayInfoModel::field('id,type,status')
  146. ->where('pay_code', $notifyInfo['out_trade_no'])
  147. ->findOrEmpty();
  148. //要加lock吗?
  149. if ($pay_info->isEmpty()) throw new Exception('该支付信息不存在');
  150. //待处理
  151. if ($pay_info->status == CommonModel::$order_status_wait_pay) {
  152. $updatetime = date('Y-m-d H:i:s');
  153. PayInfoModel::where(['pay_code' => $notifyInfo['out_trade_no'], 'status' => CommonModel::$order_status_wait_pay, 'id' => $pay_info->id])
  154. ->save([
  155. 'status' => CommonModel::$order_status_not_deliver,
  156. 'updaterid' => '0',
  157. 'updater' => '微信支付',
  158. 'updatetime' => $updatetime,
  159. 'transaction_id' => $notifyInfo['transaction_id'],
  160. 'trade_type' => $notifyInfo['trade_type'],
  161. 'notify_result' => json_encode($notifyInfo, JSON_UNESCAPED_UNICODE),
  162. ]);
  163. if ($pay_info->type == GoodModel::$type_exchange) {
  164. //兑换商品订单
  165. Db::name('order')
  166. ->where(['is_del' => CommonModel::$del_normal, 'pay_info_id' => $pay_info->id, 'status' => CommonModel::$order_status_wait_pay])
  167. ->update([
  168. 'updaterid' => '0',
  169. 'updater' => '微信支付',
  170. 'updatetime' => $updatetime,
  171. 'status' => CommonModel::$order_status_not_deliver
  172. ]);
  173. } else {
  174. //服务订单
  175. Db::name('order_service')
  176. ->where(['is_del' => CommonModel::$del_normal, 'pay_info_id' => $pay_info->id, 'status' => CommonModel::$order_status_wait_pay])
  177. ->update([
  178. 'updaterid' => '0',
  179. 'updater' => '微信支付',
  180. 'updatetime' => $updatetime,
  181. 'status' => CommonModel::$order_status_not_deliver
  182. ]);
  183. }
  184. }
  185. Db::commit();
  186. return xml(['return_code' => 'SUCCESS', 'return_msg' => 'DEAL WITH SUCCESS']);
  187. } catch (Exception $exception) {
  188. Db::rollback();
  189. return xml(['return_code' => 'FAIL', 'return_msg' => $exception->getMessage()]);
  190. }
  191. // 返回XML状态,至于XML数据可以自己生成,成功状态是必须要返回的。
  192. // <xml>
  193. // return_code><![CDATA[SUCCESS]]></return_code>
  194. // return_msg><![CDATA[OK]]></return_msg>
  195. // </xml>
  196. }
  197. //查询支付结果
  198. public static function checkPayResult(string $pay_code = ''): Json
  199. {
  200. $rs = PayInfoModel::field('id,status')
  201. ->where(['pay_code' => $pay_code, 'uid' => self::$aid])
  202. ->findOrEmpty()
  203. ->toArray();
  204. if (empty($rs)) return json_show(CommonModel::$error_param, '该支付信息不存在');
  205. if ($rs['status'] == CommonModel::$order_status_wait_pay) {
  206. //待支付时,手动去微信端查验是否支付完成(有可能异步通知到的晚,尚未更新状态)
  207. $temp = WechatLogic::checkPayResult($pay_code);
  208. $rs['status'] = $temp['RESULT_CODE'];//????@todo
  209. }
  210. return json_show(CommonModel::$success, '请求成功', ['result' => $rs['status']]);
  211. }
  212. }