Search code examples
phplaravelchargify

Laravel Client error: `PUT' resulted in a `404 Not Found` response:


I have a public function in my Subscription Controller to remove the pending status of the subscription from chargify.

From their documentation, the code using pecl/http1 method should be like:

$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;
}

Therefore I put this code inside my function

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();


 }

Because I am new to Laravel, and this document is for PHP in general, I'm not sure how could I transform this code inside Laravel framework.

reference:

error message:

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...)

Reference2: the official documentation

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


Solution

  • It looks like a basic DELETE call. You can use Guzzle

    install it via composer

    composer require guzzlehttp/guzzle:~6.0
    

    Then you include this in your controller:

    use GuzzleHttp\Client;
    

    and finally use it:

    $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();
    

    Something along those lines, haven't actually checked if it runs fine.