| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- <?php
- namespace App\Http\Controllers\Api;
- use App\Models\PaymentLogModel;
- use App\Models\User;
- use App\Models\UserVipLogModel;
- use App\Services\PayService;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use PHPUnit\Util\Exception;
- use Yansongda\Pay\Pay;
- class PayNotifyController extends Controller
- {
- /**
- * 微信支付回调
- */
- public function wx_notify(){
- $wxpay = Pay::wechat(PayService::wx_config());
- DB::beginTransaction();
- try{
- $data = $wxpay->verify();
- Log::info($data);
- $order_no = $data->out_trade_no;
- $this->order_do_sth($order_no);
- DB::commit();
- } catch (\Exception $e) {
- DB::rollBack();
- Log::info($e->getMessage());
- }
- return $wxpay->success();
- }
- /**
- * 支付宝支付回调
- */
- public function ali_notify(){
- $alipay = Pay::alipay(PayService::ali_config());
- DB::beginTransaction();
- try{
- $data = $alipay->verify();
- Log::info($data);
- $order_no = $data->out_trade_no;
- $this->order_do_sth($order_no);
- DB::commit();
- } catch (\Exception $e) {
- DB::rollBack();
- Log::info($e->getMessage());
- }
- return $alipay->success();
- }
- /**
- * 支付回调业务处理
- */
- public function order_do_sth($order_no){
- $order = PaymentLogModel::query()->where(['order_no'=>$order_no])->first();
- if(!$order){
- throw new Exception("订单不存在");
- }
- if($order['status']==1){
- return true;
- }
- //修改订单状态
- $order->status = 1;
- $order->save();
- $vip_info = json_decode($order->content,true);
- //修改用户vip状态
- $user = User::query()->where(['id'=>$order->user_id])->first();
- if($user->is_vip==0){
- $user->is_vip = 1;
- $user->save();
- }
- //变更vip记录
- $user_vip_log = UserVipLogModel::query()->where(['user_id'=>$order->user_id])->first();
- if(!$user_vip_log){
- UserVipLogModel::query()->create([
- 'user_id'=>$order->user_id,
- 'status'=>1,
- 'day'=>$vip_info['day'],
- 'end_day'=> date("Y-m-d H:i:s",strtotime("+".$vip_info['day']." day")),
- ]);
- }elseif($user_vip_log->status==1){
- $user_vip_log->end_day = date("Y-m-d H:i:s",strtotime($user_vip_log->end_day."+".$vip_info['day']." day"));
- $user_vip_log->save();
- }elseif ($user_vip_log->status==0){
- $user_vip_log->end_day = date("Y-m-d H:i:s",strtotime("+".$vip_info['day']." day"));
- $user_vip_log->status = 1;
- $user_vip_log->save();
- }
- }
- }
|