1

嗨,我想知道如何使用 JMSPaymentPaypalBundle 在贝宝中显示订单摘要?!

任何提示将不胜感激..

这是我的 paypalController 代码以备不时之需

<?php
namespace Splurgin\EventsBundle\Controller;

use JMS\DiExtraBundle\Annotation as DI;
use JMS\Payment\CoreBundle\Entity\Payment;
use JMS\Payment\CoreBundle\PluginController\Result;
use JMS\Payment\CoreBundle\Plugin\Exception\ActionRequiredException;
use JMS\Payment\CoreBundle\Plugin\Exception\Action\VisitUrl;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\RedirectResponse;


class PaymentController
{
    /** @DI\Inject */
    private $request;

    /** @DI\Inject */
    private $router;

    /** @DI\Inject("doctrine.orm.entity_manager") */
    private $em;

    /** @DI\Inject("payment.plugin_controller") */
    private $ppc;
   /**
     * @DI\Inject("service_container")
     *
     */
    private $container;
    /**
     * @Template
     */
    public function detailsAction($package)
    {
        // note this ticket at this point in inactive ...
        $order = $this->container->get('ticket')->generateTicket($package);
        $order = $this->em->getRepository('SplurginEventsBundle:SplurginEventTickets')->find($order);
        $packageId = $order->getPackageId();
        $package = $this->em->getRepository('SplurginEventsBundle:SplurginEventPackages')->find($package);
        $price = $package->getPrice();
        var_dump($price);
        if($price == null){
            throw new \RuntimeException('Package was not found: '.$result->getReasonCode());
        }

        $form = $this->getFormFactory()->create('jms_choose_payment_method', null, array(
            'amount'   => $price,
            'currency' => 'USD',
            'default_method' => 'payment_paypal', // Optional
            'predefined_data' => array(
                'paypal_express_checkout' => array(
                    'return_url' => $this->router->generate('payment_complete', array(
                        'order' => $order->getId(),
                    ), true),
                    'cancel_url' => $this->router->generate('payment_cancel', array(
                        'order' => $order->getId(),
                    ), true)
                ),
            ),
        ));

        if ('POST' === $this->request->getMethod()) {
            $form->bindRequest($this->request);

            if ($form->isValid()) {
                $this->ppc->createPaymentInstruction($instruction = $form->getData());

                $order->setPaymentInstruction($instruction);
                $this->em->persist($order);
                $this->em->flush($order);

                return new RedirectResponse($this->router->generate('payment_complete', array(
                    'order' => $order->getId(),
                )));
            }
        }
        return array(
            'form' => $form->createView(),
            'order'=>$order->getId(),
        );
    }


    /** @DI\LookupMethod("form.factory") */
    protected function getFormFactory() { }



    /**
     */
    public function completeAction($order)
    {
        $order = $this->em->getRepository('SplurginEventsBundle:SplurginEventTickets')->find($order);

        $instruction = $order->getPaymentInstruction();
        if (null === $pendingTransaction = $instruction->getPendingTransaction()) {
            $payment = $this->ppc->createPayment($instruction->getId(), $instruction->getAmount() - $instruction->getDepositedAmount());
        } else {
            $payment = $pendingTransaction->getPayment();
        }

        $result = $this->ppc->approveAndDeposit($payment->getId(), $payment->getTargetAmount());
        if (Result::STATUS_PENDING === $result->getStatus()) {
            $ex = $result->getPluginException();

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

                if ($action instanceof VisitUrl) {
                    return new RedirectResponse($action->getUrl());
                }

                throw $ex;
            }
        } else if (Result::STATUS_SUCCESS !== $result->getStatus()) {
            throw new \RuntimeException('Transaction was not successful: '.$result->getReasonCode());
        }

    }

     public function cancelAction($order)
    {
        die('cancel the payment');
    }

}
4

1 回答 1

2

我真的不知道为什么这不是文档的一部分,但是该捆绑包能够开箱即用地设置结帐参数...

这就是我的做法

$form = $this->getFormFactory()->create('jms_choose_payment_method', null, array(
    'amount'   => $price,
    'currency' => 'USD',
    'default_method' => 'payment_paypal', // Optional
    'predefined_data' => array(
        'paypal_express_checkout' => array(
            'return_url' => $this->router->generate('payment_complete', array(
                'order' => $order->getId(),
            ), true),
            'cancel_url' => $this->router->generate('payment_cancel', array(
                'order' => $order->getId(),
            ), true),
            'checkout_params' => array(
                'L_PAYMENTREQUEST_0_NAME0' => 'event',
                'L_PAYMENTREQUEST_0_DESC0' => 'some event that the user is trying to buy',
                'L_PAYMENTREQUEST_0_AMT0'=> 6.00, // if you get 10413 , then visit the api errors documentation , this number should be the total amount (usually the same as the price )
                // 'L_PAYMENTREQUEST_0_ITEMCATEGORY0'=> 'Digital',
            ),
        ),

    ),

));

错误代码可以在这里找到

在此处设置ExpressCheckout 请求字段

我会尽快向文档提供拉取请求.. :)

于 2013-06-15T14:30:45.290 回答