Created
January 18, 2015 22:29
-
-
Save jkabat/02e317daf577af690998 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace AppBundle\Controller; | |
use Payum\Core\Request\GetHumanStatus; | |
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | |
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | |
use Symfony\Component\HttpFoundation\Request; | |
use Symfony\Component\HttpFoundation\JsonResponse; | |
class PaymentController extends Controller | |
{ | |
/** | |
* @Route("/payment/prepare", name="prepare") | |
*/ | |
public function prepareAction() | |
{ | |
$paymentName = 'offline'; | |
$storage = $this->get('payum')->getStorage('AppBundle\Model\Order'); | |
$order = $storage->create(); | |
$order->setNumber(uniqid()); | |
$order->setCurrencyCode('EUR'); | |
$order->setTotalAmount(123); // 1.23 EUR | |
$order->setDescription('A description'); | |
$order->setClientId('anId'); | |
$order->setClientEmail('foo@example.com'); | |
$storage->update($order); | |
$captureToken = $this->get('payum.security.token_factory')->createCaptureToken( | |
$paymentName, | |
$order, | |
'done' // the route to redirect after capture | |
); | |
return $this->redirect($captureToken->getTargetUrl()); | |
} | |
/** | |
* @Route("/payment/done", name="done") | |
*/ | |
public function doneAction(Request $request) | |
{ | |
$token = $this->get('payum.security.http_request_verifier')->verify($request); | |
$payment = $this->get('payum')->getPayment($token->getPaymentName()); | |
// you can invalidate the token. The url could not be requested any more. | |
// $this->get('payum.security.http_request_verifier')->invalidate($token); | |
// Once you have token you can get the model from the storage directly. | |
$identity = $token->getDetails(); | |
$order = $payum->getStorage($identity->getClass())->find($identity); | |
// or Payum can fetch the model for you while executing a request (Preferred). | |
$payment->execute($status = new GetHumanStatus($token)); | |
$order = $status->getFirstModel(); | |
// you have order and payment status | |
// so you can do whatever you want for example you can just print status and payment details. | |
return new JsonResponse(array( | |
'status' => $status->getValue(), | |
'order' => array( | |
'total_amount' => $order->getTotalAmount(), | |
'currency_code' => $order->getCurrencyCode(), | |
'details' => $order->getDetails(), | |
), | |
)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment