1

updated - I am trying to use the API documentation to change the billing date using the PUT method in Http and Guzzle in Laravel, however, the JSON file would return but it will not change the billing date at all.

  • Reference 1: The official documentation about changing the billing date.
  • Reference2: their sample code in detail (sorry about the bad formatting):

    <?php
    
    $request = new HttpRequest();
    $request->setUrl('https://subdomain.chargify.com/subscriptions/subscriptionId.json');
    $request->setMethod(HTTP_METH_PUT);
    
    $request->setHeaders(array('content-type' => 'application/json'));
    
    $request->setBody('{"subscription":{"next_billing_at":"2018-12-15"}}');
    
    try {
          $response = $request->send();
    
          echo $response->getBody();
     } catch (HttpException $ex) {
          echo $ex;
     }
    

My code in detail:

public function changeYearlySubscriptionBillingDate(Request $request)
{
    $user = $request->user();
    $subscriptionId = $user->subscription->subscription_id;
    $nextBilling = Carbon::now()->addYear();
    $hostname = env('CHARGIFY_HOSTNAME');

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

    $body = ["subscription" => ["next_billing_at" =>[ $nextBilling ]]];

    $config = [
        'headers' => $headers,
        'form_param' => $body
    ];

    $client = new Client($config);

    $res = $client->put("https://$hostname/subscriptions/$subscriptionId.json");

    echo $res->getBody();
}
deefour
  • 34,974
  • 7
  • 97
  • 90
zhiyu
  • 67
  • 2
  • 10
  • Is `[ $nextBilling ]` in `"next_billing_at" =>[ $nextBilling ]` in your code supposed to be an array? It doesn't seem like it is in the example: `"next_billing_at":"2018-12-15"` – Hollings Jun 20 '17 at 15:11
  • Yes originally it isn't an array, it is: `'{"subscription":{"next_billing_at":"2018-12-15"}}'`, I tried that way too but still not working so far. – zhiyu Jun 20 '17 at 15:18
  • Okay. One other thing I noticed, you might have to format your Carbon date with `$nextBilling->toDateString()`. That'll convert it from a Carbon date object to YYYY-MM-DD string format – Hollings Jun 20 '17 at 15:19
  • Yes thanks for the notice, but it is not this problem too since their API support both the YYYY-MM-DD and more precise time stamp :) – zhiyu Jun 20 '17 at 15:26

1 Answers1

0

Changes this:

echo $response->getBody();

to

dd($response->getBody());

and repost the response data is returned.

Roopendra
  • 7,674
  • 16
  • 65
  • 92
Rob
  • 361
  • 2
  • 2
  • `Stream {#726 ▼ -stream: stream resource @11 ▼ wrapper_type: "PHP" stream_type: "TEMP" mode: "w+b" unread_bytes: 0 seekable: true uri: "php://temp" options: [] } -size: null -seekable: true -readable: true -writable: true -uri: "php://temp" -customMetadata: [] }` – zhiyu Jun 20 '17 at 15:59
  • try `dd($res->getBody());` instead. – Hollings Jun 20 '17 at 17:44
  • I did use `dd($res->getBody());` and it is the same information given above. :) – zhiyu Jun 20 '17 at 17:47
  • use GuzzleHttp\Stream\Stream; $request = $client->createRequest('PUT', 'http://httpbin.org/put', ['body' => 'testing...']); echo $request->getBody()->read(4); // test echo $request->getBody()->read(4); // ing. echo $request->getBody()->read(1024); // .. var_export($request->eof()); – Rob Jun 20 '17 at 21:28