123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- <?php
- namespace app\admin\controller\notify;
- use app\admin\service\notify\RechargeService;
- use app\model\Member;
- use app\model\MemberAccount;
- use app\model\Order;
- use app\model\PayDetail;
- use support\Db;
- use support\exception\BusinessException;
- use support\Request;
- class RechargeController
- {
- /**
- * @Desc 计划任务
- * @Author Gorden
- * @Date 2024/5/29 11:31
- *
- * @return void
- */
- // public function disposePaySuccess()
- // {
- //// $unix = strtotime(date("Y-m-d H:i",strtotime("-1 minute")).':00');
- // $payDetails = PayDetail::where('pay_category', 'RECHARGE')
- // ->where('pay_status', 'SUCCESS')
- // ->where(function ($query) {
- // $query->whereRaw("JSON_EXTRACT(`pay_extend_json`, '$.notify') IS NULL")
- // ->orWhereJsonDoesntContain('pay_extend_json->notify', 'success');
- // })
- // ->get()
- // ->toArray();
- // if (!$payDetails) {
- // return;
- // }
- //
- // foreach ($payDetails as $payDetail) {
- // $this->paySuccess($payDetail['pay_id']);
- // }
- // }
- /**
- * @Desc
- * @Author Gorden
- * @Date 2024/8/19 13:33
- *
- * @param Request $request
- * @return \support\Response
- */
- public function paySuccess(Request $request)
- {
- $data = $request->post();
- if (is_json($data)) {
- $data = json_decode($data, true);
- }
- _syslog("APP充值", "APP充值回调开始", $data, $data, 1001);
- if (!$data['pay_id']) {
- return json_fail('参数异常');
- }
- try {
- $result = $this->disposePaySuccess($data['pay_id']);
- _syslog("APP充值", "APP处理完成", $data, $data, 1001);
- return $result;
- } catch (BusinessException $e) {
- _syslog("APP充值", "APP充值失败", $data, $data, 1001);
- return json_fail("处理失败");
- } catch (\Exception $e) {
- _syslog("APP充值", "APP充值失败", $data, $data, 1001);
- return json_fail("处理失败");
- }
- // public function paySuccess($id)
- // {
- // if (!$id) {
- // return json_fail('参数异常');
- // }
- //
- // $payDetail = PayDetail::find($id);
- // if ($payDetail->pay_status != 'SUCCESS') {
- // return json_fail("支付状态异常");
- // }
- }
- /**
- * @param $payId
- * @return \support\Response
- * @throws BusinessException
- */
- public function disposePaySuccess($payId)
- {
- $payDetail = PayDetail::find($payId);
- if (!$payDetail || $payDetail->pay_status != 'SUCCESS') {
- throw new BusinessException("支付状态异常");
- // return json_fail("支付状态异常");
- }
- $extendJson = [];
- if (!empty($payDetail->pay_extend_json)) {
- $extendJson = json_decode($payDetail->pay_extend_json, true);
- if (isset($extendJson['notify']) && $extendJson['notify'] == 'success') {
- return json_success('处理完成');
- }
- }
- Db::beginTransaction();
- try {
- $extendJson['notify'] = 'success';
- $payDetail->pay_extend_json = json_encode($extendJson);
- $payDetail->save();
- // 赠送比例
- $objectJson = json_decode($payDetail->join_pay_object_json, true);
- $addedNbr = 0;
- if (isset($objectJson['order_id'])) {
- // 对应订单设置已完成
- Order::where('order_groupby', $payDetail->join_pay_order_id)->update(['order_status_system' => 'DONE', 'order_is_complete' => 'Y', 'order_status_storage' => 'DONE']);
- $addedNbr = RechargeService::disposeOrder($objectJson['order_id']);
- // throw new BusinessException("支付数据异常");
- // return json_fail("");
- }
- // 赠送金额累加到 账户表 member_account_added
- $memberAccount = MemberAccount::where('join_account_member_id', $payDetail->join_pay_member_id)
- ->where('member_account_classify', 'CASH')
- ->first();
- $payAmount = floatval($payDetail->pay_amount);
- $added = $memberAccount->member_account_added + $payAmount * $addedNbr;
- $income = $memberAccount->member_account_income + $payAmount;
- $memberAccount->member_account_added = $added;
- $memberAccount->member_account_income = $income;
- $memberAccount->member_account_surplus = $memberAccount->member_account_surplus + $payAmount;
- $memberAccount->save();
- // 根据最新的数据,更新用户等级
- // 临时屏蔽该会员升级
- // if (!in_array($payDetail->join_pay_member_id, ['MR2409132254Z9VW'])) {
- // $member = Member::find($payDetail->join_pay_member_id);
- // $roleId = RechargeService::disposeRole($payDetail->join_pay_member_id, $payDetail->pay_amount);
- // if ($roleId && $member->join_member_role_id != $roleId) {
- // $member->join_member_role_id = $roleId;
- // $member->save();
- // // 处理增值套包、组件
- // RechargeService::disposeAdded($roleId, $payDetail->join_pay_member_id);
- // }
- // }
- Db::commit();
- return json_success('success');
- } catch (BusinessException $e) {
- dump($e->getMessage());
- Db::rollBack();
- throw new BusinessException("处理失败");
- // return json_fail('处理失败');
- } catch (\Exception $e) {
- dump($e->getMessage());
- Db::rollBack();
- throw new BusinessException("处理失败");
- // return json_fail('处理失败');
- }
- }
- }
|