首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Symfony 3 JMS支付Paypal错误

Symfony 3 JMS支付Paypal错误
EN

Stack Overflow用户
提问于 2017-08-17 17:10:17
回答 1查看 445关注 0票数 1

我来找你是因为我真的不知道用这个包做贝宝结账有什么问题。

http://jmspaymentcorebundle.readthedocs.io/en/stable/setup.html

这是关于包和文档的链接。

我的问题是在教程的末尾,我有一个错误,我不知道如何解决。

关键-默认的PHP异常JMS\Payment\CoreBundle\Plugin\Exception\ActionRequiredException:“用户必须授权事务。”在C:\wamp64\www\Wolk\vendor\jms\payment-paypal-bundle\JMS\Payment\PaypalBundle\Plugin\ExpressCheckoutPlugin.php 303号线

这是我的控制器代码:

代码语言:javascript
复制
<?php
namespace Wolk\PlatformBundle\Controller;

use Wolk\PlatformBundle\Entity\Order;
use JMS\Payment\CoreBundle\Form\ChoosePaymentMethodType;
use JMS\Payment\CoreBundle\PluginController\Result;
use JMS\Payment\CoreBundle\Plugin\AbstractPlugin;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;

/**
 * @Route("/orders")
 */
class OrdersController extends Controller
{
    /**
 * @Route("/new/{amount}")
 */
public function newAction($amount, $game)
{
    $em = $this->getDoctrine()->getManager();

    $order = new Order($amount);
    $em->persist($order);
    $em->flush();

    return $this->redirect($this->generateUrl('app_orders_show', [
        'id' => $order->getId(),
        'game' => $game,
    ]));
}

/**
 * @Route("/{id}/show")
 * @Template
 */
public function showAction(Request $request, Order $order, $game)
{
    $form = $this->createForm(ChoosePaymentMethodType::class, null, [
        'amount'   => $order->getAmount(),
        'currency' => 'EUR',
    ]);

        $form->handleRequest($request);

    if ($form->isSubmitted() && $form->isValid()) {
        $ppc = $this->get('payment.plugin_controller');
        $ppc->createPaymentInstruction($instruction = $form->getData());

        $order->setPaymentInstruction($instruction);

        $em = $this->getDoctrine()->getManager();
        $em->persist($order);
        $em->flush($order);

        return $this->redirect($this->generateUrl('app_orders_paymentcreate', [
            'id' => $order->getId(),
            'game' => $game,
        ]));
    }

    return [
        'order' => $order,
        'form'  => $form->createView(),
        'game' => $game,
    ];
}

private function createPayment($order)
{
    $instruction = $order->getPaymentInstruction();
    $pendingTransaction = $instruction->getPendingTransaction();

    if ($pendingTransaction !== null) {
        return $pendingTransaction->getPayment();
    }

    $ppc = $this->get('payment.plugin_controller');
    $amount = $instruction->getAmount() - $instruction->getDepositedAmount();

    return $ppc->createPayment($instruction->getId(), $amount);
}

/**
 * @Route("/{id}/payment/create")
 */
public function paymentCreateAction(Order $order)
{
    $payment = $this->createPayment($order);

    $ppc = $this->get('payment.plugin_controller');
    $result = $ppc->approveAndDeposit($payment->getId(), $payment->getTargetAmount());



if ($result->getStatus() === Result::STATUS_PENDING) {
    $ex = $result->getPluginException();

    if ($ex instanceof ActionRequiredException) {
        $action = $ex->getAction();

        if ($action instanceof VisitUrl) {
            return $this->redirect($action->getUrl());
        }
    }
}

throw $result->getPluginException();

    // In a real-world application you wouldn't throw the exception. You would,
    // for example, redirect to the showAction with a flash message informing
    // the user that the payment was not successful.
}

/**
 * @Route("/{id}/payment/complete")
 */
public function paymentCompleteAction(Order $order)
{
    return new Response('Payment complete');
}


}

我的config.yml (这只是与沙箱帐户的对角线)

代码语言:javascript
复制
jms_payment_paypal:
    username: <USERNAME> # not your account username
    password: <PASSWORD> # not your account password
    signature: <SIGNATURE>
    debug: true
    return_url: https://yourdomain.com
    cancel_url: https://yourdomain.com

我的路由文件

代码语言:javascript
复制
app_orders_new:
    path:     /orders/new/{amount}
    defaults: { _controller: "PlatformBundle:Orders:new" }


app_orders_show:
    path:     /orders/{id}/show
    defaults: { _controller: "PlatformBundle:Orders:show" }

app_orders_paymentcreate:
    path:     /orders/{id}/payment/create
    defaults: { _controller: "PlatformBundle:Orders:paymentCreate" }

app_orders_paymentcomplete:
    path:     /orders/{id}/payment/complete
    defaults: { _controller: "PlatformBundle:Orders:paymentComplete" }

最后一个文件,实体命令:

代码语言:javascript
复制
<?php

namespace Wolk\PlatformBundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use JMS\Payment\CoreBundle\Entity\PaymentInstruction;

/**
 * @ORM\Table(name="orders")
 * @ORM\Entity
 */
class Order
{
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /** @ORM\OneToOne(targetEntity="JMS\Payment\CoreBundle\Entity\PaymentInstruction") */
    private $paymentInstruction;

    /** @ORM\Column(type="decimal", precision=10, scale=5) */
    private $amount;

    public function __construct($amount)
    {
        $this->amount = $amount;
    }

    public function getId()
    {
        return $this->id;
    }

    public function getAmount()
    {
        return $this->amount;
    }

    public function getPaymentInstruction()
    {
        return $this->paymentInstruction;
    }

    public function setPaymentInstruction(PaymentInstruction $instruction)
    {
        $this->paymentInstruction = $instruction;
    }
}

我试着搜索谷歌上的所有东西,但我什么都找不到

谢谢你的帮助:)

EN

回答 1

Stack Overflow用户

发布于 2018-07-16 09:16:54

我也犯了同样的错误,我花了很多时间去寻找解决方案。原来我忘了添加以下几行:

代码语言:javascript
复制
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use JMS\Payment\CoreBundle\Plugin\Exception\Action\VisitUrl;
use JMS\Payment\CoreBundle\Plugin\Exception\ActionRequiredException;

我在这里发了这个,也许能帮到其他人。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45741249

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档