-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from Payum/api-order-unified-payments
add unified order api, for all possible payments. Now it covers only pay...
- Loading branch information
Showing
10 changed files
with
443 additions
and
34 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"amount": 10, | ||
"currency": "USD", | ||
"meta": { | ||
"name": "paypal", | ||
"purchase_after_url": "http:\/\/google.com" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Sylius package. | ||
* | ||
* (c) Paweł Jędrzejewski | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Payum\Server\Action; | ||
|
||
use Payum\Core\Action\PaymentAwareAction; | ||
use Payum\Core\Request\StatusRequestInterface; | ||
use Payum\Server\Model\Order; | ||
|
||
class OrderStatusAction extends PaymentAwareAction | ||
{ | ||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function execute($request) | ||
{ | ||
/** @var Order $order */ | ||
$order = $request->getModel(); | ||
|
||
if ($order->getDetails()) { | ||
$request->setModel($order->getDetails()); | ||
|
||
$this->payment->execute($request); | ||
|
||
$request->setModel($order); | ||
} else { | ||
$request->markNew(); | ||
} | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function supports($request) | ||
{ | ||
return | ||
$request instanceof StatusRequestInterface && | ||
$request->getModel() instanceof Order | ||
; | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
namespace Payum\Server\Action\Paypal; | ||
|
||
use Payum\Core\Action\PaymentAwareAction; | ||
use Payum\Core\Storage\StorageInterface; | ||
use Payum\Server\Model\Order; | ||
use Payum\Server\Model\PaymentDetails; | ||
use Payum\Server\Request\SecuredCaptureRequest; | ||
|
||
class OrderCaptureAction extends PaymentAwareAction | ||
{ | ||
/** | ||
* @var \Payum\Core\Storage\StorageInterface | ||
*/ | ||
private $paymentDetailsStorage; | ||
|
||
/** | ||
* @param StorageInterface $paymentDetailsStorage | ||
*/ | ||
public function __construct(StorageInterface $paymentDetailsStorage) | ||
{ | ||
$this->paymentDetailsStorage = $paymentDetailsStorage; | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function execute($request) | ||
{ | ||
/** @var Order $order */ | ||
$order = $request->getModel(); | ||
|
||
$details = new PaymentDetails; | ||
$details['PAYMENTREQUEST_0_AMT'] = $order->getAmount(); | ||
$details['PAYMENTREQUEST_0_CURRENCYCODE'] = $order->getCurrency(); | ||
$this->paymentDetailsStorage->updateModel($details); | ||
|
||
$order->setDetails($details); | ||
|
||
$this->payment->execute($request); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function supports($request) | ||
{ | ||
return | ||
$request instanceof SecuredCaptureRequest && | ||
$request->getModel() instanceof Order && | ||
false == $request->getModel()->getDetails() | ||
; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<?php | ||
namespace Payum\Server\Controller; | ||
|
||
use Payum\Core\Bridge\Spl\ArrayObject; | ||
use Payum\Core\Registry\RegistryInterface; | ||
use Payum\Core\Request\BinaryMaskStatusRequest; | ||
use Payum\Core\Security\GenericTokenFactoryInterface; | ||
use Payum\Core\Security\HttpRequestVerifierInterface; | ||
use Payum\Server\Model\Order; | ||
use Payum\Server\Request\ProtectedDetailsRequest; | ||
use Symfony\Component\HttpFoundation\JsonResponse; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException; | ||
|
||
class ApiOrderController | ||
{ | ||
/** | ||
* @var GenericTokenFactoryInterface | ||
*/ | ||
protected $tokenFactory; | ||
|
||
/** | ||
* @var HttpRequestVerifierInterface | ||
*/ | ||
protected $httpRequestVerifier; | ||
|
||
/** | ||
* @var RegistryInterface | ||
*/ | ||
protected $registry; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $orderClass; | ||
|
||
/** | ||
* @param GenericTokenFactoryInterface $tokenFactory | ||
* @param HttpRequestVerifierInterface $httpRequestVerifier | ||
* @param RegistryInterface $registry | ||
* @param string $orderClass | ||
*/ | ||
public function __construct( | ||
GenericTokenFactoryInterface $tokenFactory, | ||
HttpRequestVerifierInterface $httpRequestVerifier, | ||
RegistryInterface $registry, | ||
$orderClass | ||
) { | ||
$this->tokenFactory = $tokenFactory; | ||
$this->registry = $registry; | ||
$this->orderClass = $orderClass; | ||
$this->httpRequestVerifier = $httpRequestVerifier; | ||
} | ||
|
||
/** | ||
* @param Request $request | ||
* | ||
* @throws \Symfony\Component\HttpKernel\Exception\BadRequestHttpException | ||
* | ||
* @return JsonResponse | ||
*/ | ||
public function createAction(Request $request) | ||
{ | ||
if ('json' !== $request->getContentType()) { | ||
throw new BadRequestHttpException('The request content type is invalid.'); | ||
} | ||
|
||
$rawDetails = json_decode($request->getContent(), true); | ||
if (null === $rawDetails) { | ||
throw new BadRequestHttpException('The request content is not valid json.'); | ||
} | ||
if (empty($rawDetails['meta']['name'])) { | ||
throw new BadRequestHttpException('The payment name must be set to meta.name.'); | ||
} | ||
$name = $rawDetails['meta']['name']; | ||
|
||
if (empty($rawDetails['meta']['purchase_after_url'])) { | ||
throw new BadRequestHttpException('The purchase after url has to be set to meta.purchase_after_url.'); | ||
} | ||
$afterUrl = $rawDetails['meta']['purchase_after_url']; | ||
|
||
$storage = $this->registry->getStorageForClass($this->orderClass, $name); | ||
|
||
/** @var Order $order */ | ||
$order = $storage->createModel(); | ||
$order->setAmount($rawDetails['amount']); | ||
$order->setCurrency($rawDetails['currency']); | ||
|
||
$storage->updateModel($order); | ||
|
||
$captureToken = $this->tokenFactory->createCaptureToken($name, $order, $afterUrl); | ||
$getToken = $this->tokenFactory->createToken($name, $order, 'order_get'); | ||
|
||
$meta = array(); | ||
$meta['links'] = array( | ||
'purchase' => null, | ||
'get' => $getToken->getTargetUrl(), | ||
); | ||
$order->setMeta($meta); | ||
|
||
$storage->updateModel($order); | ||
|
||
$meta = $order->getMeta(); | ||
$meta['links']['purchase'] = $captureToken->getTargetUrl(); | ||
$order->setMeta($meta); | ||
|
||
$response = new JsonResponse(iterator_to_array($order)); | ||
$response->headers->set('Cache-Control', 'no-store, no-cache, max-age=0, post-check=0, pre-check=0'); | ||
$response->headers->set('Pragma', 'no-cache'); | ||
|
||
return $response; | ||
} | ||
|
||
/** | ||
* @param Request $request | ||
* | ||
* @return JsonResponse | ||
*/ | ||
public function getAction(Request $request) | ||
{ | ||
$token = $this->httpRequestVerifier->verify($request); | ||
|
||
$status = new BinaryMaskStatusRequest($token); | ||
$this->registry->getPayment($token->getPaymentName())->execute($status); | ||
|
||
/** @var Order $order */ | ||
$order = $status->getModel(); | ||
$meta = $order->getMeta(); | ||
$meta['status'] = $status->getStatus(); | ||
$order->setMeta($meta); | ||
|
||
$storage = $this->registry->getStorageForClass($this->orderClass, $token->getPaymentName()); | ||
$storage->updateModel($order); | ||
|
||
return new JsonResponse(iterator_to_array($order)); | ||
} | ||
} |
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
Oops, something went wrong.