6
0
Fork 0

order profit wechat share

base
panliang 2023-10-13 09:50:43 +08:00
parent a29c9ccabf
commit 5aca2d4119
8 changed files with 88 additions and 15 deletions

View File

@ -116,10 +116,10 @@ class CouponController extends AdminController
if ($form->isCreating() || ($form->isEditing() && !$form->model()->hasReceived())) {
$form->radio('type')
->when(1, function (Form $form) {
$form->currency('amount1')->symbol('¥')->help('例100.00表示优惠100元。')->value($form->model()->amount);
$form->number('amount1')->min(0)->help('例100 表示优惠100元。')->value($form->model()->amount);
})
->when(2, function (Form $form) {
$form->currency('amount2')->symbol('%')->help('例0.95表示95折。')->value($form->model()->amount);
$form->number('amount2')->min(0)->help('例0.95 表示95折。')->value($form->model()->amount);
})
->options([
1 =>'抵扣券',
@ -127,7 +127,7 @@ class CouponController extends AdminController
])->default(1);
$form->hidden('amount');
$form->currency('threshold')->symbol('¥')->default(0);
$form->number('threshold')->min(0)->default(0);
$form->number('use_day')->min(0)->default(1)->help('单位:天;指领取后几天内有效。');
$form->datetimeRange('use_start_at', 'use_end_at', '使用时间')->help('设置时间后,期限字段失效。');
}

View File

@ -38,7 +38,6 @@ class Price implements CastsAttributes
if ((string) $value === '') {
return null;
}
return (int) bcmul($value, 100);
}
}

View File

@ -0,0 +1,60 @@
<?php
namespace App\Console\Commands;
use App\Models\Order;
use App\Models\OrderProfit;
use App\Services\DistributeService;
use App\Services\Payment\WxpayService;
use Illuminate\Console\Command;
class OrderProfitCheck extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'order-profit:check';
/**
* The console command description.
*
* @var string
*/
protected $description = '检查提成支付结果';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$service = new WxpayService();
$service1 = new DistributeService();
$list = OrderProfit::where('status', 1)->get()->groupBy('order_id');
foreach ($list as $order_id => $profits) {
$transaction_id = Order::where('id', $order_id)->value('out_trade_no');
$sn = data_get($profits->first(), 'pay_no');
if ($transaction_id) {
$result = $service->queryShare($transaction_id, $sn);
$status = data_get($result, 'status');
if ($status == 'FINISHED') {
$service1->success($profits, $result);
}
}
}
return 0;
}
}

View File

@ -24,6 +24,7 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule)
{
$schedule->command('order-profit:check')->daily();
}
/**

View File

@ -17,7 +17,7 @@ class Coupon extends Model
protected $casts = [
'amount' => Price::class,
'threshold'=>Price::class,
'threshold' => Price::class,
];
/**

View File

@ -232,7 +232,7 @@ class DistributeService
}
$item->update(['pay_way' => PayWay::WxPayShare, 'pay_no' => $sn, 'status' => 1]);
array_push($receivers, [
"type" => "PERSONAL_OPENID",
"type" => "PERSONAL_SUB_OPENID",
"account" => $openid,
"amount" => $item->money * 100,
"description" => "推荐返利"

View File

@ -109,9 +109,14 @@ class WxpayService
*/
public function share(string $order_sn, string $share_sn, array $receivers): mixed
{
$config = config('wechat.payment.transfer');
$app = Factory::payment($config);
$app = $this->payment();
// 服务商模式 (子商户)
$appId = config('wechat.payment.sub.app_id');
$mchId = config('wechat.payment.sub.mch_id');
if ($appId && $mchId) {
$app->setSubMerchant($mchId, $appId);
}
// 添加分账接收方
foreach ($receivers as $receiver) {
$app->profit_sharing->addReceiver([
@ -131,16 +136,22 @@ class WxpayService
/**
* 微信支付-查询分账结果
*
* @param string $order_sn
* @param string $transaction_id
* @param string $share_sn
* @return mixed
*/
public function queryShare(string $order_sn, string $share_sn)
public function queryShare(string $transaction_id, string $share_sn)
{
$config = config('wechat.payment.transfer');
$app = Factory::payment($config);
$app = $this->payment();
// 服务商模式 (子商户)
$appId = config('wechat.payment.sub.app_id');
$mchId = config('wechat.payment.sub.mch_id');
return $app->profit_sharing->query($order_sn, $share_sn);
if ($appId && $mchId) {
$app->setSubMerchant($mchId, $appId);
}
return $app->profit_sharing->query($transaction_id, $share_sn);
}
/**

View File

@ -3,6 +3,7 @@
namespace Tests\Feature;
use App\Services\DistributeService;
use App\Services\Payment\WxpayService;
use Illuminate\Support\Facades\DB;
use Tests\TestCase;
use App\Models\Order;
@ -16,7 +17,7 @@ class ExampleTest extends TestCase
*/
public function test_example()
{
// $order = Order::where('sn', '20230813185220618108')->first();
// $order = Order::findOrFail(3146);
// try {
// DB::beginTransaction();
// (new DistributeService())->wechatShare($order);
@ -26,7 +27,8 @@ class ExampleTest extends TestCase
// DB::rollBack();
// report($e);
// }
dump(route('wxpay.paid_notify', ['payment' => 'default']));
$result = (new WxpayService())->queryShare('4200002029202310133548206841', '20231013084421898946');
dump($result);
$this->assertTrue(true);
}
}