Skip to content
This repository has been archived by the owner on Jun 10, 2020. It is now read-only.

Commit

Permalink
update saferpay api to version 1.10
Browse files Browse the repository at this point in the history
  • Loading branch information
solverat committed Apr 7, 2019
1 parent 21f9f14 commit 2cda528
Show file tree
Hide file tree
Showing 14 changed files with 168 additions and 226 deletions.
17 changes: 7 additions & 10 deletions Action/Api/CapturePaymentAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,16 @@ public function execute($request)
RequestNotSupportedException::assertSupports($this, $request);

$details = ArrayObject::ensureArrayObject($request->getModel());
$details->validateNotEmpty([
'transaction_id',
]);

// transaction already captured
if(isset($details['transaction_captured']) && $details['transaction_captured'] === true) {
return;
$transactionIdKey = null;
if ($request->getType() === 'PAYMENT') {
$transactionIdKey = 'transaction_id';
} else {
$transactionIdKey = sprintf('%s_transaction_id', strtolower($request->getType()));
}

$details->replace(
$this->api->captureTransaction($details['transaction_id'])
);

$details->validateNotEmpty([$transactionIdKey]);
$details->replace($this->api->captureTransaction($details->get($transactionIdKey), $request->getType()));
}

/**
Expand Down
3 changes: 1 addition & 2 deletions Action/Api/CreateTransactionAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public function supports($request)
{
return
$request instanceof CreateTransaction &&
$request->getModel() instanceof \ArrayAccess
;
$request->getModel() instanceof \ArrayAccess;
}
}
3 changes: 1 addition & 2 deletions Action/Api/GetTransactionDataAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public function supports($request)
{
return
$request instanceof GetTransactionData &&
$request->getModel() instanceof \ArrayAccess
;
$request->getModel() instanceof \ArrayAccess;
}
}
9 changes: 3 additions & 6 deletions Action/Api/RefundTransactionAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,12 @@ public function execute($request)

$model = ArrayObject::ensureArrayObject($request->getModel());
$model->validateNotEmpty([
'transaction_id',
'capture_id',
'amount',
'currency_code',
]);

$model->replace(
$this->api->refundTransaction((array) $model)
);
$model->replace($this->api->refundTransaction((array) $model));
}

/**
Expand All @@ -47,7 +45,6 @@ public function supports($request)
{
return
$request instanceof RefundTransaction &&
$request->getModel() instanceof \ArrayAccess
;
$request->getModel() instanceof \ArrayAccess;
}
}
96 changes: 62 additions & 34 deletions Action/CaptureAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,50 +49,78 @@ public function execute($request)

if (isset($httpRequest->query['cancelled'])) {
$details['transaction_cancelled'] = true;
return;
}

if (isset($httpRequest->query['failed'])) {
$details['transaction_failed'] = true;
return;
}

if (false == $details['token']) {

if (false == $details['success_url'] && $request->getToken()) {
$successUrl = HttpUri::createFromString($request->getToken()->getTargetUrl());
$modifier = new MergeQuery('success=1');
$successUrl = $modifier->process($successUrl);
$details['success_url'] = (string)$successUrl;
}

if (false == $details['fail_url'] && $request->getToken()) {
$failedUrl = HttpUri::createFromString($request->getToken()->getTargetUrl());
$modifier = new MergeQuery('failed=1');
$failedUrl = $modifier->process($failedUrl);
$details['fail_url'] = (string)$failedUrl;
}

if (false == $details['abort_url'] && $request->getToken()) {
$cancelUri = HttpUri::createFromString($request->getToken()->getTargetUrl());
$modifier = new MergeQuery('cancelled=1');
$cancelUri = $modifier->process($cancelUri);
$details['abort_url'] = (string)$cancelUri;
}

if (false == $details['notify_url'] && $request->getToken() && $this->tokenFactory) {
$notifyToken = $this->tokenFactory->createNotifyToken(
$request->getToken()->getGatewayName(),
$request->getToken()->getDetails()
);

$details['notify_url'] = $notifyToken->getTargetUrl();
}

$this->gateway->execute(new CreateTransaction($details));
// no token given, we need to initialize payment page first.
if (!$details->offsetExists('token') || $details->offsetGet('token') === null) {
$this->paymentPageInitializeAction($request, $details);
return;
}

$details['capture_state_reached'] = true;
// we're back from payment page. let's assert the payment
$this->paymentPageAssertAction($request, $details);

}

/**
* @param Capture $request
* @param ArrayObject $details
*/
protected function paymentPageInitializeAction(Capture $request, ArrayObject $details)
{
if (!$details->offsetExists('success_url')) {
$successUrl = HttpUri::createFromString($request->getToken()->getTargetUrl());
$modifier = new MergeQuery('success=1');
$successUrl = $modifier->process($successUrl);
$details->offsetSet('success_url', (string) $successUrl);
}

if (!$details->offsetExists('fail_url')) {
$failedUrl = HttpUri::createFromString($request->getToken()->getTargetUrl());
$modifier = new MergeQuery('failed=1');
$failedUrl = $modifier->process($failedUrl);
$details->offsetSet('fail_url', (string) $failedUrl);
}

if (!$details->offsetExists('abort_url')) {
$cancelUri = HttpUri::createFromString($request->getToken()->getTargetUrl());
$modifier = new MergeQuery('cancelled=1');
$cancelUri = $modifier->process($cancelUri);
$details->offsetSet('abort_url', (string) $cancelUri);
}

if (!$details->offsetExists('notify_url')) {
$notifyToken = $this->tokenFactory->createNotifyToken(
$request->getToken()->getGatewayName(),
$request->getToken()->getDetails()
);
$details->offsetSet('notify_url', $notifyToken->getTargetUrl());
}

$this->gateway->execute(new CreateTransaction($details));
}

/**
* @param Capture $request
* @param ArrayObject $details
*/
protected function paymentPageAssertAction(Capture $request, ArrayObject $details)
{
// get current payment status directly from saferpay.
$this->gateway->execute(new Sync($details));

// mark payment as captured since everything seems ok so far.
// we dont actually capture payment here -> the notify action needs to do this.
// otherwise we'll run into a multi thread action loop
if ($details->offsetExists('transaction_status') && in_array($details->get('transaction_status'), ['PENDING', 'AUTHORIZED'])) {
$details->replace(['capture_authorized_or_pending' => true]);
}
}

/**
Expand Down
41 changes: 6 additions & 35 deletions Action/NotifyAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use Payum\Core\GatewayAwareInterface;
use Payum\Core\GatewayAwareTrait;
use Payum\Core\Reply\HttpResponse;
use Payum\Core\Request\GetHumanStatus;
use Payum\Core\Request\Notify;
use Payum\Core\Request\Sync;
use Payum\Core\Exception\RequestNotSupportedException;
Expand Down Expand Up @@ -49,44 +48,16 @@ public function execute($request)

$details = ArrayObject::ensureArrayObject($request->getModel());

// check lock
if ($this->api->getLockHandler()->transactionIsLocked($details['token'])) {
throw new HttpResponse('TRANSACTION_LOCKED', 503);
}

if (!isset($details['process_notify'])) {

// since we're handling with some sort of a race condition here,
// we need to throttle the unlock process for half of a second.
usleep(500000);

$details['process_notify'] = true;
throw new HttpResponse('TRANSACTION_AWAITING', 503);
}

// set lock
$this->api->getLockHandler()->lockTransaction($details['token']);

// sync data
// get current payment status directly from saferpay.
$this->gateway->execute(new Sync($details));

// remove tmp capture state
unset($details['capture_state_reached']);

$this->gateway->execute($status = new GetHumanStatus($request->getToken()));

if ($status->isCaptured()) {
throw new HttpResponse('OK', 200);
}

if (isset($details['transaction_status']) && in_array($details['transaction_status'], ['PENDING', 'AUTHORIZED'])) {
$this->gateway->execute(new CapturePayment($details));
// capture payment since everything seems ok so far.
if ($details->offsetExists('transaction_status') && in_array($details->get('transaction_status'), ['PENDING', 'AUTHORIZED'])) {
$capturePayment = new CapturePayment($details);
$capturePayment->setType('PAYMENT');
$this->gateway->execute($capturePayment);
}

$this->gateway->execute(new Sync($details));

$this->api->getLockHandler()->unlockTransaction($details['token']);

throw new HttpResponse('OK', 200);
}

Expand Down
12 changes: 8 additions & 4 deletions Action/RefundAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ public function execute($request)

$this->gateway->execute(new RefundTransaction($details));

if($details['transaction_id'] !== false) {
$this->gateway->execute(new CapturePayment($details));
// capture refund since everything seems ok so far.
if ($details->offsetExists('refund_transaction_type') && $details->get('refund_transaction_type') === 'REFUND') {
if ($details->offsetExists('refund_transaction_status') && in_array($details->get('refund_transaction_status'), ['PENDING', 'AUTHORIZED'])) {
$capturePayment = new CapturePayment($details);
$capturePayment->setType('REFUND');
$this->gateway->execute($capturePayment);
}
}
}

Expand All @@ -40,7 +45,6 @@ public function supports($request)
{
return
$request instanceof Refund &&
$request->getModel() instanceof \ArrayAccess
;
$request->getModel() instanceof \ArrayAccess;
}
}
37 changes: 14 additions & 23 deletions Action/StatusAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ class StatusAction implements ActionInterface, ApiAwareInterface
const STATUS_CAPTURED = 'CAPTURED';
const STATUS_PENDING = 'PENDING';

/**
* CaptureAction constructor.
*/
public function __construct()
{
$this->apiClass = Api::class;
Expand All @@ -40,46 +37,39 @@ public function execute($request)

$details = ArrayObject::ensureArrayObject($request->getModel());

if (!isset($details['transaction_status'])
&& isset($details['token'])
&& isset($details['expiration'])
&& !is_null($details['expiration']) && $details['expiration'] < time()) {
$request->markExpired();
if (!$details->offsetExists('token') || $details->offsetGet('token') === null) {
$request->markNew();
return;
}

//handle failed cancellation
if (isset($details['transaction_cancelled']) && $details['transaction_cancelled'] === true) {
$request->markCanceled();
if ($details->offsetExists('capture_authorized_or_pending') && $details->offsetGet('capture_authorized_or_pending') === true) {
$request->markAuthorized();
return;
}

//handle failed transaction
if (isset($details['transaction_failed']) && $details['transaction_failed'] === true) {
$request->markFailed();
if ($details->offsetExists('transaction_cancelled') && $details->get('transaction_cancelled') === true) {
$request->markCanceled();
return;
}

//handle tmp capture transaction
if (isset($details['capture_state_reached']) && $details['capture_state_reached'] === true) {
$request->markAuthorized();
if ($details->offsetExists('transaction_failed') && $details->get('transaction_failed') === true) {
$request->markFailed();
return;
}

if (!isset($details['token']) || !strlen($details['token'])) {
if (!$details->offsetExists('transaction_status')) {
$request->markNew();
return;
}

if (!isset($details['transaction_status'])) {
$request->markNew();
if ($details->offsetExists('expiration') && !is_null($details->get('expiration')) && $details->get('expiration') < time()) {
$request->markExpired();
return;
}

$status = isset($details['transaction_status']) ? $details['transaction_status'] : null;

switch ($details['transaction_type']) {
switch ($details->get('transaction_type')) {
case self::TYPE_PAYMENT:
$status = $details->offsetExists('transaction_status') ? $details->get('transaction_status') : null;
switch ($status) {
case self::STATUS_AUTHORIZED:
$request->markAuthorized();
Expand All @@ -96,6 +86,7 @@ public function execute($request)
}
break;
case self::TYPE_REFUND:
$status = $details->offsetExists('refund_transaction_status') ? $details->get('refund_transaction_status') : null;
switch ($status) {
case self::STATUS_AUTHORIZED:
$request->markAuthorized();
Expand Down
2 changes: 1 addition & 1 deletion Action/SyncAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function execute($request)

$details = ArrayObject::ensureArrayObject($request->getModel());

if ($details['token']) {
if ($details->offsetExists('token')) {
$this->gateway->execute(new GetTransactionData($details));
}
}
Expand Down
Loading

0 comments on commit 2cda528

Please sign in to comment.