From 5aca2d411929bbf124a42793734458c059523e13 Mon Sep 17 00:00:00 2001 From: panliang <1163816051@qq.com> Date: Fri, 13 Oct 2023 09:50:43 +0800 Subject: [PATCH] order profit wechat share --- app/Admin/Controllers/CouponController.php | 6 +-- app/Casts/Price.php | 1 - app/Console/Commands/OrderProfitCheck.php | 60 ++++++++++++++++++++++ app/Console/Kernel.php | 1 + app/Models/Coupon.php | 2 +- app/Services/DistributeService.php | 2 +- app/Services/Payment/WxpayService.php | 25 ++++++--- tests/Feature/ExampleTest.php | 6 ++- 8 files changed, 88 insertions(+), 15 deletions(-) create mode 100644 app/Console/Commands/OrderProfitCheck.php diff --git a/app/Admin/Controllers/CouponController.php b/app/Admin/Controllers/CouponController.php index 8cd45c2a..b4dc670c 100644 --- a/app/Admin/Controllers/CouponController.php +++ b/app/Admin/Controllers/CouponController.php @@ -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('设置时间后,期限字段失效。'); } diff --git a/app/Casts/Price.php b/app/Casts/Price.php index adb848d8..ff2067ac 100644 --- a/app/Casts/Price.php +++ b/app/Casts/Price.php @@ -38,7 +38,6 @@ class Price implements CastsAttributes if ((string) $value === '') { return null; } - return (int) bcmul($value, 100); } } diff --git a/app/Console/Commands/OrderProfitCheck.php b/app/Console/Commands/OrderProfitCheck.php new file mode 100644 index 00000000..3f290762 --- /dev/null +++ b/app/Console/Commands/OrderProfitCheck.php @@ -0,0 +1,60 @@ +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; + } +} diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 762e9ac6..25e83bf7 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -24,6 +24,7 @@ class Kernel extends ConsoleKernel */ protected function schedule(Schedule $schedule) { + $schedule->command('order-profit:check')->daily(); } /** diff --git a/app/Models/Coupon.php b/app/Models/Coupon.php index 03e0086f..756a5101 100644 --- a/app/Models/Coupon.php +++ b/app/Models/Coupon.php @@ -17,7 +17,7 @@ class Coupon extends Model protected $casts = [ 'amount' => Price::class, - 'threshold'=>Price::class, + 'threshold' => Price::class, ]; /** diff --git a/app/Services/DistributeService.php b/app/Services/DistributeService.php index 81703a78..8f839b31 100644 --- a/app/Services/DistributeService.php +++ b/app/Services/DistributeService.php @@ -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" => "推荐返利" diff --git a/app/Services/Payment/WxpayService.php b/app/Services/Payment/WxpayService.php index 45f81530..64242590 100644 --- a/app/Services/Payment/WxpayService.php +++ b/app/Services/Payment/WxpayService.php @@ -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); } /** diff --git a/tests/Feature/ExampleTest.php b/tests/Feature/ExampleTest.php index 9aea456a..5aebe412 100644 --- a/tests/Feature/ExampleTest.php +++ b/tests/Feature/ExampleTest.php @@ -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); } }