|
@@ -0,0 +1,264 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace app\mobile\logic;
|
|
|
+
|
|
|
+use app\model\AccountModel;
|
|
|
+use app\model\AddrModel;
|
|
|
+use app\model\CommonModel;
|
|
|
+use app\model\GoodModel;
|
|
|
+use app\model\PayInfoModel;
|
|
|
+use app\model\ServiceModel;
|
|
|
+use think\facade\Db;
|
|
|
+use think\response\Json;
|
|
|
+
|
|
|
+class PayLogic extends BaseLogic
|
|
|
+{
|
|
|
+
|
|
|
+ //微信支付预下单
|
|
|
+ public static function getPrepayId(array $data = []): json
|
|
|
+ {
|
|
|
+ $openId = AccountModel::where(['id' => self::$aid, 'is_del' => CommonModel::$del_normal])
|
|
|
+ ->value('wx_openId', '');
|
|
|
+
|
|
|
+ if ($openId == '') return json_show(CommonModel::$error_token, '获取账户openId失败,请重新登录');
|
|
|
+
|
|
|
+ Db::startTrans();
|
|
|
+
|
|
|
+ try {
|
|
|
+
|
|
|
+ $rs = AddrModel::field('id')
|
|
|
+ ->where(['is_del' => CommonModel::$del_normal, 'id' => $data['addr_id'], 'uid' => self::$aid])
|
|
|
+ ->findOrEmpty()
|
|
|
+ ->isEmpty();
|
|
|
+ if ($rs) throw new Exception('该地址不存在');
|
|
|
+
|
|
|
+ $amount = '0';//支付金额
|
|
|
+ $num = array_column($data['list'], 'num', 'id');//
|
|
|
+ $date = date('Y-m-d H:i:s');
|
|
|
+
|
|
|
+ $pay_code = make_no('ZF');
|
|
|
+
|
|
|
+ //支付信息表
|
|
|
+ $pay_info_id = Db::name('pay_info')->insertGetId([
|
|
|
+ 'pay_code' => $pay_code,
|
|
|
+ 'wx_openId' => $openId,
|
|
|
+ 'type' => $data['type'],
|
|
|
+ 'ids' => json_encode($data['list']),
|
|
|
+ 'status' => CommonModel::$order_status_wait_pay,
|
|
|
+ 'createrid' => self::$aid,
|
|
|
+ 'creater' => self::$aname,
|
|
|
+ 'addtime' => $date,
|
|
|
+ 'updaterid' => self::$aid,
|
|
|
+ 'updater' => self::$aname,
|
|
|
+ 'updatetime' => $date,
|
|
|
+ ]);
|
|
|
+
|
|
|
+
|
|
|
+ if ($data['type'] == CommonModel::$pay_type_service) {
|
|
|
+ //购买服务
|
|
|
+ $rs = ServiceModel::field('id,original_price,activity_price,title,activity_status')
|
|
|
+ ->whereIn('id', array_column($data['list'], 'id'))
|
|
|
+ ->where(['is_del' => CommonModel::$del_normal, 'status' => CommonModel::$status_normal])
|
|
|
+ ->select()
|
|
|
+ ->toArray();
|
|
|
+ if (empty($rs)) throw new Exception('购买的服务不存在或已下架');
|
|
|
+
|
|
|
+ $insert_service = [];
|
|
|
+ foreach ($rs as $item) {
|
|
|
+ //单价
|
|
|
+ if ($item['activity_status'] == ServiceModel::$activity_status_ing) $price = $rs['activity_price'];
|
|
|
+ else $price = $rs['original_price'];
|
|
|
+
|
|
|
+ $total_price = bcmul($price, $num[$item['id']] ?? 0, 2);//总价
|
|
|
+ $amount = bcadd($amount, $total_price, 2);//总金额
|
|
|
+
|
|
|
+ $insert_service[] = [
|
|
|
+ 'orderCode' => make_no('FW'),
|
|
|
+ 'pay_info_id' => $pay_info_id,
|
|
|
+ 'uid' => self::$aid,
|
|
|
+ 'service_id' => $item['id'],
|
|
|
+ 'num' => $num[$item['id']] ?? 0,
|
|
|
+ 'addr_id' => $data['addr_id'],
|
|
|
+ 'price' => $price,
|
|
|
+ 'total_price' => $total_price,
|
|
|
+ 'status' => CommonModel::$order_status_wait_pay,
|
|
|
+ 'is_del' => CommonModel::$del_normal,
|
|
|
+ 'createrid' => self::$aid,
|
|
|
+ 'creater' => self::$aname,
|
|
|
+ 'addtime' => $date,
|
|
|
+ 'updaterid' => self::$aid,
|
|
|
+ 'updater' => self::$aname,
|
|
|
+ 'updatetime' => $date,
|
|
|
+ ];
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //向服务订单表写数据
|
|
|
+ Db::name('order_service')->insertAll($insert_service);
|
|
|
+
|
|
|
+ } else {
|
|
|
+ //购买商城商品
|
|
|
+ $order_insert = [];
|
|
|
+ $rs = GoodModel::field('id,price,good_name')
|
|
|
+ ->whereIn('id', array_column($data['list'], 'id'))
|
|
|
+ ->where(['is_del' => CommonModel::$del_normal, 'status' => CommonModel::$status_normal])
|
|
|
+ ->select()
|
|
|
+ ->toArray();
|
|
|
+ if (empty($rs)) throw new Exception('购买的商品不存在或已禁用');
|
|
|
+
|
|
|
+ $i = 0;
|
|
|
+ $orderCode = make_no('QR');
|
|
|
+ $orderCode = substr($orderCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
|
|
|
+ foreach ($rs as $item) {
|
|
|
+
|
|
|
+ $total_price = bcmul($item['price'], $num[$item['id']] ?? 0, 2);//总价
|
|
|
+ $order_insert[] = [
|
|
|
+ 'orderCode' => $orderCode,
|
|
|
+ 'pay_info_id' => $pay_info_id,
|
|
|
+ 'company_id' => self::$company_id,
|
|
|
+ 'card_id' => self::$card_id,
|
|
|
+ 'uid' => self::$aid,
|
|
|
+ 'good_id' => $item['id'],
|
|
|
+ 'num' => $num[$item['id']],
|
|
|
+ 'price' => $item['price'],
|
|
|
+ 'total_price' => $total_price,
|
|
|
+ 'addr_id' => $data['addr_id'],
|
|
|
+ 'type' => CommonModel::$pay_type_shopping_good,
|
|
|
+ 'status' => CommonModel::$order_status_wait_pay,
|
|
|
+ 'is_del' => CommonModel::$del_normal,
|
|
|
+ 'createrid' => self::$aid,
|
|
|
+ 'creater' => self::$aname,
|
|
|
+ 'addtime' => $date,
|
|
|
+ 'updaterid' => self::$aid,
|
|
|
+ 'updater' => self::$aname,
|
|
|
+ 'updatetime' => $date,
|
|
|
+ ];
|
|
|
+ $amount = bcadd($amount, $total_price, 2);
|
|
|
+
|
|
|
+ $i++;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //获取预支付信息,
|
|
|
+ //@todo body要写什么????????
|
|
|
+ $result = WechatLogic::getPrepayId($openId, '', $pay_code, $amount);
|
|
|
+
|
|
|
+
|
|
|
+ //更新pay_info表
|
|
|
+ Db::name('pay_info')
|
|
|
+ ->where('id', $pay_info_id)
|
|
|
+ ->update([
|
|
|
+ 'prepay_info' => $result,
|
|
|
+ 'amount' => $amount,
|
|
|
+ 'expiretime' => date('Y-m-d H:i:s', time() + 5 * 60),
|
|
|
+ ]);
|
|
|
+
|
|
|
+
|
|
|
+ //@todo 要给前端返回什么信息?
|
|
|
+ $res = ['pay_code' => $pay_code];
|
|
|
+
|
|
|
+
|
|
|
+ Db::commit();
|
|
|
+ return json_show(CommonModel::$success, '微信支付预下单成功', $res);
|
|
|
+ } catch (Exception $exception) {
|
|
|
+ Db::rollback();
|
|
|
+ return json_show(CommonModel::$success, '微信支付预下单失败' . $exception->getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ //微信支付成功后的异步通知及订单处理
|
|
|
+ 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 == CommonModel::$order_status_wait_pay) {
|
|
|
+
|
|
|
+ $updatetime = date('Y-m-d H:i:s');
|
|
|
+
|
|
|
+ PayInfoModel::where(['pay_code' => $notifyInfo['out_trade_no'], 'status' => CommonModel::$order_status_wait_pay, 'id' => $pay_info->id])
|
|
|
+ ->save([
|
|
|
+ 'status' => CommonModel::$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' => CommonModel::$order_status_wait_pay])
|
|
|
+ ->update([
|
|
|
+ 'updaterid' => '0',
|
|
|
+ 'updater' => '微信支付',
|
|
|
+ 'updatetime' => $updatetime,
|
|
|
+ 'status' => CommonModel::$order_status_not_deliver
|
|
|
+ ]);
|
|
|
+
|
|
|
+
|
|
|
+ } else {
|
|
|
+ //服务订单
|
|
|
+ Db::name('order_service')
|
|
|
+ ->where(['is_del' => CommonModel::$del_normal, 'pay_info_id' => $pay_info->id, 'status' => CommonModel::$order_status_wait_pay])
|
|
|
+ ->update([
|
|
|
+ 'updaterid' => '0',
|
|
|
+ 'updater' => '微信支付',
|
|
|
+ 'updatetime' => $updatetime,
|
|
|
+ 'status' => CommonModel::$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'] == CommonModel::$order_status_wait_pay) {
|
|
|
+ //待支付时,手动去微信端查验是否支付完成(有可能异步通知到的晚,尚未更新状态)
|
|
|
+ $temp = WechatLogic::checkPayResult($pay_code);
|
|
|
+ $rs['status'] = $temp['RESULT_CODE'];//????@todo
|
|
|
+ }
|
|
|
+
|
|
|
+ return json_show(CommonModel::$success, '请求成功', ['result' => $rs['status']]);
|
|
|
+ }
|
|
|
+
|
|
|
+}
|