1

我的订阅控制器中有一个公共函数,可以从 chargify 中删除订阅的挂起状态。

从他们的文档中,使用 pecl/http1 方法的代码应该是这样的:

$request = new HttpRequest();
$request->setUrl('https://subdomain.chargify.com/subscriptions/$subscriptionId/delayed_cancel.json');
$request->setMethod(HTTP_METH_DELETE);

$request->setHeaders(array(
  'authorization' => 'Basic YXBpLWtleTp4'
));

$request->setBody('{}');

try {
  $response = $request->send();

  echo $response->getBody();
} catch (HttpException $ex) {
  echo $ex;
}

因此我把这段代码放在我的函数中

public function changeYearlySubscriptionBillingDate(Request $request)
 {
     $chargify = new \Crucial\Service\Chargify([
         'hostname' => env('CHARGIFY_HOSTNAME'),
         'api_key' => env('CHARGIFY_KEY'),
         'shared_key' => env('CHARGIFY_SHARED_KEY')
     ]);

     $subscription = $chargify->subscription();
     $user = $request->user();
     $subscriptionId = $user->subscription->subscription_id;
     $nextBilling = Carbon::now()->addYear();
     $hostname = env('CHARGIFY_HOSTNAME');

     $config = [
         'headers' => [
             'authorization' => 'Basic YXBpLWtleTp4',
             'content-type' => 'application/json'
         ]
     ];

     $client  = new Client($config);

     $res = $client->put("https://$hostname/subscriptions/$subscriptionId/.json",
     ["json" => [
             [ "subscription" =>
                 [ "next_billing_at" => $nextBilling ]
             ]
         ]]);


     echo $res->getBody();


 }

因为我是 Laravel 的新手,而且这个文档一般是针对 PHP 的,所以我不确定如何在 Laravel 框架中转换这段代码。

参考:

错误信息:

Client error: `PUT https://(the hostname)/subscriptions/(the id)/.json` `resulted in a `404 Not Found` response:`
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>404 File Not Found</title>
<style>
b (truncated...)

参考2:官方文档

https://reference.chargify.com/v1/subscriptions/update-subscription-calendar-billing-day-change

4

1 回答 1

2

它看起来像一个基本的 DELETE 调用。您可以使用Guzzle

通过作曲家安装它

composer require guzzlehttp/guzzle:~6.0

然后将其包含在控制器中:

use GuzzleHttp\Client;

最后使用它:

$config = [
            'headers' => [
               'authorization' => 'Basic YXBpLWtleTp4'
         ]
];

$client  = new Client($config); 
$res = $client->delete("https://subdomain.chargify.com/subscriptions/$subscriptionId/delayed_cancel.json",
  [ "json" =>
    [
      [ "subscription" => ["next_billing_at" => $nextBilling]] 
    ]
  ]
);
echo $res->getBody();

沿着这些思路,实际上并没有检查它是否运行良好。

于 2017-06-16T20:03:38.380 回答