yansongda / laravel-pay

可能是我用过的最优雅的 Alipay/WeChat/Unipay 的 laravel 支付扩展包了

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

laravel10.10在队列中执行代码时的问题

houguang opened this issue · comments

job代码

<?php

namespace App\Jobs;

use App\Models\CashOutRecordsModel;
use app\Traits\MiniTrait;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Yansongda\LaravelPay\Facades\Pay;
use Yansongda\Pay\Exception\ContainerException;
use Yansongda\Pay\Exception\InvalidParamsException;
use Yansongda\Pay\Plugin\Wechat\Fund\Transfer\QueryBatchIdPlugin;

/**
 * 查询支付结果
 */
class GetTransferBatchJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    use MiniTrait;

    private string $batch_id;
    private Collection $collection;

    public function __construct(string $batch_id, Collection $collection)
    {
        $this->batch_id = $batch_id;
        $this->collection = $collection;
    }

    public function handle(): void
    {
        //转账查询
        try {
            $plugins = Pay::wechat()->mergeCommonPlugins([QueryBatchIdPlugin::class]);
            $res = Pay::wechat()->pay($plugins, [
                "batch_id"          => $this->batch_id,
                "need_query_detail" => "true",
                "offset"            => 0,
                "limit"             => 100,
                "detail_status"     => "ALL"
            ]);
            $this->collection->map(function (CashOutRecordsModel $model) use ($res) {
                foreach ($res["transfer_detail_list"] as $item) {
                    if ($model->out_detail_no == $item["out_detail_no"]) {
                        $model->detail_status = $item["detail_status"];
                        $model->save();
                    }
                }
            });
        } catch (ContainerException|InvalidParamsException $e) {
            Log::error("转账查询失败", [$e->getMessage(), $e->getTrace()]);
        }
    }
}

执行结果始终报错

Client error: `GET https://api.mch.weixin.qq.com/v3/transfer/batches/batch-id/131000506006701239776512023072531814997229` resulted in a `400 Bad Request` response:
{"code":"PARAM_ERROR","detail":{"location":"uri_template","value":""},"message":"输入源“/uri_template/need_query_de (truncated...)

在控制器中执行代码

<?php

namespace App\Http\Controllers;

use app\Traits\MiniTrait;
use Yansongda\LaravelPay\Facades\Pay;
use Yansongda\Pay\Exception\ContainerException;
use Yansongda\Pay\Exception\InvalidParamsException;
use Yansongda\Pay\Plugin\Wechat\Fund\Transfer\QueryBatchDetailIdPlugin;
use Yansongda\Pay\Plugin\Wechat\Fund\Transfer\QueryBatchIdPlugin;

class Test
{
    use MiniTrait;
    public function index()
    {
        //转账查询
        try {
            $plugins = Pay::wechat()->mergeCommonPlugins([QueryBatchIdPlugin::class]);
            return Pay::wechat()->pay($plugins, [
                "batch_id"          => "131000406095101239776512023072548685505085",
                "need_query_detail" => "true",
                "offset"            => 0,
                "limit"             => 100,
                "detail_status"     => "ALL"
            ]);
        } catch (ContainerException|InvalidParamsException $e) {
            return $e->getMessage();
        }
    }
}

返回结果正常

{
    "limit": 100,
    "offset": 0,
    "transfer_batch": {
        "appid": "wx8bca0bcd47e59801",
        "batch_id": "131000406095101239776512023072548685123123",
        "batch_name": "同意佣金批量提现",
        "batch_remark": "佣金提现",
        "batch_status": "FINISHED",
        "batch_type": "API",
        "create_time": "2023-07-25T23:10:14+08:00",
        "fail_amount": 2,
        "fail_num": 2,
        "mchid": "160300****",
        "out_batch_no": "20230725231014FjkXDYUPWZ",
        "success_amount": 0,
        "success_num": 0,
        "total_amount": 2,
        "total_num": 2,
        "transfer_scene_id": "1001",
        "update_time": "2023-07-25T23:10:24+08:00"
    },
    "transfer_detail_list": [
        {
            "detail_id": "13200040609510123977651202307250494123123",
            "detail_status": "FAIL",
            "out_detail_no": "20230725231014n0NiZ5CpXy"
        },
        {
            "detail_id": "132000406095101239776512023072504946123123",
            "detail_status": "FAIL",
            "out_detail_no": "20230725231014Xm67LeMmQz"
        }
    ]
}

同样的代码,是什么原因导致在job队列中报错呢?