【问题标题】:Paypal API Subscription Create returns 400 Bad Request response "name", though request is formed as in documentationPaypal API 订阅创建返回 400 错误请求响应“名称”,但请求的格式与文档中的一样
【发布时间】:2021-08-01 15:23:15
【问题描述】:

我尝试按照:https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_create实现支付宝订阅服务

这是我的第一次尝试。

在沙盒业务帐户中,我创建了两个测试订阅:每月和每年,并使用其 ID 配置应用程序。

这是方法:

public function createSubscription($planSlug, $name, $email) {
    return $this->makeRequest(
        'POST',
        'v1/billing/subscriptions',
        [],
        [
            'plan_id' => $this->plans[$planSlug],
            'subscriber' => [
                'name' => [
                    'given_name' => $name,
                ],
                'email_address' => $email
            ],
            'application_context'=> [
                'brand_name' => config('app.name'),
                'shipping_preference' => 'NO_SHIPPING',
                'user_action' => 'SUBSCRIBE_NOW',
                'return_url' => route('subscribe.approval', ['plan' => $planSlug]),
                'cancel_url'=> route('subscribe.cancelled')
            ],
        ],
        [],
        $isJsonRequest = true
    );
}

但是,当我调用 API 以创建测试订阅时,我收到奇怪的响应,即“名称”参数的格式不正确:

php artisan tinker

>>> $paypal = resolve(App\Services\PaypalService::class);
=> App\Services\PaypalService {#3413}
>>> $paypal->createSubscription('monthly', 'Test', 'test@test.com');

GuzzleHttp\Exception\ClientException with message 'Client error: `POST https://api- 
m.sandbox.paypal.com/v1/billing/subscriptions` resulted in a `400 Bad Request` response:
{"name":"INVALID_REQUEST","message":"Request is not well-formed, syntactically incorrect, or 
violates schema.","debug_id (truncated...)

这很奇怪,因为在 Paypal API 文档(见上文)中,'name' 参数的描述完全一样!

我错过了什么,或者是 Paypal API 表现得很时髦?

【问题讨论】:

  • 最近我正在测试他们的按钮,但他们出现了故障。他们首先未经客户同意就向客户收费。他们的订阅也是我处理过的另一个问题,他们的 webhook 是另外一回事。你应该再试一次。

标签: laravel api paypal


【解决方案1】:

试试这个:

try {
  // your code here
} catch(\Throwable $th) {
  if ($th instanceof ClientException) {
     $r = $th->getResponse();
     $responseBodyAsString = json_decode($r->getBody()->getContents());
     dd($responseBodyAsString);
  }
}

我以前也遇到过这种情况,我很难弄清楚如何显示明确的错误消息。

【讨论】:

    【解决方案2】:

    'return_url' => route('subscribe.approval', ['plan' => $planSlug]), 'cancel_url'=> 路由('subscribe.cancelled')

    问题出在这两个url,可能是你改了.env中的APP_URL

    APP_URL=http://127.0.0.1:8000

    输入应用网址并尝试

    【讨论】:

      猜你喜欢
      • 2017-02-17
      • 2020-06-16
      • 2015-07-17
      • 2022-01-19
      • 2021-01-07
      • 2015-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多