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

调用PhonePe退款API时遭遇Unauthenticated(未授权)错误的排查求助

调用PhonePe退款API时遭遇Unauthenticated(未授权)错误的排查求助

大家好,我最近碰到一个棘手的问题:调用PhonePe的退款API时始终返回Unauthenticated错误,但同一个token在发起支付请求时完全正常使用,实在摸不着头绪,想请教各位开发者帮忙排查一下。

我是基于Laravel框架开发的,支付流程完全没问题,但退款环节卡在这里了。以下是我当前的退款实现代码:

$data = $this->getAuthKey();
$payment = Payment::where('id', $paymentId)->first();
$token = $data['access_token']; 
// $token = $payment->auth_key; 我也试过用支付时存储的auth_key,结果一样报错

$url = 'https://api.phonepe.com/apis/pg/payments/v2/refund';
$amount = $payment->amount * 100; // 转换为分单位
$refund_id = 'REF' . $paymentId . uniqid();
$order_id = $payment->transaction_id;

$payload = [
    "merchantRefundId" => $refund_id,
    "originalMerchantOrderId" => $order_id,
    "amount" => $amount,
];

$response = Http::withHeaders([
    'Content-Type' => 'application/json',
    'Authorization' => 'O-Bearer ' . $token,
])->post($url, $payload);

$response = $response->json();
return $response;

我已经做了这些排查,但还是没解决问题:

  • 试过两种token来源:重新获取的access_token、支付时存储的auth_key,都返回未授权
  • 确认了退款API的URL是官方文档指定的正确地址
  • 核对过payload里的所有参数:退款ID格式合法、原订单ID和支付时的transaction_id一致、金额转换为分单位也没问题
  • 请求头的Authorization格式和支付请求完全一致(都是O-Bearer {token})

有没有朋友遇到过类似的情况?或者我是不是忽略了PhonePe退款API的特殊要求?比如退款需要额外的权限配置,或者token的权限范围和支付不一样?

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.08 09:24:32