PayLogic.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. namespace app\mobile\logic;
  3. use app\model\CommonModel;
  4. use app\model\GoodModel;
  5. use app\model\MasterOrderModel;
  6. use app\model\PayInfoModel;
  7. use think\Exception;
  8. use think\facade\Db;
  9. use think\response\Json;
  10. class PayLogic extends BaseLogic
  11. {
  12. //微信支付成功后的异步通知及订单处理
  13. public static function Notify()
  14. {
  15. Db::startTrans();
  16. try {
  17. $notifyInfo = WechatLogic::Notify();
  18. $pay_info = PayInfoModel::field('id,type,status')
  19. ->where('pay_code', $notifyInfo['out_trade_no'])
  20. ->findOrEmpty();
  21. //要加lock吗?
  22. if ($pay_info->isEmpty()) throw new Exception('该支付信息不存在');
  23. //待处理
  24. if ($pay_info->status == MasterOrderModel::$order_status_wait_pay) {
  25. $updatetime = date('Y-m-d H:i:s');
  26. PayInfoModel::where(['pay_code' => $notifyInfo['out_trade_no'], 'status' => MasterOrderModel::$order_status_wait_pay, 'id' => $pay_info->id])
  27. ->save([
  28. 'status' => MasterOrderModel::$order_status_not_deliver,
  29. 'updaterid' => '0',
  30. 'updater' => '微信支付',
  31. 'updatetime' => $updatetime,
  32. 'transaction_id' => $notifyInfo['transaction_id'],
  33. 'trade_type' => $notifyInfo['trade_type'],
  34. 'notify_result' => json_encode($notifyInfo, JSON_UNESCAPED_UNICODE),
  35. ]);
  36. if ($pay_info->type == GoodModel::$type_exchange) {
  37. //兑换商品订单
  38. Db::name('order')
  39. ->where(['is_del' => CommonModel::$del_normal, 'pay_info_id' => $pay_info->id, 'status' => MasterOrderModel::$order_status_wait_pay])
  40. ->update([
  41. 'updaterid' => '0',
  42. 'updater' => '微信支付',
  43. 'updatetime' => $updatetime,
  44. 'status' => MasterOrderModel::$order_status_not_deliver
  45. ]);
  46. } else {
  47. //服务订单
  48. Db::name('order_service')
  49. ->where(['is_del' => CommonModel::$del_normal, 'pay_info_id' => $pay_info->id, 'status' => MasterOrderModel::$order_status_wait_pay])
  50. ->update([
  51. 'updaterid' => '0',
  52. 'updater' => '微信支付',
  53. 'updatetime' => $updatetime,
  54. 'status' => MasterOrderModel::$order_status_not_deliver
  55. ]);
  56. }
  57. }
  58. Db::commit();
  59. return xml(['return_code' => 'SUCCESS', 'return_msg' => 'DEAL WITH SUCCESS']);
  60. } catch (Exception $exception) {
  61. Db::rollback();
  62. return xml(['return_code' => 'FAIL', 'return_msg' => $exception->getMessage()]);
  63. }
  64. // 返回XML状态,至于XML数据可以自己生成,成功状态是必须要返回的。
  65. // <xml>
  66. // return_code><![CDATA[SUCCESS]]></return_code>
  67. // return_msg><![CDATA[OK]]></return_msg>
  68. // </xml>
  69. }
  70. //查询支付结果
  71. public static function checkPayResult(string $pay_code = ''): Json
  72. {
  73. $rs = PayInfoModel::field('id,status')
  74. ->where(['pay_code' => $pay_code, 'uid' => self::$aid])
  75. ->findOrEmpty()
  76. ->toArray();
  77. if (empty($rs)) return json_show(CommonModel::$error_param, '该支付信息不存在');
  78. if ($rs['status'] == MasterOrderModel::$order_status_wait_pay) {
  79. //待支付时,手动去微信端查验是否支付完成(有可能异步通知到的晚,尚未更新状态)
  80. $temp = WechatLogic::checkPayResult($pay_code);
  81. $rs['status'] = $temp['RESULT_CODE'];//????@todo
  82. }
  83. return json_show(CommonModel::$success, '请求成功', ['result' => $rs['status']]);
  84. }
  85. }