payment with payum symfony5

586 Views Asked by At

I try to make an application to pay with paypal in symfony5 and it returns me an error. I followed this instructions:https://github.com/Payum/PayumBundle/blob/master/Resources/doc/get_it_started.md but i don't know whether they are right for symfony5. I have this controller:

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use Payum\Core\Model\Payment;
use Payum\Core\Reply\HttpRedirect;
use Payum\Core\Reply\HttpResponse;
use Payum\Core\Request\Capture;

class PayumController extends AbstractController
{
    /**
     * @Route("/payum", name="payum")
     */
    public function index() 
    {
        
        $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());           
    } 
}

and this is the services.yaml

parameters:

services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.

    # makes classes in src/ available to be used as services
    # this creates a service per class whose id is the fully-qualified class name
    App\:
        resource: '../src/'
        exclude:
            - '../src/DependencyInjection/'
            - '../src/Entity/'
            - '../src/Kernel.php'
            - '../src/Tests/'

    # controllers are imported separately to make sure services can be injected
    # as action arguments even if you don't extend any base controller class
    App\Controller\:
        resource: '../src/Controller/'
        tags: ['controller.service_arguments']

    # add more service definitions when explicit configuration is needed
    # please note that last definitions always *replace* previous ones

and it retunrns me that error:

Service "payum" not found: even though it exists in the app's container, the container inside "App\Controller\PayumController" is a smaller service locator that only knows about the "doctrine", "form.factory", "http_kernel", "parameter_bag", "request_stack", "router", "security.authorization_checker", "security.csrf.token_manager", "security.token_storage", "serializer", "session" and "twig" services. Try using dependency injection instead.

in the line:$storage = $this->get('payum')->getStorage('Acme\PaymentBundle\Entity\Payment'); how can i solve this issue?

1

There are 1 best solutions below

0
On

I know this is a few months old but it might help others finding this question.

Add a constructor to your "PayumController" class and autowire "Payum\Core\Payum",e.g.

public function __construct(Payum\Core\Payum $payum)
{
  $this->payum = $payum;
}

And then of course replace calls to $this->get('payum') with $this->payum