Search code examples
try-catchbalanced-payments

Balanced-Payments Try Catch response


I've attempting to learn how to set up credit card payments and using a Balanced Payments test marketplace. I haven't ever set up a try catch statement before and I am having some trouble with processing the response.

Here is my controller try/catch statement:

try{
        $card->debits->create(array(
        "amount" => $amount,
        "appears_on_statement_as" => "test",
        "description" => $invoiceId,
        "order" => $order,
        ));

        } catch (Balanced\Errors\Declined $e) {
            $this->arrResponse['status'] = $e->getMessage();
            return Response::json($this->arrResponse);
        }
        $invoice->save();

         // send email function...
         //redirect
         $this->arrResponse['status'] = SUCCESS;
            return Response::json($this->arrResponse);

I can see the error on chrome developer tools, but I can't make it appear on my view.

Chrome dev tools 500 internal server error statement:

error: {type: "Balanced\Errors\Declined", message: "",…}
file:  "/Applications/MAMP/htdocs/testcc/vendor/balanced/balanced/src/Balanced/Errors/Error.php"
line: 42
message: ""
type: "Balanced\Errors\Declined

processpayment.js file:

 jQuery.post(baseURL+'/processPayment', {
            uri: fundingInstrument.href,
            amount: amount,
            invoiceId: invoiceId,
        }, function(r) {
            // backend response
            if (r.status === 200) {
                $('#msgSection').empty().removeClass('alert-error alert-success').addClass('alert-success').text(' payment has been received').show();

            } else {
                // failure from backend
                $('#msgSection').empty().removeClass('alert-success').addClass('alert-warning').text('error').show();
            }
        });

When test card is processed successfully, everything works and success message appears on my view. However, when I use a test card that is declined, no message is sent to my view. Anyone see what I am doing wrong?


Solution

  • try to check balanced-php client testsuite to see how they use try catch block from here

    so your code would be like.

      try{
           $card->debits->create(array(
              "amount" => $amount,
              "appears_on_statement_as" => "test",
              "description" => $invoiceId,
              "order" => $order,
            ));
    
      } catch (Balanced\Errors\Declined $e) {
             $this->arrResponse['status'] = $e->category_code;
             return Response::json($this->arrResponse);
      }
      $invoice->save();
    
      // send email function...
      //redirect
      $this->arrResponse['status'] = SUCCESS;
      return Response::json($this->arrResponse);
    

    $e->category_code will be either funding-destination-declined or authorization-failed or card-declined