<?php
namespace CoreBundle\Ecommerce\Payment\Paypal;
use Pimcore\Bundle\EcommerceFrameworkBundle\Factory;
use Snk\PaypalApiWrapper\ExecutePayment;
trait PaypalExecutePaymentTrait
{
/**
* Call PayPal API Wrapper to execute the payment.
*
* If the payment status is successful manually commit the Order (because we're not using Pimcore's PayPal
* implementation, instead we're using our own) and set the Order ID so the CheckoutController can handle
* inside it's completed action.
*
* @param string $paymentID
* @param string $payerID
* @return array
* @throws \Exception
*/
public function executePayment($paymentID, $payerID)
{
$cart = $this->getCart();
$checkoutManager = Factory::getInstance()->getCheckoutManager($cart);
$checkoutManager->startOrderPayment();
$order = $checkoutManager->getOrder();
$executePayment = new ExecutePayment($this->auth());
$response = $executePayment->execute($paymentID, $payerID);
if ($response['status'] == 1) {
$orderCompleteService = $this->get('core.payment_save_order_send_email');
$order = $orderCompleteService->saveOrder($cart, $response);
$orderCompleteService->updateStockOfProduct($order);
$url = $this->generateUrl('shopHandlerCheckout', ['action' => 'completed']);
$response['successUrl'] = $url;
} elseif ($response['status'] == 2 && $order->getOrderState() == $order::ORDER_STATE_PAYMENT_PENDING) {
// Payment will be cancelled, order state will be resetted and cart will we writable again
$checkoutManager->cancelStartedOrderPayment();
}
return $response;
}
}