Login With PayPal scopes功能异常,请求处理失败求助
问题场景
我正在开发一个支持卖家服务交易的电商平台,仅使用PayPal作为支付网关。为存储卖家的PayPal账户邮箱、账户ID等信息(方便客户购买服务时将款项转至其账户),计划采用Login With PayPal实现:
- 仅使用
openid profile email这些scopes时,点击登录按钮跳转至sandbox登录页,输入账号密码后可正常获取授权code; - 添加
https://uri.paypal.com/services/paypalattributes这个scope(用于获取账户验证状态和PayPal账户ID)时,会收到“请求无法处理,请重试”的错误,无法跳转到包含授权code的回调URL,多次尝试均无效。
使用Sandbox环境,Next.js中的Login With PayPal按钮代码如下:
<Script src="https://www.paypalobjects.com/js/external/api.js" onReady={() => { paypal.use( ['login'], function (login) { login.render ({ "appid":"...", "authend":"sandbox", "scopes":"openid profile email https://uri.paypal.com/services/paypalattributes", "containerid":"lippButton", "responseType":"code", "locale":"en-us", "buttonType":"LWP", "buttonShape":"pill", "buttonSize":"lg", "fullPage":"true", "returnurl":"http://127.0.0.1:3000/api/paypal/callback" }) }) }}/>
已在PayPal账户设置中勾选Account verification status和PayPal account ID (payer ID)权限,通过开发者工具获取的错误返回:
{ "errors": [ { "message": "Invalid request during get consent content", "locations": [ { "line": 3, "column": 5 } ], "path": [ "connect", "getConsent" ], "extensions": { "name": "INVALID_REQUEST", "details": [] } } ], "data": { "connect": { "getConsent": null, "__typename": "ConnectQuery" } }, "extensions": { "tracing": { "version": 1, "startTime": "2022-10-07T01:54:28.831Z", "endTime": "2022-10-07T01:54:29.053Z", "duration": 221889707, "execution": { "resolvers": [ { "path": [ "connect" ], "parentType": "Query", "fieldName": "connect", "returnType": "ConnectQuery", "startOffset": 1755903, "duration": 28116203 }, { "path": [ "connect", "getConsent" ], "parentType": "ConnectQuery", "fieldName": "getConsent", "returnType": "ConsentEvaluationResponse", "startOffset": 30085850, "duration": 188656843 } ] } }, "correlationId": "..." } }
排查与解决步骤
修复Scope格式错误:代码中
scopes字段存在换行,PayPal要求scope参数为空格分隔的单行字符串,需去掉换行,修改为:"scopes":"openid profile email https://uri.paypal.com/services/paypalattributes",确认应用权限配置:确保是在对应Sandbox应用的设置中启用权限,而非主账户设置。进入PayPal开发者平台,找到目标Sandbox应用,在
Products & Services下的Login with PayPal模块中,确认Account verification status和PayPal account ID (payer ID)已启用并保存。验证回调URL有效性:检查回调URL
http://127.0.0.1:3000/api/paypal/callback是否已添加到应用设置的Return URLs列表中(Sandbox环境下支持本地地址,但需确保已配置)。检查Sandbox账户状态:测试用的Sandbox卖家账户需为已验证状态,未验证账户可能导致权限请求失败。可在PayPal开发者平台
Sandbox > Accounts中查看并调整账户状态,必要时重新创建已验证账户。更换为新版API脚本:旧版
api.js脚本兼容性较差,建议使用PayPal JS SDK集成:<Script src="https://www.paypal.com/sdk/js?client-id=你的客户端ID&enable-funding=paypal&components=login" onReady={() => { paypal.Buttons({ createAuthorization: function() { return paypal.login({ scopes: 'openid profile email https://uri.paypal.com/services/paypalattributes', redirectUri: 'http://127.0.0.1:3000/api/paypal/callback' }); } }).render('#lippButton'); }} />
内容的提问来源于stack exchange,提问作者Luis Baz

