【发布时间】: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()->put('params', $params);我想我知道为什么我有很多错误,因为我拥有的控制器是用于 laravel 而不是 symfony2。 -
是的,你不能在 symfony 中使用 laravel 控制器,反之亦然。
标签: php symfony paypal twig omnipay