crucialwebstudio / chargify-sdk-php

Chargify SDK for PHP
Apache License 2.0
26 stars 44 forks source link

Call object doesn't read errors in certain scenarios #7

Closed zillarelli closed 8 years ago

zillarelli commented 8 years ago

I am trying to read a response from a request from Direct but don't see any errors, even when i force an error. It looks like the Call class is looking in the wrong place for the errors. When I try to re-submit a form with previously used secure data, the output of $return in the getResponseArray() in the Call method shows the following

array:1 [▼
  "call" => array:7 [▼
    "id" => "my_id"
    "api_id" => "my_api_id"
    "timestamp" => 1448233460
    "nonce" => "some_nonce"
    "request" => array:3 [▼
      "secure" => array:5 [▼
        "api_id" => "my_api_id"
        "timestamp" => "1448233460"
        "nonce" => "some_nonce"
        "data" => "redirect_uri=myredirecturi"
        "signature" => "some_signature"
      ]
      "signup" => array:2 [▼
        "product" => array:1 [▼
          "handle" => "selected-plan"
        ]
        "payment_profile" => array:11 [▼
          "card_number" => ""
          "expiration_month" => "x"
          "expiration_year" => "xxxx"
          "first_name" => "Example"
          "last_name" => "User"
          "email" => "demo@demo.com"
          "billing_address" => ""
          "billing_address_2" => ""
          "billing_city" => ""
          "billing_country" => ""
          "billing_zip" => ""
        ]
      ]
    ]
    "response" => array:2 [▼
      "result" => array:3 [▼
        "status_code" => 422
        "result_code" => 4221
        "errors" => array:1 [▼
          0 => array:2 [▼
            "attribute" => "base"
            "message" => "Duplicate submission detected. Please try again, and refrain from using your browser's Back button"
          ]
        ]
      ]
      "meta" => array:3 [▼
        "status_code" => 422
        "result_code" => 4221
        "errors" => array:1 [▼
          0 => array:2 [▼
            "attribute" => "base"
            "message" => "Duplicate submission detected. Please try again, and refrain from using your browser's Back button"
          ]
        ]
      ]
    ]
    "success" => false
  ]
]

So, instead of looking in $return['result']['errors'], it should be looking in $return['call']['response']['result']['errors']. I noticed that the Call class has a note about this but there hasn't been any fix implemented to catch these errors.

dan-bowen commented 8 years ago

Hello. The check for errors is on the API request itself, not the contents of your actual $call object. You could have a successful API call that returns a $call object that contains errors. In that case you would check the success/fail status of the $call in your calling code.

zillarelli commented 8 years ago

Apologies for not posting my entire code. I am instantiating a new Call object and calling ->readByChargifyId() with the call id given from the Direct response, since the response from Chargify doesn't include error details in their response.

// $response = data returned from Chargify via the Direct form request
$chargify = new ChargifyV2(config('services.chargify.direct'));
$call = $chargify->call();
$responseCall = $call->readByChargifyId($response->get('call_id'));

$responseCall->isError() and $responseCall->getErrors() return false and empty, respectively, even though the call has an error.

Right now I have to do the following to extract the errors:

$chargify = new ChargifyV2(config('services.chargify.direct'));
$call = $chargify->call();
$service = $call->getService();
$response = $service->request('calls/' . $request->get('call_id'), 'GET');
$responseArray = $call->getResponseArray($response);
if (!empty($responseArray['call']['response']['result']['errors'])) {
    foreach ($responseArray['call']['response']['result']['errors'] as $error) {
          $errors->add($error['attribute'], $error['message']);
    }
}

Not sure of any other way to extract the errors. Seems a little more roundabout than it's intended.

dan-bowen commented 8 years ago

Your first code is accurate. As I mentioned, ->isError() and ->getErrors() only deal with problems with the http request to Chargify. As your example code is showing, the http response was successful so there should not be any errors from ->isError() or ->getErrors(). This is expected.

You can get the errors on the actual call with the following:

$chargify = new ChargifyV2(config('services.chargify.direct'));
$call = $chargify->call();
$responseCall = $call->readByChargifyId($response->get('call_id'));
var_dump($responseCall['response']['result']['errors']);
zillarelli commented 8 years ago

ah, gotcha. Thanks for clearing that up.