samedi 24 juin 2017

Laravel: How can we know the error when sending HTTP request

I am trying to use a Http method to reactivate user's subscription, using HTTP request, but there might be two problems that exist (the json code would return two potential errors):

  1. Reactivating a canceled subscription with an invalid payment profile

    • Given you have a canceled subscription with ID "1000"
    • The subscription has an invalid payment profile Send a PUT request to http://ift.tt/2s2TQ9k
    • The response status should be "422 Unprocessable Entity"

The subscription should be canceled with the following response

  {
      "errors": ["The credit card on file could not be charged."]
  }

  1. Attempting to reactivate an already active subscription
    • Given you have an active subscription with ID "1000"
    • Send a PUT request to http://ift.tt/2s2TQ9k
    • The response status should be "422 Unprocessable Entity"

The subscription should be active with the following JSON response

  {
      "errors": ["Cannot reactivate a subscription that is not marked 
                    \"Canceled\", \"Unpaid\", or \"Trial Ended\"."]
  }

I am new to Laravel, therefore I would like to know the way I can detect the error when sending the request to the server, but right now I only have one option (without a valid if statement) which cannot detect the problem.

The function is here:

public function reactivateSubscription(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();

    // we used Chargify Laravel SDK here
    $subscriptionId = $user->subscription->subscription_id;
    $subscriptionReact = $chargify->subscription()
        ->reactivate($subscriptionId);

    // we use Eloquent to let customers see the effect first
    $user->subscription->state = 'active';
    $user->subscription->save();

    return redirect('account/settings')
        ->with('account', [['msg' => 'Thanks, ' . $user->first_name . ',
         you are now subscribed!', 'type' => 'success', 'icon' => 'fa-check']]);
}

I think an if statement would do but I'm not sure how. Thanks!



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2sNcOhp
via IFTTT

Aucun commentaire:

Enregistrer un commentaire