Skip to content

Commit adef090

Browse files
authored
feature: Cancel support (#3)
1 parent 858f25b commit adef090

File tree

7 files changed

+85
-23
lines changed

7 files changed

+85
-23
lines changed

src/Action/AuthorizeAction.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,13 @@ public function execute($request)
5151
$this->gateway->execute($httpRequest);
5252

5353
$authRequest = ArrayObject::ensureArrayObject($httpRequest->request);
54-
$authRequest->validateNotEmpty(['paymentid', 'securitytoken', 'authorizationcode', 'responsecode', 'result']);
54+
// $authRequest->validateNotEmpty(['paymentid', 'securitytoken', 'authorizationcode', 'responsecode', 'result']);
55+
$authRequest->validateNotEmpty(['paymentid', 'result']);
5556

5657
$details->replace($this->api->authorizeTransaction((array) $details, (array) $authRequest));
5758

58-
if ($details['authorization']) {
59-
// TODO: we can communicate back with Setefi, what can we say here? Maybe a different response URL?
60-
throw new HttpResponse('OK');
61-
}
59+
// TODO: we can communicate back with Setefi, what can we say here? Maybe a different response URL?
60+
throw new HttpResponse('OK');
6261
}
6362

6463
/**

src/Action/CancelAction.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace Webburza\Payum\Setefi\Action;
4+
5+
use Payum\Core\Action\ActionInterface;
6+
use Payum\Core\Bridge\Spl\ArrayObject;
7+
use Payum\Core\Exception\RequestNotSupportedException;
8+
use Payum\Core\GatewayAwareTrait;
9+
use Payum\Core\Request\Cancel;
10+
11+
/**
12+
* Class CancelAction.
13+
*/
14+
class CancelAction implements ActionInterface
15+
{
16+
use GatewayAwareTrait;
17+
18+
/**
19+
* {@inheritdoc}
20+
*
21+
* @param Cancel $request
22+
*/
23+
public function execute($request)
24+
{
25+
RequestNotSupportedException::assertSupports($this, $request);
26+
$model = ArrayObject::ensureArrayObject($request->getModel());
27+
28+
$model['cancellation'] = true;
29+
}
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
public function supports($request)
35+
{
36+
return
37+
$request instanceof Cancel &&
38+
$request->getModel() instanceof \ArrayAccess;
39+
}
40+
}

src/Action/CaptureAction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function execute($request)
6363
// we're need to create the transaction on Setefi
6464
// and redirect the user there
6565
$this->gateway->execute(new CreateTransaction($details));
66-
} elseif (null === $details['authorization']) {
66+
} elseif (null === $details['authorization'] && null === $details['cancellation']) {
6767
// second pass
6868
// we're authorizing the transaction by a server-to-server request
6969
// this request is served to Setefi backend, NOT the returning user

src/Action/StatusAction.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ public function execute($request)
3030
return;
3131
}
3232

33+
if (true === isset($model['cancellation'])) {
34+
$request->markCanceled();
35+
36+
return;
37+
}
38+
3339
if (true === isset($model['authorization'])) {
3440
$authorization = PaymentAuthorization::fromArray($model['authorization']);
3541
if ($authorization->isValid()) {

src/Api.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class Api
3131
const PROPERTY_SECURITY_TOKEN = 'securitytoken';
3232

3333
const STATUS_SUCCESS = '000';
34+
const STATUS_CANCELED = '-1';
3435

3536
private static $currencies = [
3637
self::CURRENCY_GBP => '826',
@@ -97,6 +98,12 @@ public function authorizeTransaction(array $model, array $authorizationRequest)
9798
$payment = Payment::fromArray($model['payment']);
9899
$authorization = PaymentAuthorization::fromSetefiResponse($authorizationRequest);
99100

101+
if (true === $authorization->isCanceled()) {
102+
$model['cancellation'] = $authorization->toArray();
103+
104+
return $model;
105+
}
106+
100107
if (false === $payment->isMatchingAuthorization($authorization)) {
101108
throw new InvalidArgumentException('Invalid payment authorization');
102109
}

src/Response/PaymentAuthorization.php

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ class PaymentAuthorization
2222

2323
/**
2424
* @param string $payment
25+
* @param string $result
26+
* @param string $code
2527
* @param string $token
2628
* @param string $authorization
27-
* @param string $code
28-
* @param string $result
2929
*/
30-
public function __construct($payment, $token, $authorization, $code, $result)
30+
public function __construct($payment, $result, $code, $token = null, $authorization = null)
3131
{
3232
$this->payment = $payment;
33+
$this->result = $result;
34+
$this->code = $code;
3335
$this->token = $token;
3436
$this->authorization = $authorization;
35-
$this->code = $code;
36-
$this->result = $result;
3737
}
3838

3939
/**
@@ -43,13 +43,13 @@ public function __construct($payment, $token, $authorization, $code, $result)
4343
*/
4444
public static function fromSetefiResponse(array $response)
4545
{
46-
return new self(
47-
$response[Api::PROPERTY_PAYMENT_ID],
48-
$response[Api::PROPERTY_SECURITY_TOKEN],
49-
$response[Api::PROPERTY_AUTHORIZATION],
50-
$response[Api::PROPERTY_RESPONSE_CODE],
51-
$response[Api::PROPERTY_RESULT]
52-
);
46+
$payment = $response[Api::PROPERTY_PAYMENT_ID];
47+
$result = $response[Api::PROPERTY_RESULT];
48+
$code = isset($response[Api::PROPERTY_RESPONSE_CODE]) ? $response[Api::PROPERTY_RESPONSE_CODE] : Api::STATUS_CANCELED;
49+
$token = isset($response[Api::PROPERTY_SECURITY_TOKEN]) ? $response[Api::PROPERTY_SECURITY_TOKEN] : null;
50+
$authorization = isset($response[Api::PROPERTY_AUTHORIZATION]) ? $response[Api::PROPERTY_AUTHORIZATION] : null;
51+
52+
return new self($payment, $result, $code, $token, $authorization);
5353
}
5454

5555
/**
@@ -61,10 +61,10 @@ public static function fromArray(array $array)
6161
{
6262
return new self(
6363
$array['payment'],
64-
$array['token'],
65-
$array['authorization'],
64+
$array['result'],
6665
$array['code'],
67-
$array['result']
66+
$array['token'],
67+
$array['authorization']
6868
);
6969
}
7070

@@ -75,10 +75,10 @@ public function toArray()
7575
{
7676
return [
7777
'payment' => $this->payment,
78+
'result' => $this->result,
79+
'code' => $this->code,
7880
'token' => $this->token,
7981
'authorization' => $this->authorization,
80-
'code' => $this->code,
81-
'result' => $this->result,
8282
];
8383
}
8484

@@ -100,4 +100,12 @@ public function isValid()
100100
{
101101
return $this->code === Api::STATUS_SUCCESS;
102102
}
103+
104+
/**
105+
* @return bool
106+
*/
107+
public function isCanceled()
108+
{
109+
return $this->code === Api::STATUS_CANCELED;
110+
}
103111
}

src/SetefiGatewayFactory.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Payum\Core\GatewayFactory;
77
use Webburza\Payum\Setefi\Action\AuthorizeAction;
88
use Webburza\Payum\Setefi\Action\CaptureAction;
9+
use Webburza\Payum\Setefi\Action\CancelAction;
910
use Webburza\Payum\Setefi\Action\ConvertPaymentAction;
1011
use Webburza\Payum\Setefi\Action\CreateTransactionAction;
1112
use Webburza\Payum\Setefi\Action\RefundAction;
@@ -31,6 +32,7 @@ protected function populateConfig(ArrayObject $config)
3132
'payum.action.capture' => new CaptureAction(),
3233
'payum.action.authorize' => new AuthorizeAction(),
3334
'payum.action.refund' => new RefundAction(),
35+
'payum.action.cancel' => new CancelAction(),
3436
'payum.action.status' => new StatusAction(),
3537
'payum.action.convert_payment' => new ConvertPaymentAction(),
3638

0 commit comments

Comments
 (0)