You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel集成MercadoPago遇application_fee支付错误求助

Laravel集成MercadoPago支付错误:"You cannot use application_fee with this payment"

问题情况

处理支付时触发错误:

"You cannot use application_fee with this payment."

官方文档说明该错误是因使用的Access Token未通过OAuth获取,但已完成OAuth流程获取Token,仍出现此问题。

相关代码与配置

授权路由 /connect-mercadopago

$codeVerifier = bin2hex(random_bytes(32));
session(['mp_code_verifier' => $codeVerifier]);

// Generate code challenge
$codeChallenge = rtrim(strtr(base64_encode(hash('sha256', $codeVerifier, true)), '+/', '-_'), '=');

// auth params
$queryParams = http_build_query([
    'response_type' => 'code',
    'client_id' => env('MERCADOPAGO_CLIENT_ID'),
    'redirect_uri' => url('/callback-mercadopago'),
    'code_challenge' => $codeChallenge,
    'code_challenge_method' => 'S256',
]);

return redirect(env('MERCADOPAGO_GETAUTH') . $queryParams);

MERCADOPAGO_GETAUTH=https://auth.mercadopago.com/authorization?

OAuth回调函数 callBackToken

public function callBackToken(Request $request): void
{
    $code = request('code');
    $codeVerifier = session('mp_code_verifier');

    if (!$code || !$codeVerifier) {
        echo "Error: Missing Params.";
        return;
    }

    $postData = [
        'client_id' => env('MERCADOPAGO_CLIENT_ID'),
        'client_secret' => env('MERCADOPAGO_CLIENT_SECRET'),
        'grant_type' => 'authorization_code',
        'code' => $code,
        'redirect_uri' => env('REDIRECT_URL'),
        'code_verifier' => $codeVerifier,
    ];

    $response = Http::asForm()->post(env('MERCADOPAGO_OAUTH'), $postData);

    if ($response->failed() || $response->status() >= 400) {
        echo "Error: " . $response->body();
        return;
    }

    $responseData = $response->json();

    if (!is_array($responseData) || !isset($responseData['access_token'], $responseData['refresh_token'], $responseData['expires_in'])) {
        echo "Error: " . $response;       
    }

    // **SAVE**
    $data = [
        'user_id' => auth()->id(),
        'encrypted_access_token' => encrypt($responseData['access_token']),
        'encrypted_refresh_token' => encrypt($responseData['refresh_token']),
        'expires_at' => now()->addSeconds($responseData['expires_in']),
        'public_key' => encrypt($responseData['public_key'])
    ];
    $request->user()->Seller()->create($data);

    echo json_encode($responseData);
}

MERCADOPAGO_OAUTH=https://api.mercadopago.com/oauth/token

排查与解决方向

1. 补充OAuth授权范围(Scope)

当前授权请求未指定scope参数,MercadoPago默认授权可能不包含收取application_fee所需的权限。需在授权URL的queryParams中添加对应权限,例如:

$queryParams = http_build_query([
    // ... 原有参数
    'scope' => 'read,write,marketplace', // 需包含marketplace相关权限
]);

具体权限需根据MercadoPago官方要求选择,确保包含允许平台收取服务费的权限。

2. 验证Token权限

使用获取到的access_token调用https://api.mercadopago.com/users/me接口,检查返回的scope字段是否包含所需权限。若缺少,需重新发起授权并指定正确的scope。

3. 确认账户类型

确保你的MercadoPago账户是Marketplace类型,个人卖家账户不支持application_fee参数,只有平台账户才能使用该功能收取服务费。

4. 检查支付请求参数

确认创建支付时application_fee的使用符合规则:

  • 金额需为合法数值,且不超过支付总金额的合理比例
  • 参数需放在正确的层级(例如在transaction_details下,或根据MercadoPago API版本的要求放置)

内容的提问来源于stack exchange,提问作者tota2000

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.13 17:10:18