我尝试使用symfony5中的paypal制作一个应用程序,它会返回一个错误。我遵循了以下说明:https://github.com/Payum/PayumBundle/blob/master/Resources/doc/get_it_started.md,但我不知道它们是否适合symfony5。我有一个控制器:
<?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('foo@example.com');
$storage->update($payment);
$captureToken = $this->get('payum')->getTokenFactory()->createCaptureToken(
$gatewayName,
$payment,
'done' // the route to redirect after capture
);
return $this->redirect($captureToken->getTargetUrl());
}
}这是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它反驳了我的错误:
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.在这一行中:$storage = $this->get('payum')->getStorage('Acme\PaymentBundle\Entity\Payment');如何解决这个问题?
发布于 2022-01-04 17:36:07
我知道这已经有几个月的历史了,但它可能会帮助其他人发现这个问题。
在您的"PayumController“类中添加一个构造函数,并自动添加"Payum\Core\Payum",例如
public function __construct(Payum\Core\Payum $payum)
{
$this->payum = $payum;
}当然,将对$this->get('payum')的调用替换为$this->payum
https://stackoverflow.com/questions/67232684
复制相似问题