From ff37d8f17edb2bccb46051dd2325897dc54682d6 Mon Sep 17 00:00:00 2001 From: Hankin Date: Wed, 13 Aug 2025 13:53:48 +0800 Subject: [PATCH] transfer --- app/Http/Controllers/Controller.php | 8 +--- app/Services/WechatPayService.php | 67 ++++++++++++++++++++--------- routes/util/api.php | 1 - 3 files changed, 47 insertions(+), 29 deletions(-) diff --git a/app/Http/Controllers/Controller.php b/app/Http/Controllers/Controller.php index ba0c979..4bcfa0d 100644 --- a/app/Http/Controllers/Controller.php +++ b/app/Http/Controllers/Controller.php @@ -20,13 +20,7 @@ class Controller extends BaseController $openid = "oPC_2vneOWpQbicNZQAUCxuwZ4mw"; $amount = 100; $remark = "测试"; - $res = WechatPayService::officialUserTransfer($trade_no, $scene_id, $openid, $amount, $remark); - dd($res); - } - - public function getWechatplatformcCert() - { - $res = WechatPayService::getPlatformCert(); + $res = WechatPayService::transferBatches($trade_no, $scene_id, $openid, $amount, $remark); dd($res); } } diff --git a/app/Services/WechatPayService.php b/app/Services/WechatPayService.php index ee77039..5aec876 100644 --- a/app/Services/WechatPayService.php +++ b/app/Services/WechatPayService.php @@ -55,30 +55,11 @@ class WechatPayService return $instance; } - public function getPlatformCert() - { - try { - $instance = $this->newClient(); - $resp = $instance->chain('v3/certificates')->get( - /** @see https://docs.guzzlephp.org/en/stable/request-options.html#debug */ - // ['debug' => true] // 调试模式 - ); - return $resp->getBody(); - } catch (\Exception $e) { - // 进行异常捕获并进行错误判断处理 - Log::info($e->getMessage()); - if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) { - return $resp->getBody(); - } - return $e->getTraceAsString(); - } - } - /** - * 转账零钱 + * 商家转账 * @return void */ - public function officialUserTransfer(string $trade_no, string $scene_id, string $openid, int $amount, string $remark, array $transfer_scene_report_infos = []): array + public function mchTransfer(string $trade_no, string $scene_id, string $openid, int $amount, string $remark, array $transfer_scene_report_infos = []): array { // 发送请求 if (empty(count($transfer_scene_report_infos))) { @@ -124,6 +105,50 @@ class WechatPayService } } + /** + * 商家批量转账到零钱 + */ + + public function transferBatches(string $trade_no, string $scene_id, string $openid, int $amount, string $remark): array + { + try { + $appid = config("wechat.official_account.default.app_id"); + $data = [ + "appid" => $appid, + "out_bill_no" => $trade_no, + "transfer_scene_id" => (string) $scene_id, + "batch_name" => $remark, + "batch_remark" => $remark, + "total_amount" => $amount, + "total_num" => 1, + "transfer_detail_list" => [ + "out_detail_no" => $trade_no, + "transfer_amount" => $amount, + "transfer_remark" => $remark, + "openid" => $openid, + ] + ]; + Log::info("转账到零钱数据", $data); + $instance = $this->newClient(); + $resp = $instance->chain('v3/transfer/batche')->post([ + "json" => $data + ]); + $res = json_decode($resp->getBody(), true); + dd($res); + } catch (\Exception $e) { + // 进行异常捕获并进行错误判断处理 + Log::info($e->getMessage()); + if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) { + $r = $e->getResponse(); + $res = json_decode($r->getBody()); + return ["code" => 1, "err_msg" => $r->getBody()]; + } + + return ["code" => 1, "err_msg" => $e->getMessage()]; + } + } + + /** * 转账银行卡 * @return void diff --git a/routes/util/api.php b/routes/util/api.php index ca55c0a..53fa7bc 100644 --- a/routes/util/api.php +++ b/routes/util/api.php @@ -21,4 +21,3 @@ Route::prefix("transfer")->group(function () { Route::get("test", [Controller::class, "test"]); -Route::get("wechat/platform/cert", [Controller::class, "getWechatplatformcCert"]);