123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
- namespace app\mobile\logic;
- use app\model\CommonModel;
- use app\model\GoodModel;
- use app\model\MasterOrderModel;
- use app\model\PayInfoModel;
- use think\Exception;
- use think\facade\Db;
- use think\response\Json;
- class PayLogic extends BaseLogic
- {
- //微信支付成功后的异步通知及订单处理
- public static function Notify()
- {
- Db::startTrans();
- try {
- $notifyInfo = WechatLogic::Notify();
- $pay_info = PayInfoModel::field('id,type,status')
- ->where('pay_code', $notifyInfo['out_trade_no'])
- ->findOrEmpty();
- //要加lock吗?
- if ($pay_info->isEmpty()) throw new Exception('该支付信息不存在');
- //待处理
- if ($pay_info->status == MasterOrderModel::$order_status_wait_pay) {
- $updatetime = date('Y-m-d H:i:s');
- PayInfoModel::where(['pay_code' => $notifyInfo['out_trade_no'], 'status' => MasterOrderModel::$order_status_wait_pay, 'id' => $pay_info->id])
- ->save([
- 'status' => MasterOrderModel::$order_status_not_deliver,
- 'updaterid' => '0',
- 'updater' => '微信支付',
- 'updatetime' => $updatetime,
- 'transaction_id' => $notifyInfo['transaction_id'],
- 'trade_type' => $notifyInfo['trade_type'],
- 'notify_result' => json_encode($notifyInfo, JSON_UNESCAPED_UNICODE),
- ]);
- if ($pay_info->type == GoodModel::$type_exchange) {
- //兑换商品订单
- Db::name('order')
- ->where(['is_del' => CommonModel::$del_normal, 'pay_info_id' => $pay_info->id, 'status' => MasterOrderModel::$order_status_wait_pay])
- ->update([
- 'updaterid' => '0',
- 'updater' => '微信支付',
- 'updatetime' => $updatetime,
- 'status' => MasterOrderModel::$order_status_not_deliver
- ]);
- } else {
- //服务订单
- Db::name('order_service')
- ->where(['is_del' => CommonModel::$del_normal, 'pay_info_id' => $pay_info->id, 'status' => MasterOrderModel::$order_status_wait_pay])
- ->update([
- 'updaterid' => '0',
- 'updater' => '微信支付',
- 'updatetime' => $updatetime,
- 'status' => MasterOrderModel::$order_status_not_deliver
- ]);
- }
- }
- Db::commit();
- return xml(['return_code' => 'SUCCESS', 'return_msg' => 'DEAL WITH SUCCESS']);
- } catch (Exception $exception) {
- Db::rollback();
- return xml(['return_code' => 'FAIL', 'return_msg' => $exception->getMessage()]);
- }
- // 返回XML状态,至于XML数据可以自己生成,成功状态是必须要返回的。
- // <xml>
- // return_code><![CDATA[SUCCESS]]></return_code>
- // return_msg><![CDATA[OK]]></return_msg>
- // </xml>
- }
- //查询支付结果
- public static function checkPayResult(string $pay_code = ''): Json
- {
- $rs = PayInfoModel::field('id,status')
- ->where(['pay_code' => $pay_code, 'uid' => self::$aid])
- ->findOrEmpty()
- ->toArray();
- if (empty($rs)) return json_show(CommonModel::$error_param, '该支付信息不存在');
- if ($rs['status'] == MasterOrderModel::$order_status_wait_pay) {
- //待支付时,手动去微信端查验是否支付完成(有可能异步通知到的晚,尚未更新状态)
- $temp = WechatLogic::checkPayResult($pay_code);
- $rs['status'] = $temp['RESULT_CODE'];//????@todo
- }
- return json_show(CommonModel::$success, '请求成功', ['result' => $rs['status']]);
- }
- }
|