0

I am trying to catch InvalidCreditCardException and any other exception using Omnipay Bridge of PayumBundle.

I have tried:

try {
    return $this->forward('PayumBundle:Capture:do', array(
        'payum_token' => $captureToken,
    ));
} catch (\Exception $e) {
    $exceptions = array();
    do {
        $exceptions[] = $e->getMessage();
    } while ($e = $e->getPrevious());
    $response = new Response(json_encode(array('status' => 'failed', 'Message' => $exceptions)));

    return $response;
}

But before the code gets to my catch the Omnipay throws its own Exception of creditcard number not being valid.

In comments it also says:

Generally if you want to validate the credit card yourself with custom error messages, you should use your framework's validation library, not this method.

This is exactly what I am trying to do, how can I validate card with custom error using Symfony PayumBundle exception?

Please do not answer this question by giving reference like the first answer below. I do appreciate his assistance but that does not help me on how to make use of that exception in my code.

Very Important

I am trying to understand PayumBundle better so I need to understand what I am doing wrong and steps (not in english but in real example code with explanation) I should take to catch hold of exception in my action and display user friendly message. Please understand, I am really new to symfony and I am trying learn, if you are going to ask me to create a service and do this or that then it will not help me, please see the example here, I really really appreciate the help but then I am really really lost.

Please note that if the correct credit number is entered the transaction goes through just fine, it is only the exception that I need to catch when wrong card number is entered, or card is expired or any other.

In case if it helps, this is how my config.yml looks like

contexts:
    paypal:
        paypal_express_checkout_nvp:
            username: %paypal_username%
            password: %paypal_password%
            signature: %paypal_signature%
            sandbox: %paypal_sandbox%


    stripe_omnipay:
        omnipay:
            type: Stripe
            options:
                apiKey: xxxx
                testMode: true
            extensions:
                PaymentExtension:
                    class: Payum\Core\Extension\PaymentExtention
Shairyar
  • 3,268
  • 7
  • 46
  • 86

2 Answers2

0

You can use a payum extension onException message. In the method you can catch an exception and add info about it to the payment details.

Maksim Kotlyar
  • 3,821
  • 27
  • 31
0

You need to add an extension that will be executed by the Payum\Core\Payment object. As you can see in this code, exception are captured and then adequate extensions will be executed sequentially : https://github.com/Payum/Core/blob/master/Payment.php#L104-L108

You need to create a new class that implements Payum\Core\Extension\ExtensionInterface and implement the logic you want inside the onException() method.

Here is an example of an extension :

class YourExtension implements Payum\Core\Extension\ExtensionInterface
{
    /**
     * @param mixed $request
     */
    public function onPreExecute($request) {}

    /**
     * @param mixed                              $request
     * @param \Payum\Core\Action\ActionInterface $action
     */
    public function onExecute($request, ActionInterface $action) {}

    /**
     * @param mixed                              $request
     * @param \Payum\Core\Action\ActionInterface $action
     */
    public function onPostExecute($request, ActionInterface $action) {}

    /**
     * @param \Payum\Core\Reply\ReplyInterface   $reply
     * @param mixed                              $request
     * @param \Payum\Core\Action\ActionInterface $action
     *
     * @return null|\Payum\Core\Reply\ReplyInterface an extension able to change reply to something else.
     */
    public function onReply(ReplyInterface $reply, $request, ActionInterface $action) {}

    /**
     * @param \Exception                              $exception
     * @param mixed                                   $request
     * @param \Payum\Core\Action\ActionInterface|null $action
     */
    public function onException(\Exception $exception, $request, ActionInterface $action = null)
    {
        // Put your code here
    }
}

Then, in your app configuration file, you must add that extension, so it can get loaded and executed by the framework. The exact answer depends on the type of format you are using as configuration file (yml, php, ini, ?).

Here is an example with yml (official documentation is here btw : https://github.com/Payum/Payum/blob/master/docs/symfony/container-tags.md) :

payum:
   contexts:
       yourContextName:
           PaymentFactoryName: 
              extensions:
                  YourCustomExtension:
                      class: Payum\Core\Extension\YourExtension

In this config file, you should :

Maksim Kotlyar
  • 3,821
  • 27
  • 31
alfallouji
  • 1,160
  • 8
  • 12
  • Thanks. Where do I need to create this new extension class (in what folder) – Shairyar Mar 03 '15 at 03:19
  • You can create an extension folder within your src folder. I am pretty sure your autoloader will catch it. – alfallouji Mar 03 '15 at 03:46
  • Not sure if my answer was clear, I meant anywhere below the src folder (you can create subfolders). – alfallouji Mar 03 '15 at 06:58
  • Thanks for the follow up, I am struggling with trying to setup the config changes you mentioned, the error i get is `Invalid type for path "payum.contexts.stripe_omnipay.omnipay.extensions.PaymentExtension". Expected scalar, but got array.` – Shairyar Mar 03 '15 at 07:33
  • i have updated my question by adding my config.yml, just in case if that helps explain how i added it – Shairyar Mar 03 '15 at 07:37
  • I noticed a typo (Extention instead of Extension) : `class: Payum\Core\Extension\PaymentExtention` in your main post – alfallouji Mar 03 '15 at 07:44
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/72120/discussion-between-baig-and-alfallouji). – Shairyar Mar 03 '15 at 07:51