Skip to content

Commit 04ae7b0

Browse files
authored
Merge pull request #68 from SwedbankPay/hotfix/failed-transactions
Add details of failed transactions
2 parents 3097420 + 504767c commit 04ae7b0

File tree

4 files changed

+163
-9
lines changed

4 files changed

+163
-9
lines changed

changelog.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
Version 4.1.1
2+
Changed
3+
- Add details of failed transactions
4+
15
Version 4.1.0
26
Added
37
- Partial capture and refund

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"email": "[email protected]"
1313
}
1414
],
15-
"version": "4.1.0",
15+
"version": "4.1.1",
1616
"keywords": [
1717
"SwedbankPay",
1818
"core",

src/SwedbankPay/Core/Api/Transaction.php

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,8 @@
2121
* @method $this setNumber($value)
2222
* @method int getAmount()
2323
* @method $this setAmount($value)
24-
* @method $this setVatAmount($value)
2524
* @method string getDescription()
2625
* @method $this setDescription($value)
27-
* @method $this setPayeeReference($value)
2826
*/
2927
class Transaction extends Data implements TransactionInterface
3028
{
@@ -38,6 +36,18 @@ public function __construct(array $data = [])
3836
$this->setData($data);
3937
}
4038

39+
/**
40+
* Set VAT amount.
41+
*
42+
* @param mixed $vatAmount
43+
*
44+
* @return $this
45+
*/
46+
public function setVatAmount($vatAmount)
47+
{
48+
return $this->setData(self::VAT_AMOUNT, $vatAmount);
49+
}
50+
4151
/**
4252
* Get VAT amount.
4353
*
@@ -48,6 +58,18 @@ public function getVatAmount()
4858
return $this->getData(self::VAT_AMOUNT);
4959
}
5060

61+
/**
62+
* Set Payee Reference.
63+
*
64+
* @param string $payeeReference
65+
*
66+
* @return $this
67+
*/
68+
public function setPayeeReference($payeeReference)
69+
{
70+
return $this->setData(self::PAYEE_REFERENCE, $payeeReference);
71+
}
72+
5173
/**
5274
* Get Payee Reference.
5375
*
@@ -98,16 +120,21 @@ public function getFailedErrorDescription()
98120
*/
99121
public function getFailedDetails()
100122
{
101-
if ($this->hasData('problem')) {
123+
if ($this->getData(self::PROBLEM)) {
102124
return $this->getProblem()->toString();
103125
}
104126

105127
// Deprecated
106-
return implode('; ', [
107-
$this->getFailedReason(),
108-
$this->getFailedErrorCode(),
109-
$this->getFailedErrorDescription()
110-
]);
128+
if (!empty($this->getFailedReason())) {
129+
return implode('; ', [
130+
$this->getFailedReason(),
131+
$this->getFailedErrorCode(),
132+
$this->getFailedErrorDescription()
133+
]);
134+
}
135+
136+
// No details
137+
return 'Transaction has been failed, no details';
111138
}
112139

113140
/**

tests/unit/TransactionTest.php

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
<?php
2+
3+
use SwedbankPay\Core\Api\Transaction;
4+
use SwedbankPay\Core\Api\TransactionInterface;
5+
use SwedbankPay\Core\Api\Problem;
6+
use SwedbankPay\Core\Api\ProblemInterface;
7+
8+
class TransactionTest extends TestCase
9+
{
10+
public function testGetters()
11+
{
12+
$data = '{
13+
"id": "/psp/creditcard/payments/d0b69e11-a6b5-4ef7-8b95-08d94fff2cbf/transactions/dd08a774-34a0-40ee-bb7b-08d94fcc1da0",
14+
"created": "2021-08-02T05:55:05.4437298Z",
15+
"updated": "2021-08-02T05:55:11.9725364Z",
16+
"type": "Authorization",
17+
"state": "Completed",
18+
"number": 40108027689,
19+
"amount": 28125,
20+
"vatAmount": 5625,
21+
"description": "Order #33787",
22+
"payeeReference": "33787xljqrs",
23+
"isOperational": false,
24+
"operations": []
25+
}';
26+
$data = json_decode($data, true);
27+
28+
$transaction = new Transaction($data);
29+
$this->assertInstanceOf(TransactionInterface::class, $transaction);
30+
$this->assertEquals(
31+
'/psp/creditcard/payments/d0b69e11-a6b5-4ef7-8b95-08d94fff2cbf/transactions/dd08a774-34a0-40ee-bb7b-08d94fcc1da0',
32+
$transaction->getId()
33+
);
34+
$this->assertEquals('2021-08-02T05:55:05.4437298Z', $transaction->getCreated());
35+
$this->assertEquals('2021-08-02T05:55:11.9725364Z', $transaction->getUpdated());
36+
$this->assertEquals(TransactionInterface::TYPE_AUTHORIZATION, $transaction->getType());
37+
$this->assertEquals(40108027689, $transaction->getNumber());
38+
$this->assertEquals(28125, $transaction->getAmount());
39+
$this->assertEquals(5625, $transaction->getVatAmount());
40+
$this->assertEquals('33787xljqrs', $transaction->getPayeeReference());
41+
$this->assertEquals(false, $transaction->isInitialized());
42+
$this->assertEquals(false, $transaction->isAwaitingActivity());
43+
$this->assertEquals(false, $transaction->isPending());
44+
$this->assertEquals(true, $transaction->isCompleted());
45+
$this->assertEquals(false, $transaction->isFailed());
46+
$this->assertEquals(false, $transaction->isInitialized());
47+
}
48+
49+
public function testSetters()
50+
{
51+
$transaction = new Transaction([]);
52+
$transaction->setId(
53+
'/psp/creditcard/payments/d0b69e11-a6b5-4ef7-8b95-08d94fff2cbf/transactions/dd08a774-34a0-40ee-bb7b-08d94fcc1da0'
54+
)
55+
->setCreated('2021-08-02T05:55:05.4437298Z')
56+
->setUpdated('2021-08-02T05:55:11.9725364Z')
57+
->setType(TransactionInterface::TYPE_AUTHORIZATION)
58+
->setState(TransactionInterface::STATE_COMPLETED)
59+
->setNumber(40108027689)
60+
->setAmount(28125)
61+
->setVatAmount(5625)
62+
->setDescription('Order #33787')
63+
->setPayeeReference('33787xljqrs');
64+
65+
$this->assertInstanceOf(TransactionInterface::class, $transaction);
66+
$this->assertEquals(
67+
'/psp/creditcard/payments/d0b69e11-a6b5-4ef7-8b95-08d94fff2cbf/transactions/dd08a774-34a0-40ee-bb7b-08d94fcc1da0',
68+
$transaction->getId()
69+
);
70+
$this->assertEquals('2021-08-02T05:55:05.4437298Z', $transaction->getCreated());
71+
$this->assertEquals('2021-08-02T05:55:11.9725364Z', $transaction->getUpdated());
72+
$this->assertEquals(TransactionInterface::TYPE_AUTHORIZATION, $transaction->getType());
73+
$this->assertEquals(40108027689, $transaction->getNumber());
74+
$this->assertEquals(28125, $transaction->getAmount());
75+
$this->assertEquals(5625, $transaction->getVatAmount());
76+
$this->assertEquals('33787xljqrs', $transaction->getPayeeReference());
77+
$this->assertEquals(false, $transaction->isInitialized());
78+
$this->assertEquals(false, $transaction->isAwaitingActivity());
79+
$this->assertEquals(false, $transaction->isPending());
80+
$this->assertEquals(true, $transaction->isCompleted());
81+
$this->assertEquals(false, $transaction->isFailed());
82+
$this->assertEquals(false, $transaction->isInitialized());
83+
}
84+
85+
public function testProblem()
86+
{
87+
$data = '{
88+
"id": "/psp/creditcard/payments/dce2cabe-8382-46b8-58ae-08d94fcbe1c7/transactions/857f0f4a-511b-42da-f730-08d94fff3046",
89+
"created": "2021-07-31T07:48:26.9976381Z",
90+
"updated": "2021-07-31T07:50:12.3446716Z",
91+
"type": "Authorization",
92+
"state": "Failed",
93+
"number": 40108019523,
94+
"amount": 28125,
95+
"vatAmount": 5625,
96+
"description": "Order #33782",
97+
"payeeReference": "33782xktuyo",
98+
"failedReason": "PspProblemException",
99+
"failedActivityName": "VerifyAuthentication",
100+
"isOperational": false,
101+
"problem": {
102+
"type": "https://api.payex.com/psp/errordetail/creditcard/systemerror",
103+
"title": "Error in system",
104+
"status": 500,
105+
"detail": "Unable to complete operation, error calling 3rd party",
106+
"problems": [{
107+
"name": "CommunicationError",
108+
"description": "Unexpected communication behavior"
109+
}]
110+
}
111+
}';
112+
$data = json_decode($data, true);
113+
114+
$transaction = new Transaction($data);
115+
$this->assertInstanceOf(TransactionInterface::class, $transaction);
116+
$this->assertEquals(true, $transaction->isFailed());
117+
$this->assertInstanceOf(Problem::class, $transaction->getProblem());
118+
$this->assertInstanceOf(ProblemInterface::class, $transaction->getProblem());
119+
$this->assertEquals('PspProblemException', $transaction->getFailedReason());
120+
$this->assertEquals('VerifyAuthentication', $transaction->getData('failedActivityName'));
121+
$this->assertEquals('(CommunicationError) Unexpected communication behavior', $transaction->getProblem()->toString());
122+
}
123+
}

0 commit comments

Comments
 (0)