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

Commit 2cda528

Browse files
committed
update saferpay api to version 1.10
1 parent 21f9f14 commit 2cda528

14 files changed

+168
-226
lines changed

Action/Api/CapturePaymentAction.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,19 +32,16 @@ public function execute($request)
3232
RequestNotSupportedException::assertSupports($this, $request);
3333

3434
$details = ArrayObject::ensureArrayObject($request->getModel());
35-
$details->validateNotEmpty([
36-
'transaction_id',
37-
]);
3835

39-
// transaction already captured
40-
if(isset($details['transaction_captured']) && $details['transaction_captured'] === true) {
41-
return;
36+
$transactionIdKey = null;
37+
if ($request->getType() === 'PAYMENT') {
38+
$transactionIdKey = 'transaction_id';
39+
} else {
40+
$transactionIdKey = sprintf('%s_transaction_id', strtolower($request->getType()));
4241
}
4342

44-
$details->replace(
45-
$this->api->captureTransaction($details['transaction_id'])
46-
);
47-
43+
$details->validateNotEmpty([$transactionIdKey]);
44+
$details->replace($this->api->captureTransaction($details->get($transactionIdKey), $request->getType()));
4845
}
4946

5047
/**

Action/Api/CreateTransactionAction.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,6 @@ public function supports($request)
5555
{
5656
return
5757
$request instanceof CreateTransaction &&
58-
$request->getModel() instanceof \ArrayAccess
59-
;
58+
$request->getModel() instanceof \ArrayAccess;
6059
}
6160
}

Action/Api/GetTransactionDataAction.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public function supports($request)
4545
{
4646
return
4747
$request instanceof GetTransactionData &&
48-
$request->getModel() instanceof \ArrayAccess
49-
;
48+
$request->getModel() instanceof \ArrayAccess;
5049
}
5150
}

Action/Api/RefundTransactionAction.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@ public function execute($request)
3030

3131
$model = ArrayObject::ensureArrayObject($request->getModel());
3232
$model->validateNotEmpty([
33-
'transaction_id',
33+
'capture_id',
3434
'amount',
3535
'currency_code',
3636
]);
3737

38-
$model->replace(
39-
$this->api->refundTransaction((array) $model)
40-
);
38+
$model->replace($this->api->refundTransaction((array) $model));
4139
}
4240

4341
/**
@@ -47,7 +45,6 @@ public function supports($request)
4745
{
4846
return
4947
$request instanceof RefundTransaction &&
50-
$request->getModel() instanceof \ArrayAccess
51-
;
48+
$request->getModel() instanceof \ArrayAccess;
5249
}
5350
}

Action/CaptureAction.php

Lines changed: 62 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -49,50 +49,78 @@ public function execute($request)
4949

5050
if (isset($httpRequest->query['cancelled'])) {
5151
$details['transaction_cancelled'] = true;
52+
return;
5253
}
5354

5455
if (isset($httpRequest->query['failed'])) {
5556
$details['transaction_failed'] = true;
57+
return;
5658
}
5759

58-
if (false == $details['token']) {
59-
60-
if (false == $details['success_url'] && $request->getToken()) {
61-
$successUrl = HttpUri::createFromString($request->getToken()->getTargetUrl());
62-
$modifier = new MergeQuery('success=1');
63-
$successUrl = $modifier->process($successUrl);
64-
$details['success_url'] = (string)$successUrl;
65-
}
66-
67-
if (false == $details['fail_url'] && $request->getToken()) {
68-
$failedUrl = HttpUri::createFromString($request->getToken()->getTargetUrl());
69-
$modifier = new MergeQuery('failed=1');
70-
$failedUrl = $modifier->process($failedUrl);
71-
$details['fail_url'] = (string)$failedUrl;
72-
}
73-
74-
if (false == $details['abort_url'] && $request->getToken()) {
75-
$cancelUri = HttpUri::createFromString($request->getToken()->getTargetUrl());
76-
$modifier = new MergeQuery('cancelled=1');
77-
$cancelUri = $modifier->process($cancelUri);
78-
$details['abort_url'] = (string)$cancelUri;
79-
}
80-
81-
if (false == $details['notify_url'] && $request->getToken() && $this->tokenFactory) {
82-
$notifyToken = $this->tokenFactory->createNotifyToken(
83-
$request->getToken()->getGatewayName(),
84-
$request->getToken()->getDetails()
85-
);
86-
87-
$details['notify_url'] = $notifyToken->getTargetUrl();
88-
}
89-
90-
$this->gateway->execute(new CreateTransaction($details));
60+
// no token given, we need to initialize payment page first.
61+
if (!$details->offsetExists('token') || $details->offsetGet('token') === null) {
62+
$this->paymentPageInitializeAction($request, $details);
63+
return;
9164
}
9265

93-
$details['capture_state_reached'] = true;
66+
// we're back from payment page. let's assert the payment
67+
$this->paymentPageAssertAction($request, $details);
68+
69+
}
70+
71+
/**
72+
* @param Capture $request
73+
* @param ArrayObject $details
74+
*/
75+
protected function paymentPageInitializeAction(Capture $request, ArrayObject $details)
76+
{
77+
if (!$details->offsetExists('success_url')) {
78+
$successUrl = HttpUri::createFromString($request->getToken()->getTargetUrl());
79+
$modifier = new MergeQuery('success=1');
80+
$successUrl = $modifier->process($successUrl);
81+
$details->offsetSet('success_url', (string) $successUrl);
82+
}
83+
84+
if (!$details->offsetExists('fail_url')) {
85+
$failedUrl = HttpUri::createFromString($request->getToken()->getTargetUrl());
86+
$modifier = new MergeQuery('failed=1');
87+
$failedUrl = $modifier->process($failedUrl);
88+
$details->offsetSet('fail_url', (string) $failedUrl);
89+
}
90+
91+
if (!$details->offsetExists('abort_url')) {
92+
$cancelUri = HttpUri::createFromString($request->getToken()->getTargetUrl());
93+
$modifier = new MergeQuery('cancelled=1');
94+
$cancelUri = $modifier->process($cancelUri);
95+
$details->offsetSet('abort_url', (string) $cancelUri);
96+
}
97+
98+
if (!$details->offsetExists('notify_url')) {
99+
$notifyToken = $this->tokenFactory->createNotifyToken(
100+
$request->getToken()->getGatewayName(),
101+
$request->getToken()->getDetails()
102+
);
103+
$details->offsetSet('notify_url', $notifyToken->getTargetUrl());
104+
}
105+
106+
$this->gateway->execute(new CreateTransaction($details));
107+
}
108+
109+
/**
110+
* @param Capture $request
111+
* @param ArrayObject $details
112+
*/
113+
protected function paymentPageAssertAction(Capture $request, ArrayObject $details)
114+
{
115+
// get current payment status directly from saferpay.
94116
$this->gateway->execute(new Sync($details));
95117

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

98126
/**

Action/NotifyAction.php

Lines changed: 6 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Payum\Core\GatewayAwareInterface;
1212
use Payum\Core\GatewayAwareTrait;
1313
use Payum\Core\Reply\HttpResponse;
14-
use Payum\Core\Request\GetHumanStatus;
1514
use Payum\Core\Request\Notify;
1615
use Payum\Core\Request\Sync;
1716
use Payum\Core\Exception\RequestNotSupportedException;
@@ -49,44 +48,16 @@ public function execute($request)
4948

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

52-
// check lock
53-
if ($this->api->getLockHandler()->transactionIsLocked($details['token'])) {
54-
throw new HttpResponse('TRANSACTION_LOCKED', 503);
55-
}
56-
57-
if (!isset($details['process_notify'])) {
58-
59-
// since we're handling with some sort of a race condition here,
60-
// we need to throttle the unlock process for half of a second.
61-
usleep(500000);
62-
63-
$details['process_notify'] = true;
64-
throw new HttpResponse('TRANSACTION_AWAITING', 503);
65-
}
66-
67-
// set lock
68-
$this->api->getLockHandler()->lockTransaction($details['token']);
69-
70-
// sync data
51+
// get current payment status directly from saferpay.
7152
$this->gateway->execute(new Sync($details));
7253

73-
// remove tmp capture state
74-
unset($details['capture_state_reached']);
75-
76-
$this->gateway->execute($status = new GetHumanStatus($request->getToken()));
77-
78-
if ($status->isCaptured()) {
79-
throw new HttpResponse('OK', 200);
80-
}
81-
82-
if (isset($details['transaction_status']) && in_array($details['transaction_status'], ['PENDING', 'AUTHORIZED'])) {
83-
$this->gateway->execute(new CapturePayment($details));
54+
// capture payment since everything seems ok so far.
55+
if ($details->offsetExists('transaction_status') && in_array($details->get('transaction_status'), ['PENDING', 'AUTHORIZED'])) {
56+
$capturePayment = new CapturePayment($details);
57+
$capturePayment->setType('PAYMENT');
58+
$this->gateway->execute($capturePayment);
8459
}
8560

86-
$this->gateway->execute(new Sync($details));
87-
88-
$this->api->getLockHandler()->unlockTransaction($details['token']);
89-
9061
throw new HttpResponse('OK', 200);
9162
}
9263

Action/RefundAction.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,13 @@ public function execute($request)
2828

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

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

@@ -40,7 +45,6 @@ public function supports($request)
4045
{
4146
return
4247
$request instanceof Refund &&
43-
$request->getModel() instanceof \ArrayAccess
44-
;
48+
$request->getModel() instanceof \ArrayAccess;
4549
}
4650
}

Action/StatusAction.php

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@ class StatusAction implements ActionInterface, ApiAwareInterface
2121
const STATUS_CAPTURED = 'CAPTURED';
2222
const STATUS_PENDING = 'PENDING';
2323

24-
/**
25-
* CaptureAction constructor.
26-
*/
2724
public function __construct()
2825
{
2926
$this->apiClass = Api::class;
@@ -40,46 +37,39 @@ public function execute($request)
4037

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

43-
if (!isset($details['transaction_status'])
44-
&& isset($details['token'])
45-
&& isset($details['expiration'])
46-
&& !is_null($details['expiration']) && $details['expiration'] < time()) {
47-
$request->markExpired();
40+
if (!$details->offsetExists('token') || $details->offsetGet('token') === null) {
41+
$request->markNew();
4842
return;
4943
}
5044

51-
//handle failed cancellation
52-
if (isset($details['transaction_cancelled']) && $details['transaction_cancelled'] === true) {
53-
$request->markCanceled();
45+
if ($details->offsetExists('capture_authorized_or_pending') && $details->offsetGet('capture_authorized_or_pending') === true) {
46+
$request->markAuthorized();
5447
return;
5548
}
5649

57-
//handle failed transaction
58-
if (isset($details['transaction_failed']) && $details['transaction_failed'] === true) {
59-
$request->markFailed();
50+
if ($details->offsetExists('transaction_cancelled') && $details->get('transaction_cancelled') === true) {
51+
$request->markCanceled();
6052
return;
6153
}
6254

63-
//handle tmp capture transaction
64-
if (isset($details['capture_state_reached']) && $details['capture_state_reached'] === true) {
65-
$request->markAuthorized();
55+
if ($details->offsetExists('transaction_failed') && $details->get('transaction_failed') === true) {
56+
$request->markFailed();
6657
return;
6758
}
6859

69-
if (!isset($details['token']) || !strlen($details['token'])) {
60+
if (!$details->offsetExists('transaction_status')) {
7061
$request->markNew();
7162
return;
7263
}
7364

74-
if (!isset($details['transaction_status'])) {
75-
$request->markNew();
65+
if ($details->offsetExists('expiration') && !is_null($details->get('expiration')) && $details->get('expiration') < time()) {
66+
$request->markExpired();
7667
return;
7768
}
7869

79-
$status = isset($details['transaction_status']) ? $details['transaction_status'] : null;
80-
81-
switch ($details['transaction_type']) {
70+
switch ($details->get('transaction_type')) {
8271
case self::TYPE_PAYMENT:
72+
$status = $details->offsetExists('transaction_status') ? $details->get('transaction_status') : null;
8373
switch ($status) {
8474
case self::STATUS_AUTHORIZED:
8575
$request->markAuthorized();
@@ -96,6 +86,7 @@ public function execute($request)
9686
}
9787
break;
9888
case self::TYPE_REFUND:
89+
$status = $details->offsetExists('refund_transaction_status') ? $details->get('refund_transaction_status') : null;
9990
switch ($status) {
10091
case self::STATUS_AUTHORIZED:
10192
$request->markAuthorized();

Action/SyncAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function execute($request)
2525

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

28-
if ($details['token']) {
28+
if ($details->offsetExists('token')) {
2929
$this->gateway->execute(new GetTransactionData($details));
3030
}
3131
}

0 commit comments

Comments
 (0)