Witam, mam problem z konfiguracją Paypal na pakiecie JMS Payment Core Bundle płatności paypal_express_checkout na Symfony 2.
Po wysłaniu formularza dostaję komunikat:
- form.error.payment_method_required
OrdersController.php
<?php
namespace Adevo\AdminBundle\Controller;
use JMS\DiExtraBundle\Annotation as DI;
use JMS\Payment\CoreBundle\Form\ChoosePaymentMethodType;
use JMS\Payment\CoreBundle\Plugin\Exception\Action\VisitUrl;
use JMS\Payment\CoreBundle\Plugin\Exception\ActionRequiredException;
use JMS\Payment\CoreBundle\PluginController\Result;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Component\HttpFoundation\Request;
use Adevo\AdminBundle\Entity\Order;
use Adevo\AdminBundle\Entity\PaymentProduct;
/**
* @Route("/orders")
*/
class OrdersController extends Controller {
/**
* @Route("/{id_product}",
* name="app_orders_show"
* )
* @Template
*/
public function showAction(Request $request, $id_product) {
$paymentProduct = $this->getDoctrine()->getRepository('AdevoAdminBundle:PaymentProduct')->find($id_product);
if (NULL === $paymentProduct) {
throw new NotFoundHttpException("Błąd, nieprawidłowe ID produktu");
}
$em = $this->getDoctrine()->getManager();
$order = new Order();
$order->setPaymantProduct($paymentProduct);
$order->setUser($this->getUser());
$order->setPrice($paymentProduct->getPrice());
$em->persist($order);
$em->flush();
$form = $this->createForm('jms_choose_payment_method', null, [
'amount' => $order->getPrice(),
'currency' => 'USD',
'default_method' => 'paypal_express_checkout',
'method_options' => [
'paypal_express_checkout' => [
'label' => false,
],
],
'predefined_data' => array(
'paypal_express_checkout' => array(
'return_url' => $this->generateUrl('orders_payment_complete', [
'id' => $order->getId(),
]),
'cancel_url' => $this->generateUrl('orders_payment_cancel', [
'id' => $order->getId(),
])
),
),
]);
$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(),
]));
}
return [
'order' => $order,
'paymentProduct' => $paymentProduct,
'form' => $form->createView(),
];
}
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",
* name="app_orders_paymentcreate"
* )
*
*/
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());
}
}
} else if (Result::STATUS_SUCCESS !== $result->getStatus()) {
throw new \RuntimeException('Transaction was not successful: ' . $result->getReasonCode());
}
if ($result->getStatus() === Result::STATUS_SUCCESS) {
return $this->redirect($this->generateUrl('app_orders_paymentcomplete', [
'id' => $order->getId(),
]));
}
throw $result->getPluginException();
}
/**
* @Route("/{id}/payment/complete",
* name="orders_payment_complete"
* )
*
*/
public function paymentCompleteAction(Order $order) {
return new Response('Payment complete');
}
/**
* @Route("/{id}/payment/complete",
* name="orders_payment_cancel"
* )
*
*/
public function paymentCancelAction(Order $order) {
return new Response('Payment not complete');
}
}
config.yml
jms_payment_core:
encryption:
secret: xxx
jms_payment_paypal:
username: poczta-facilitator_api1.xxx.pl
password: XXX
signature: xxxx4b0W26LJ
return_url: "https://xxx.pl/app_dev.php/admin-panel/orders/my-account"
cancel_url: "https://xxx.pl/app_dev.php/admin-panel/companies"
debug: true
show.html.twig
<div class="text-right">
{% form_theme form 'AdevoAdminBundle:Orders:theme.html.twig' %}
{{ form_start(form) }}
{{ form_widget(form) }}
<button class="btn btn-danger" type="submit"> Zapłać </button>
{{ form_end(form) }}
</div>
Entity Order.php
<?php
namespace Adevo\AdminBundle\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=2, scale=2) */
private $price;
/**
* @ORM\ManyToOne(
* targetEntity = "Common\UserBundle\Entity\User"
* )
*
* @ORM\JoinColumn(
* name = "author_id",
* referencedColumnName = "id"
* )
*/
private $user;
/**
* @ORM\ManyToOne(
* targetEntity = "PaymentProduct",
* inversedBy = "orders"
* )
*
* @ORM\JoinColumn(
* name = "paymentProduct_id",
* referencedColumnName = "id",
* onDelete = "SET NULL"
* )
*/
private $paymantProduct;
/**
* @ORM\Column(name="create_date", type="datetime")
*/
private $createDate;
public function __construct()
{
$this->createDate = new \DateTime();
// $this->orderNumber = $orderNumber;
}
public function getId()
{
return $this->id;
}
public function getAmount()
{
return $this->amount;
}
public function setAmount($amount)
{
$this->amount = $amount;
}
public function getPaymentInstruction()
{
return $this->paymentInstruction;
}
public function setPaymentInstruction(PaymentInstruction $instruction)
{
$this->paymentInstruction = $instruction;
}
/**
* Set price
*
* @param string $price
*
* @return Order
*/
public function setPrice($price)
{
$this->price = $price;
return $this;
}
/**
* Get price
*
* @return string
*/
public function getPrice()
{
return $this->price;
}
/**
* Set createDate
*
* @param \DateTime $createDate
*
* @return Order
*/
public function setCreateDate($createDate)
{
$this->createDate = $createDate;
return $this;
}
/**
* Get createDate
*
* @return \DateTime
*/
public function getCreateDate()
{
return $this->createDate;
}
/**
* Set user
*
* @param \Common\UserBundle\Entity\User $user
*
* @return Order
*/
public function setUser(\Common\UserBundle\Entity\User $user = null)
{
$this->user = $user;
return $this;
}
/**
* Get user
*
* @return \Common\UserBundle\Entity\User
*/
public function getUser()
{
return $this->user;
}
/**
* Set paymantProduct
*
* @param \Adevo\AdminBundle\Entity\PaymentProduct $paymantProduct
*
* @return Order
*/
public function setPaymantProduct(\Adevo\AdminBundle\Entity\PaymentProduct $paymantProduct = null)
{
$this->paymantProduct = $paymantProduct;
return $this;
}
/**
* Get paymantProduct
*
* @return \Adevo\AdminBundle\Entity\PaymentProduct
*/
public function getPaymantProduct()
{
return $this->paymantProduct;
}
/**
* Set orderNumber
*
* @param string $orderNumber
*
* @return Order
*/
public function setOrderNumber($orderNumber)
{
$this->orderNumber = $orderNumber;
return $this;
}
/**
* Get orderNumber
*
* @return string
*/
public function getOrderNumber()
{
return $this->orderNumber;
}
}