Skip to content

Commit e5053e0

Browse files
PHPSDK-156: Add getGatewayId for TransactionResponse class (#351)
1 parent b88fa07 commit e5053e0

File tree

2 files changed

+104
-1
lines changed

2 files changed

+104
-1
lines changed

src/Api/Transactions/TransactionResponse.php

+22-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
*/
3131
class TransactionResponse extends ResponseBody
3232
{
33-
3433
/**
3534
* @var string
3635
*/
@@ -382,4 +381,26 @@ public function requiresShoppingCart(): bool
382381
{
383382
return in_array($this->getPaymentDetails()->getType(), Gateways::SHOPPING_CART_REQUIRED_GATEWAYS, true);
384383
}
384+
385+
/**
386+
* Retrieve the gateway ID from the transaction
387+
* Will return multiple gateway IDs as a string of comma separated values if multiple payment methods are used
388+
*
389+
* @return string
390+
*/
391+
public function getGatewayId(): string
392+
{
393+
$paymentMethods = [];
394+
395+
foreach ($this->getPaymentMethods() as $paymentMethod) {
396+
if ($paymentMethod->getType() === 'COUPON') {
397+
$paymentMethods[] = $paymentMethod->getData()['coupon_brand'] ?? 'unknown';
398+
continue;
399+
}
400+
401+
$paymentMethods[] = $paymentMethod->getType();
402+
}
403+
404+
return implode(', ', $paymentMethods);
405+
}
385406
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* Copyright © MultiSafepay, Inc. All rights reserved.
4+
* See DISCLAIMER.md for disclaimer details.
5+
*/
6+
7+
namespace MultiSafepay\Tests\Api\Integration\Transactions;
8+
9+
use MultiSafepay\Api\Transactions\TransactionResponse;
10+
use PHPUnit\Framework\TestCase;
11+
12+
class TransactionResponseTest extends TestCase
13+
{
14+
/**
15+
* Test if getGatewayId will return a single gateway ID when only one payment method is used
16+
*
17+
* @return void
18+
*/
19+
public function testGetGatewayIdReturnsSingleGatewayIdWhenOnlyOnePaymentMethodIsUsed(): void
20+
{
21+
$transactionResponse = $this->createTransactionResponseWithPaymentMethods([
22+
['type' => 'VISA'],
23+
]);
24+
25+
$this->assertEquals('VISA', $transactionResponse->getGatewayId());
26+
}
27+
28+
/**
29+
* Test if getGatewayId will return multiple gateway IDs when multiple payment methods are used
30+
*
31+
* @return void
32+
*/
33+
public function testGetGatewayIdReturnsMultipleGatewayIdsWhenMultiplePaymentMethodsAreUsed(): void
34+
{
35+
$transactionResponse = $this->createTransactionResponseWithPaymentMethods([
36+
['type' => 'VISA'],
37+
['type' => 'MASTERCARD'],
38+
]);
39+
40+
$this->assertEquals('VISA, MASTERCARD', $transactionResponse->getGatewayId());
41+
}
42+
43+
/**
44+
* Test if getGatewayId will return the coupon brand when a coupon payment method is used
45+
*
46+
* @return void
47+
*/
48+
public function testGetGatewayIdReturnsCouponBrandWhenCouponPaymentMethodIsUsed(): void
49+
{
50+
$transactionResponse = $this->createTransactionResponseWithPaymentMethods([
51+
['type' => 'COUPON', 'coupon_brand' => 'GIFT'],
52+
]);
53+
54+
$this->assertEquals('GIFT', $transactionResponse->getGatewayId());
55+
}
56+
57+
/**
58+
* Test if getGatewayId will return unknown when a coupon payment method is used without a brand
59+
*
60+
* @return void
61+
*/
62+
public function testGetGatewayIdReturnsUnknownWhenCouponPaymentMethodIsUsedWithoutBrand(): void
63+
{
64+
$transactionResponse = $this->createTransactionResponseWithPaymentMethods([
65+
['type' => 'COUPON'],
66+
]);
67+
68+
$this->assertEquals('unknown', $transactionResponse->getGatewayId());
69+
}
70+
71+
/**
72+
* Retrieve a TransactionResponse with the provided payment methods
73+
*
74+
* @param array $paymentMethods
75+
* @return TransactionResponse
76+
*/
77+
private function createTransactionResponseWithPaymentMethods(array $paymentMethods): TransactionResponse
78+
{
79+
$data = ['payment_methods' => $paymentMethods];
80+
return new TransactionResponse($data);
81+
}
82+
}

0 commit comments

Comments
 (0)