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

Commit 21f9f14

Browse files
committed
only capture payment in notify action
1 parent b1179ce commit 21f9f14

File tree

6 files changed

+44
-30
lines changed

6 files changed

+44
-30
lines changed

Action/CaptureAction.php

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use Payum\Core\Security\GenericTokenFactoryAwareInterface;
1919
use DachcomDigital\Payum\Saferpay\Api;
2020
use DachcomDigital\Payum\Saferpay\Request\Api\CreateTransaction;
21-
use DachcomDigital\Payum\Saferpay\Request\Api\CapturePayment;
2221

2322
class CaptureAction implements ActionInterface, ApiAwareInterface, GatewayAwareInterface, GenericTokenFactoryAwareInterface
2423
{
@@ -91,23 +90,9 @@ public function execute($request)
9190
$this->gateway->execute(new CreateTransaction($details));
9291
}
9392

94-
if($this->api->getLockHandler()->transactionIsLocked($details['token'])) {
95-
return;
96-
}
97-
98-
// set lock
99-
$this->api->getLockHandler()->lockTransaction($details['token']);
100-
101-
$this->gateway->execute(new Sync($details));
102-
103-
if (isset($details['transaction_status']) && in_array($details['transaction_status'], ['PENDING', 'AUTHORIZED'])) {
104-
$this->gateway->execute(new CapturePayment($details));
105-
}
106-
93+
$details['capture_state_reached'] = true;
10794
$this->gateway->execute(new Sync($details));
10895

109-
$this->api->getLockHandler()->unlockTransaction($details['token']);
110-
11196
}
11297

11398
/**
@@ -117,7 +102,6 @@ public function supports($request)
117102
{
118103
return
119104
$request instanceof Capture &&
120-
$request->getModel() instanceof \ArrayAccess
121-
;
105+
$request->getModel() instanceof \ArrayAccess;
122106
}
123107
}

Action/NotifyAction.php

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,31 @@
1111
use Payum\Core\GatewayAwareInterface;
1212
use Payum\Core\GatewayAwareTrait;
1313
use Payum\Core\Reply\HttpResponse;
14+
use Payum\Core\Request\GetHumanStatus;
1415
use Payum\Core\Request\Notify;
1516
use Payum\Core\Request\Sync;
1617
use Payum\Core\Exception\RequestNotSupportedException;
18+
use Payum\Core\Storage\StorageInterface;
1719

1820
class NotifyAction implements ActionInterface, ApiAwareInterface, GatewayAwareInterface
1921
{
2022
use GatewayAwareTrait;
2123
use ApiAwareTrait;
2224

25+
/**
26+
* @var StorageInterface
27+
*/
28+
protected $tokenStorage;
29+
2330
/**
2431
* NotifyAction constructor.
32+
*
33+
* @param StorageInterface $tokenStorage
2534
*/
26-
public function __construct()
35+
public function __construct(StorageInterface $tokenStorage)
2736
{
2837
$this->apiClass = Api::class;
38+
$this->tokenStorage = $tokenStorage;
2939
}
3040

3141
/**
@@ -40,15 +50,35 @@ public function execute($request)
4050
$details = ArrayObject::ensureArrayObject($request->getModel());
4151

4252
// check lock
43-
if($this->api->getLockHandler()->transactionIsLocked($details['token'])) {
44-
throw new HttpResponse('OK', 200);
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);
4565
}
4666

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

70+
// sync data
5071
$this->gateway->execute(new Sync($details));
5172

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+
5282
if (isset($details['transaction_status']) && in_array($details['transaction_status'], ['PENDING', 'AUTHORIZED'])) {
5383
$this->gateway->execute(new CapturePayment($details));
5484
}
@@ -67,7 +97,6 @@ public function supports($request)
6797
{
6898
return
6999
$request instanceof Notify &&
70-
$request->getModel() instanceof \ArrayAccess
71-
;
100+
$request->getModel() instanceof \ArrayAccess;
72101
}
73102
}

Action/StatusAction.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,12 @@ public function execute($request)
6060
return;
6161
}
6262

63+
//handle tmp capture transaction
64+
if (isset($details['capture_state_reached']) && $details['capture_state_reached'] === true) {
65+
$request->markAuthorized();
66+
return;
67+
}
68+
6369
if (!isset($details['token']) || !strlen($details['token'])) {
6470
$request->markNew();
6571
return;

Action/SyncAction.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ public function supports($request)
3737
{
3838
return
3939
$request instanceof Sync &&
40-
$request->getModel() instanceof \ArrayAccess
41-
;
40+
$request->getModel() instanceof \ArrayAccess;
4241
}
4342
}

Handler/LockHandler.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,6 @@ public function lockTransaction($id)
3838
*/
3939
public function unlockTransaction($id)
4040
{
41-
// since we're handling with some sort of a race condition here,
42-
// we need to throttle the unlock process for half of a second.
43-
usleep(500000);
44-
4541
if ($this->transactionIsLocked($id)) {
4642
unlink($this->getTransactionLockPath($id));
4743
}

SaferpayGatewayFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected function populateConfig(ArrayObject $config)
2828

2929
'payum.action.capture' => new CaptureAction(),
3030
'payum.action.status' => new StatusAction(),
31-
'payum.action.notify' => new NotifyAction(),
31+
'payum.action.notify' => new NotifyAction($config['payum.security.token_storage']),
3232
'payum.action.sync' => new SyncAction(),
3333
'payum.action.refund' => new RefundAction(),
3434
'payum.action.convert_payment' => new ConvertPaymentAction(),

0 commit comments

Comments
 (0)