【问题标题】:Integrating Omnipay with PayPal Express Checkout [symfony2]将 Omnipay 与 PayPal Express Checkout 集成 [symfony2]
【发布时间】:2016-07-14 20:15:31
【问题描述】:

我正在尝试在我的网站中将 ominipay 与 PayPal Express Checkout 集成。我有一个命令表(英文订单),我在其中保存参考、日期、用户 ID、命令 =>[命令正在存储:priceTTC、priceHT、地址、数量、令牌]。

当用户点击支付按钮时,我有这个错误:

控制器 "FLY\BookingsBundle\Controller\PayController::postPaymentAction" 为 URI“/payment/2”不可调用。

这是我的 validation.html.twig

 <form action="{{ path('postPayment', { 'id' : commande.id }) }}" 
    method="POST"/>
    <input name="token" type="hidden" value="{{ commande.commande.token }}" />
    <input name="price" type="hidden" value="{{ commande.commande.priceTTC }}" />
    <input name="date" type="hidden" value="{{ commande.date|date('dmyhms') }}" />
    <button type="submit" class="btn btn-success pull-right">Pay</button>
  </form>

Routing.yml

postPayment:
     pattern:  /payment/{id}
     defaults: { _controller: FLYBookingsBundle:Pay:postPayment }

getSuccessPayment:
     pattern:  /success/{id}
     defaults: { _controller: FLYBookingsBundle:Pay:getSuccessPayment }

PayController.php

class PayController extends Controller
{

    public function postPayment (Commandes $commande)
    {
        $params = array(
            'cancelUrl' => 'here you should place the url to which the users will be redirected if they cancel the payment',
            'returnUrl' => 'here you should place the url to which the response of PayPal will be proceeded', // in your case             //  you have registered in the routes 'payment_success'
            'amount' => $commande->get('priceTTC'),
        );

        session()->put('params', $params); // here you save the params to the session so you can use them later.
        session()->save();

        $gateway = Omnipay::create('PayPal_Express');
        $gateway->setUsername('xxxxxxxxx-facilitator_api1.gmail.com'); // here you should place the email of the business sandbox account
        $gateway->setPassword('xxxxxxxxxxxxxx'); // here will be the password for the account
        $gateway->setSignature('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'); // and the signature for the account
        $gateway->setTestMode(true); // set it to true when you develop and when you go to production to false
        $response = $gateway->purchase($params)->send(); // here you send details to PayPal

        if ($response->isRedirect()) {
            // redirect to offsite payment gateway
            $response->redirect();
        }
        else {
            // payment failed: display message to customer
            echo $response->getMessage();
        }
    }

.

public function getSuccessPayment (Auth $auth, Transaction $transaction)
    {
        $gateway = Omnipay::create('PayPal_Express');
        $gateway->setUsername('xxxxxxxxxxx-facilitator_api1.gmail.com\''); // here you should place the email of the business sandbox account
        $gateway->setPassword('xxxxxxxxxxxxxxxx'); // here will be the password for the account
        $gateway->setSignature('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'); // and the signature for the account
        $gateway->setTestMode(true);
        $params = session()->get('params');
        $response = $gateway->completePurchase($params)->send();
        $paypalResponse = $response->getData(); // this is the raw response object

        if(isset($paypalResponse['PAYMENTINFO_0_ACK']) && $paypalResponse['PAYMENTINFO_0_ACK'] === 'Success') {
            // here you process the response. Save to database ...

        }
        else {
            // Failed transaction ...
        }
    }
}

【问题讨论】:

  • 这看起来更像是一个 symfony 问题,而不是一个 omnipay 问题。从omnipay 的角度来看,您在控制器操作中所做的操作看起来是正确的,但错误消息似乎表明symfony 路由器没有找到您的控制器。我不是 symfony 专家,但也许您的控制器操作需要命名为 postPaymentAction 而不是 postPayment?
  • 我删除了 routing.yml 中的路由,并在我的控制器顶部创建了一个路由,如下所示:/** * * @Route("/", name="pay") * @Method("GET") */ 然后在validation.html.twig 中我将路径更改为pay。它似乎有效,但是当我单击按钮付款时,它会将我重定向到空白页面。 http://127.0.0.1/symfony/web/app_dev.php/?id=34 。我认为我应该重定向到贝宝,以便用户可以付款...?
  • 我还将 postPayment 更改为 postPaymentAction。现在我有这个错误:Attempted to call function "session" from namespace "FLY\BookingsBundle\Controller"session()-&gt;put('params', $params); 我想我知道为什么我有很多错误,因为我拥有的控制器是用于 laravel 而不是 symfony2。
  • 是的,你不能在 symfony 中使用 laravel 控制器,反之亦然。

标签: php symfony paypal twig omnipay


【解决方案1】:

Symfony 控制器的可调用方法应该以 Action 字结尾。

public function postPayment(...) --> public function postPaymentAction(...)

然后,您的一些控制器方法不是 symfony 有效的,它们似乎是基于 laravel 的。

// Laravel
session()->put('params', $params); // here you save the params to the session so you can use them later.
session()->save();

-->
// Symfony

use Symfony\Component\HttpFoundation\Request;

public function postPaymentAction(Commandes $commande, Request $request)

$request->getSession(); // The request should be incldued as an action parameter
$session->set('params', $params);

然后,关于 Omnipay 本身的使用,我想说在 Symfony 控制器中使用 3rd 方库是一种糟糕的做法。

我建议您改用服务,并从其配置(可能是参数)中传递您的凭据信息。

http://symfony.com/doc/current/service_container.html

// Direct, bad practice
$gateway = Omnipay::create('PayPal_Express');
$gateway->setUsername('xxxxxxxxx-facilitator_api1.gmail.com'); // here you should place the email of the business sandbox account
$gateway->setPassword('xxxxxxxxxxxxxx'); // here will be the password for the account
$gateway->setSignature('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'); // and the signature for the account
$gateway->setTestMode(true); // set it to true when you develop and when you go to production to false

$response = $gateway->purchase($params)->send(); // here you send details to PayPal

您甚至已经有一个 3rd-party 捆绑包可以做到这一点:

https://github.com/colinodell/omnipay-bundle

// Using a Service to get a full-configured gateway
$gateway = $this->get('omnipay')->getDefaultGateway();

$response = $gateway->purchase($params)->send();

您也可以在路由器文件中锁定 HTTP 方法,即使它是可选的:

postPayment:
     pattern:  /payment/{id}
     method:   POST
     defaults: { _controller: FLYBookingsBundle:Pay:postPayment }

getSuccessPayment:
     pattern:  /success/{id}
     method:   GET
     defaults: { _controller: FLYBookingsBundle:Pay:getSuccessPayment }

【讨论】:

  • 感谢您的回复。我已经找到了解决我的问题的方法,我决定将 Paypal 快速结帐与 Payum 一起使用。在我的下一个项目中,我将尝试使用 Omnipay,您的回答会有所帮助。 :)
猜你喜欢
  • 2017-02-08
  • 2014-02-27
  • 2018-04-11
  • 1970-01-01
  • 2012-10-10
  • 2017-08-21
  • 2013-06-05
  • 1970-01-01
  • 2014-07-18
相关资源
最近更新 更多