gpt4 book ai didi

php - 将 Omnipay 与 PayPal Express Checkout 集成 [symfony2]

转载 作者:太空宇宙 更新时间:2023-11-03 16:24:59 29 4
gpt4 key购买 nike

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

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

Controller "FLY\BookingsBundle\Controller\PayController::postPaymentAction" for URI "/payment/2" is not callable.

这是我的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>

路由.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 Controller 可调用方法应该以 Action 词结尾。

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

然后,您的某些 Controller 方法不是 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 Controller 中使用第 3 方库是一种糟糕的做法。

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

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

你甚至已经有了一个第三方包来做到这一点:

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 }

关于php - 将 Omnipay 与 PayPal Express Checkout 集成 [symfony2],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36247632/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com