2 changed files with 163 additions and 0 deletions
@ -0,0 +1,131 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Http\Controllers; |
||||
|
|
||||
|
use App\Common\OrderStatus; |
||||
|
use App\Common\PayType; |
||||
|
use App\Models\AdminSetting; |
||||
|
use App\Models\IndustryOrder; |
||||
|
use App\Models\IndustryPayLog; |
||||
|
use App\Models\IndustryProduct; |
||||
|
use EasyWeChat\Factory; |
||||
|
use EasyWeChat\Kernel\Exceptions\Exception; |
||||
|
use EasyWeChat\Payment\Kernel\Exceptions\InvalidSignException; |
||||
|
use Illuminate\Support\Facades\DB; |
||||
|
|
||||
|
/** |
||||
|
* 行业产品微信支付回调 |
||||
|
* Class IndustryProductWxpay |
||||
|
* @package App\Http\Controllers |
||||
|
*/ |
||||
|
class IndustryProductWxpay |
||||
|
{ |
||||
|
public function notify() |
||||
|
{ |
||||
|
$setting = AdminSetting::val(['payee_appid', 'payee_mchid', 'payee_mchkey']); |
||||
|
if (!isset($setting['payee_appid'], $setting['payee_mchid'], $setting['payee_mchkey'])) { |
||||
|
return '获取系统配置失败'; |
||||
|
} |
||||
|
|
||||
|
$config = [ |
||||
|
'app_id' => $setting['payee_appid'], |
||||
|
'mch_id' => $setting['payee_mchid'], |
||||
|
'key' => $setting['payee_mchkey'], |
||||
|
]; |
||||
|
|
||||
|
$app = Factory::payment($config); |
||||
|
try { |
||||
|
$response = $app->handlePaidNotify(function ($message, $fail) { |
||||
|
//仅测试用,回调记录
|
||||
|
DB::table('pay_debugs')->insert(['agent_id' => 0, 'type' => 1, 'content' => json_encode($message)]); |
||||
|
|
||||
|
// 请求成功
|
||||
|
if ($message['return_code'] === 'SUCCESS') { |
||||
|
//主要是为了区分定金支付和尾款支付,订单号带有-status后缀,分割后前面才是真正的订单号
|
||||
|
$order_no = explode('-', $message['out_trade_no'])[0]; |
||||
|
$order = IndustryOrder::query() |
||||
|
->where(['order_no' => $order_no]) |
||||
|
->first(); |
||||
|
|
||||
|
//已经处理过的订单直接返回true
|
||||
|
if ($order && in_array($order->status, [OrderStatus::PAID, OrderStatus::PAID_RETAINAGE, OrderStatus::SUCCESS])) { |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
//判断该微信支付订单号有没有处理过
|
||||
|
$exist_log = IndustryPayLog::where([ |
||||
|
'agent_id' => $order->agent_id, |
||||
|
'supplier_id' => $order->supplier_id, |
||||
|
'industry_order_id' => $order->id, |
||||
|
'type' => 1, |
||||
|
'transaction_id' => $message['transaction_id'], |
||||
|
])->first(); |
||||
|
if ($exist_log) { |
||||
|
return true; |
||||
|
} |
||||
|
|
||||
|
// 支付成功
|
||||
|
if ($message['result_code'] === 'SUCCESS') { |
||||
|
DB::beginTransaction(); |
||||
|
try { |
||||
|
//增加销量,库存在拍下时已经减了
|
||||
|
IndustryProduct::query() |
||||
|
->where('id', $order->product_id) |
||||
|
->increment('sale', $order->num); |
||||
|
|
||||
|
$old_status = $order->status; |
||||
|
$pay_type = $order->pay_type; |
||||
|
$money = $message['total_fee'] / 100; |
||||
|
//定金支付和首付款支付
|
||||
|
if (in_array($pay_type, [PayType::DEPOSIT_PAY, PayType::EARNEST_PAY, PayType::DOWN_PAYMENT])) { |
||||
|
if ($old_status == OrderStatus::UNPAID) { |
||||
|
$order->status = OrderStatus::PAY_EARNEST; |
||||
|
} else if ($old_status == OrderStatus::PAY_EARNEST) { |
||||
|
$order->status = OrderStatus::PAID_RETAINAGE; |
||||
|
$order->verify_code = uniqid(); //生成核销码
|
||||
|
} |
||||
|
} else if ($pay_type == PayType::ONLINE) { |
||||
|
$order->status = OrderStatus::PAID; |
||||
|
$order->verify_code = uniqid(); //生成核销码
|
||||
|
} |
||||
|
|
||||
|
$order->paid_at = now(); |
||||
|
$order->paid_money = DB::raw('`paid_money` + ' . $money); |
||||
|
$order->timeout = null; |
||||
|
$order->save(); |
||||
|
|
||||
|
//资金流水
|
||||
|
IndustryPayLog::create([ |
||||
|
'user_id' => $order->user_id, |
||||
|
'agent_id' => $order->agent_id, |
||||
|
'money' => $money, |
||||
|
'industry_order_id' => $order->id, |
||||
|
'type' => 1, |
||||
|
'desc' => DB::raw("LEFT('购买产品:{$order->title}', 250)"), |
||||
|
'transaction_id' => $message['transaction_id'], //微信支付订单号
|
||||
|
'created_at' => now(), //模型没有updated_at,无法自动写入时间
|
||||
|
'out_trade_no' => $message['out_trade_no'] ?? '', |
||||
|
]); |
||||
|
|
||||
|
DB::commit(); |
||||
|
return true; |
||||
|
} catch (Exception $e) { |
||||
|
DB::rollBack(); |
||||
|
$fail('Unknown error'); |
||||
|
} |
||||
|
} // 支付失败
|
||||
|
else if ($message['result_code'] === 'FAIL') { |
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
// 希望微信重试
|
||||
|
$fail('Unknown error 2'); |
||||
|
}); |
||||
|
} catch (InvalidSignException | Exception $e) { |
||||
|
return 'error' . $e->getFile() . $e->getMessage() . $e->getLine(); |
||||
|
} |
||||
|
|
||||
|
return $response; |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,32 @@ |
|||||
|
<?php |
||||
|
|
||||
|
namespace App\Models; |
||||
|
|
||||
|
use Illuminate\Database\Eloquent\Factories\HasFactory; |
||||
|
|
||||
|
class IndustryPayLog extends BaseModel |
||||
|
{ |
||||
|
use HasFactory; |
||||
|
protected $guarded = ['id']; //不允许修改的字段
|
||||
|
|
||||
|
public function __construct(array $attributes = []) |
||||
|
{ |
||||
|
parent::__construct($attributes); |
||||
|
$this->timestamps = false; |
||||
|
} |
||||
|
|
||||
|
public function agent() |
||||
|
{ |
||||
|
return $this->belongsTo(Agent::class)->withTrashed(); |
||||
|
} |
||||
|
|
||||
|
public function supplier() |
||||
|
{ |
||||
|
return $this->belongsTo(Supplier::class)->withTrashed(); |
||||
|
} |
||||
|
|
||||
|
public function industryOrder() |
||||
|
{ |
||||
|
return $this->belongsTo(IndustryOrder::class); |
||||
|
} |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue