Search code examples
phpsymfonypaymentpayum

How to generate a url for the first test of Payum Payment Bundle with symfony2.8


I am trying to integrate payum payment bundle into symfony 2.8

My final goal was handling the paypal subscription application.

I have finished first setup as described here.

However I don't know how to test or where to start.

There is public function prepareAction(), I guess at first I should access here.

So I put in my route.yml

acme_payment:
    resource: "@AcmePaymentBundle/Resources/config/routing.yml"
    prefix:   /payment

and in my Acme/PaymentBundle/Resources/config.routing.yml

acme_payment_prepare:
    path:     /prepare
    defaults: { _controller: AcmePaymentBundle:Payment:prepare }

Then try to access,

http://localhost/myapp/web/app_dev.php/payment/prepare

However it shows the error like this .

Unable to generate a URL for the named route "done" as such route does not exist.

I am sure this is related with my PaymentController.php

I think this error is appear in PaymentController.php, but how can I fix it ??

'done' // the route to redirect after capture

<?php


namespace Acme\PaymentBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use Payum\Core\Request\GetHumanStatus;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;

class PaymentController extends Controller 
{
   public function prepareAction()
    {
        $gatewayName = 'offline';

        $storage = $this->get('payum')->getStorage('Acme\PaymentBundle\Entity\Payment');

        $payment = $storage->create();
        $payment->setNumber(uniqid());
        $payment->setCurrencyCode('EUR');
        $payment->setTotalAmount(123); // 1.23 EUR
        $payment->setDescription('A description');
        $payment->setClientId('anId');
        $payment->setClientEmail('[email protected]');

        $storage->update($payment);

        $captureToken = $this->get('payum')->getTokenFactory()->createCaptureToken(
            $gatewayName,
            $payment,
            'done' // the route to redirect after capture
            );

        return $this->redirect($captureToken->getTargetUrl());
    }
    public function doneAction(Request $request)
    {
        $token = $this->get('payum')->getHttpRequestVerifier()->verify($request);

        $gateway = $this->get('payum')->getGateway($token->getGatewayName());

        // you can invalidate the token. The url could not be requested any more.
        // $this->get('payum')->getHttpRequestVerifier()->invalidate($token);

        // Once you have token you can get the model from the storage directly.
        //$identity = $token->getDetails();
        //$payment = $this->get('payum')->getStorage($identity->getClass())->find($identity);

        // or Payum can fetch the model for you while executing a request (Preferred).
        $gateway->execute($status = new GetHumanStatus($token));
        $payment = $status->getFirstModel();

        // you have order and payment status
        // so you can do whatever you want for example you can just print status and payment details.

        return new JsonResponse(array(
            'status' => $status->getValue(),
            'payment' => array(
                'total_amount' => $payment->getTotalAmount(),
                'currency_code' => $payment->getCurrencyCode(),
                'details' => $payment->getDetails(),
            ),
        ));
    }

}



}

Solution

  • Inside your Acme/PaymentBundle/Resources/config.routing.yml you need to also define your done route.

    So the route configuration might look the following:

    acme_payment_prepare:
        path:     /prepare
        defaults: { _controller: AcmePaymentBundle:Payment:prepare }
    
    acme_payment_done:
        path:     /done
        defaults: { _controller: AcmePaymentBundle:Payment:done }   
    

    Then inside your prepareAction method in the PaymentController.php change the done route:

    $captureToken = $this->get('payum')->getTokenFactory()->createCaptureToken(
        $gatewayName,
        $payment,
        'acme_payment_done' // NOTE: I replaced `done` with `acme_payment_done`
     );
    

    I've never used the payum bundle but I hope that helps.