84 lines
2.6 KiB
PHP
84 lines
2.6 KiB
PHP
<?php
|
||
/**
|
||
* Created by PhpStorm.
|
||
* User: sunny
|
||
* Date: 2019/2/21
|
||
* Time: 3:21 PM
|
||
*/
|
||
|
||
namespace App\Api\Controllers;
|
||
|
||
use App\Events\VipPaid;
|
||
use App\Models\Payment;
|
||
use App\Models\VipPament;
|
||
|
||
class CallbackController
|
||
{
|
||
/**
|
||
* 订单付款回掉地址。
|
||
* @param
|
||
* @return array
|
||
*/
|
||
|
||
public function index()
|
||
{
|
||
$app = app('wechat.payment.mini');
|
||
$response = $app->handlePaidNotify(function ($message, $fail) {
|
||
$payment = Payment::where('trade_no', $message['out_trade_no'])->first();
|
||
if (!$payment || $payment->paid_at) {
|
||
return true;
|
||
}
|
||
if ($message['return_code'] === 'SUCCESS') {
|
||
// return_code 表示通信状态,不代表支付状态
|
||
// 用户是否支付成功
|
||
if (array_get($message, 'result_code') === 'SUCCESS') {
|
||
$payment->state = 'SUCCESS';
|
||
$payment->paid_at = $message['time_end'];
|
||
} elseif (array_get($message, 'result_code') === 'FAIL') {
|
||
$payment->state = 'INIT';
|
||
}
|
||
} else {
|
||
return $fail('通信失败,请稍后再通知我');
|
||
}
|
||
$payment->save(); // 保存订单
|
||
$payment->order->paid();
|
||
return true; // 返回处理完成
|
||
});
|
||
|
||
return $response;
|
||
}
|
||
|
||
/**
|
||
* 开通vip,付款回掉地址。
|
||
* @param
|
||
* @return array
|
||
*/
|
||
public function vip()
|
||
{
|
||
$payment = app('wechat.payment.mini');
|
||
$response = $payment->handlePaidNotify(function ($message, $fail) {
|
||
$vip_payment = VipPament::where('trade_no', $message['out_trade_no'])->first();
|
||
if (!$vip_payment || $vip_payment->paid_at) {
|
||
return true;
|
||
}
|
||
if ($message['return_code'] === 'SUCCESS') {
|
||
// return_code 表示通信状态,不代表支付状态
|
||
// 用户是否支付成功
|
||
if (array_get($message, 'result_code') === 'SUCCESS') {
|
||
$vip_payment->state = 'SUCCESS';
|
||
$vip_payment->paid_at = $message['time_end'];
|
||
} elseif (array_get($message, 'result_code') === 'FAIL') {
|
||
$vip_payment->state = 'INIT';
|
||
}
|
||
} else {
|
||
return $fail('通信失败,请稍后再通知我');
|
||
}
|
||
$vip_payment->save(); // 保存订单
|
||
event(new VipPaid($vip_payment));
|
||
return true; // 返回处理完成
|
||
});
|
||
|
||
return $response;
|
||
}
|
||
|
||
} |