Skip to content

Commit 144deda

Browse files
committed
Add & Update: raw/metadata/paymentMethod field in Transaction & Gateway Response
1 parent d9c4477 commit 144deda

File tree

12 files changed

+117
-39
lines changed

12 files changed

+117
-39
lines changed

Gateway/PaypalPaymentGateway.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public function getResponse(
4747
}
4848

4949
$gatewayResponse = (new GatewayResponse())
50+
->setRaw($request->request->all())
5051
->setDate(new \DateTime())
5152
->setStatus(PaymentStatus::STATUS_FAILED)
5253
;
@@ -61,9 +62,11 @@ public function getResponse(
6162
$amount = $paypalPayment->getTransactions()[0]->getAmount();
6263

6364
$gatewayResponse
65+
->setPaymentMethod($paypalPayment->getPayer()->getPaymentMethod())
6466
->setTransactionUuid($request->get('transactionID'))
6567
->setAmount($amount->total * 100)
6668
->setCurrencyCode($amount->currency)
69+
->setRaw($paypalPayment->toArray())
6770
;
6871

6972
$execution = new PaymentExecution();

Gateway/SystemPayPaymentGateway.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@ public function getResponse(
107107
->setCurrencyCode((new ISO4217())->findByNumeric($requestData->get('vads_currency'))->getAlpha3())
108108
->setDate(new \DateTime())
109109
->setStatus(PaymentStatus::STATUS_FAILED)
110+
->setPaymentMethod($requestData->get('vads_card_brand'))
110111
;
111112

112113
if ($requestData->get('vads_ctx_mode') != $paymentGatewayConfiguration->get('ctx_mode')) {

Model/GatewayResponse.php

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ class GatewayResponse
99
*/
1010
private $transactionUuid;
1111

12+
/**
13+
* @var string
14+
*/
15+
private $paymentMethod;
16+
1217
/**
1318
* @var int
1419
*/
@@ -62,6 +67,18 @@ public function setTransactionUuid(string $transactionUuid): self
6267
return $this;
6368
}
6469

70+
public function getPaymentMethod(): ?string
71+
{
72+
return $this->paymentMethod;
73+
}
74+
75+
public function setPaymentMethod(?string $paymentMethod): self
76+
{
77+
$this->paymentMethod = $paymentMethod;
78+
79+
return $this;
80+
}
81+
6582
public function getAmount(): ?int
6683
{
6784
return $this->amount;
@@ -122,17 +139,13 @@ public function setDate(\DateTime $date): self
122139
return $this;
123140
}
124141

125-
public function getRaw(): ?string
142+
public function getRaw(): ?array
126143
{
127144
return $this->raw;
128145
}
129146

130147
public function setRaw($raw): self
131148
{
132-
if (is_array($raw)) {
133-
$raw = json_encode($raw);
134-
}
135-
136149
$this->raw = $raw;
137150

138151
return $this;

Model/Transaction.php

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ class Transaction
2121
*/
2222
protected $gatewayConfigurationAlias;
2323

24+
/**
25+
* @var string
26+
*/
27+
protected $paymentMethod;
28+
2429
/**
2530
* @var string
2631
*/
@@ -59,7 +64,12 @@ class Transaction
5964
/**
6065
* @var array
6166
*/
62-
protected $metadatas;
67+
protected $metadata;
68+
69+
/**
70+
* @var array
71+
*/
72+
protected $raw;
6373

6474
/**
6575
* @var \DateTime
@@ -86,14 +96,16 @@ public function toArray(): array
8696
return [
8797
'id' => $this->id,
8898
'gateway_configuration_alias' => $this->gatewayConfigurationAlias,
99+
'payment_method' => $this->paymentMethod,
89100
'item_id' => $this->itemId,
90101
'customer_id' => $this->customerId,
91102
'customer_email' => $this->customerEmail,
92103
'status' => $this->status,
93104
'amount' => $this->amount,
94105
'currency_code' => $this->currencyCode,
95106
'description' => $this->description,
96-
'metadatas' => $this->metadatas,
107+
'metadata' => $this->metadata,
108+
'raw' => $this->raw,
97109
'createdAt' => $this->createdAt,
98110
'updatedAt' => $this->updatedAt,
99111
];
@@ -135,6 +147,18 @@ public function setGatewayConfigurationAlias(string $gatewayConfigurationAlias):
135147
return $this;
136148
}
137149

150+
public function getPaymentMethod(): ?string
151+
{
152+
return $this->paymentMethod;
153+
}
154+
155+
public function setPaymentMethod(?string $paymentMethod): self
156+
{
157+
$this->paymentMethod = $paymentMethod;
158+
159+
return $this;
160+
}
161+
138162
public function getItemId(): ?string
139163
{
140164
return $this->itemId;
@@ -221,31 +245,42 @@ public function setDescription(?string $description): self
221245

222246
public function hasMetadata(string $key)
223247
{
224-
return isset($this->metadatas[$key]);
248+
return isset($this->metadata[$key]);
225249
}
226250

227-
public function getMetadata(string $key)
251+
public function getMetadata(?string $key = null)
228252
{
229-
return $this->metadatas[$key];
253+
if (null === $key) {
254+
return $this->metadata;
255+
}
256+
257+
return $this->hasMetadata($key) ? $this->metadata[$key] : null;
230258
}
231259

232260
public function addMetadata(string $key, $value)
233261
{
234-
$this->metadatas[$key] = $value;
262+
$this->metadata[$key] = $value;
235263
}
236264

237-
public function getMetadatas(): ?array
265+
public function setMetadata(array $metadata): self
238266
{
239-
return $this->metadatas;
267+
$this->metadata = [];
268+
269+
foreach ($metadata as $key => $value) {
270+
$this->addMetadata($key, $value);
271+
}
272+
273+
return $this;
240274
}
241275

242-
public function setMetadatas(array $metadatas): self
276+
public function getRaw(): ?array
243277
{
244-
$this->metadatas = [];
278+
return $this->raw;
279+
}
245280

246-
foreach ($metadatas as $key => $value) {
247-
$this->addMetadata($key, $value);
248-
}
281+
public function setRaw(?array $raw = []): self
282+
{
283+
$this->raw = $raw;
249284

250285
return $this;
251286
}

Payment/PaymentContext.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,11 @@ public function handleGatewayCallback(Request $request): Transaction
9393
]);
9494
}
9595

96-
return $transaction->setStatus($status);
96+
return $transaction
97+
->setStatus($status)
98+
->setPaymentMethod($gatewayResponse->getPaymentMethod())
99+
->setRaw($gatewayResponse->getRaw())
100+
;
97101
}
98102

99103
public function hasTransaction(): bool

Payment/TransactionFactory.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,16 @@ public function create(array $parameters): Transaction
3434
->setId($resolvedParameters['id'])
3535
->setNumber($resolvedParameters['number'])
3636
->setItemId($resolvedParameters['item_id'])
37+
->setPaymentMethod($resolvedParameters['payment_method'])
3738
->setGatewayConfigurationAlias($resolvedParameters['gateway_configuration_alias'])
3839
->setCustomerId($resolvedParameters['customer_id'])
3940
->setCustomerEmail($resolvedParameters['customer_email'])
4041
->setStatus($resolvedParameters['status'])
4142
->setAmount($resolvedParameters['amount'])
4243
->setCurrencyCode($resolvedParameters['currency_code'])
4344
->setDescription($resolvedParameters['description'])
44-
->setMetadatas($resolvedParameters['metadatas'])
45+
->setMetadata($resolvedParameters['metadata'])
46+
->setRaw($resolvedParameters['raw'])
4547
;
4648
}
4749

@@ -63,21 +65,25 @@ protected function configureParameters(OptionsResolver $resolver)
6365
'id' => Flaky::id(62),
6466
'number' => null,
6567
'gateway_configuration_alias' => null,
68+
'payment_method' => null,
6669
'customer_id' => null,
6770
'customer_email' => null,
6871
'description' => null,
69-
'metadatas' => [],
72+
'metadata' => [],
73+
'raw' => [],
7074
'status' => PaymentStatus::STATUS_CREATED,
7175
])
7276
->setAllowedTypes('item_id', ['int', 'string'])
7377
->setAllowedTypes('number', ['null', 'int'])
7478
->setAllowedTypes('gateway_configuration_alias', ['null', 'string'])
79+
->setAllowedTypes('payment_method', ['null', 'string'])
7580
->setAllowedTypes('amount', ['int', 'double', 'string'])
7681
->setAllowedTypes('currency_code', 'string')
7782
->setAllowedTypes('customer_id', ['null', 'int', 'string'])
7883
->setAllowedTypes('customer_email', ['null', 'string'])
7984
->setAllowedTypes('description', ['null', 'string'])
80-
->setAllowedTypes('metadatas', ['null', 'array'])
85+
->setAllowedTypes('metadata', ['null', 'array'])
86+
->setAllowedTypes('raw', ['null', 'array'])
8187
->setAllowedTypes('status', ['string'])
8288
->setAllowedValues('status', [
8389
PaymentStatus::STATUS_APPROVED,

Resources/config/doctrine/Transaction.orm.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@
77
<id name="id" type="string" column="id"></id>
88
<field name="itemId" type="string" column="item_id" />
99
<field name="gatewayConfigurationAlias" type="string" column="gateway_configuration_alias" />
10+
<field name="paymentMethod" type="string" column="payment_method" nullable="true" />
1011
<field name="customerId" type="string" column="customer_id" nullable="true" />
1112
<field name="customerEmail" type="string" column="customer_email" nullable="true" />
1213
<field name="status" type="string" column="status" />
1314
<field name="amount" type="integer" column="amount" />
1415
<field name="currencyCode" type="string" column="currency_code" />
1516
<field name="description" type="text" column="description" nullable="true" />
16-
<field name="metadatas" type="json_array" column="metadatas" />
17+
<field name="metadata" type="json_array" column="metadata" />
18+
<field name="raw" type="json_array" column="raw" />
1719
<field name="createdAt" type="datetime" column="created_at" />
1820
<field name="updatedAt" type="datetime" column="updated_at" />
1921
<lifecycle-callbacks>

Resources/config/event_actions.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ idci_step:
1414
customer_id: {extra_form_type: "text", options: {required: false}}
1515
customer_email: {extra_form_type: "text", options: {required: false}}
1616
description: {extra_form_type: "text", options: {required: false}}
17+
metadata: {extra_form_type: "text", options: {required: false}}
1718
success_message: {extra_form_type: "text", options: {required: false}}
1819
error_message: {extra_form_type: "text", options: {required: false}}
1920
template_extra_vars: {extra_form_type: "text", options: {required: false}}

Step/Event/Action/ManageTransactionStepEventAction.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ private function prepareInitializeTransaction(StepEventInterface $event, array $
121121
'customer_id' => $parameters['customer_id'],
122122
'customer_email' => $parameters['customer_email'],
123123
'description' => $parameters['description'],
124+
'metadata' => $parameters['metadata'],
124125
]);
125126

126127
$paymentGatewayConfiguration = $paymentContext->getPaymentGatewayConfiguration();
@@ -225,6 +226,7 @@ protected function setDefaultParameters(OptionsResolver $resolver)
225226
'customer_id' => null,
226227
'customer_email' => null,
227228
'description' => null,
229+
'metadata' => [],
228230
'success_message' => 'Your transaction succeeded.',
229231
'error_message' => 'There was a problem with your transaction, please try again.',
230232
'template_extra_vars' => [],
@@ -237,6 +239,7 @@ protected function setDefaultParameters(OptionsResolver $resolver)
237239
->setAllowedTypes('customer_id', ['null', 'string'])
238240
->setAllowedTypes('customer_email', ['null', 'string'])
239241
->setAllowedTypes('description', ['null', 'string'])
242+
->setAllowedTypes('metadata', ['array'])
240243
->setAllowedTypes('success_message', ['null', 'string'])
241244
->setAllowedTypes('error_message', ['null', 'string'])
242245
->setAllowedTypes('template_extra_vars', ['array'])
@@ -246,6 +249,16 @@ function (OptionsResolver $options, $value) {
246249
return (bool) $value;
247250
}
248251
)
252+
->setNormalizer(
253+
'metadata',
254+
function (OptionsResolver $options, $metadata) {
255+
array_walk_recursive($metadata, function (&$value, $key) {
256+
$value = json_decode($value, true) ?? $value;
257+
});
258+
259+
return $metadata;
260+
}
261+
)
249262
->setNormalizer(
250263
'template_extra_vars',
251264
function (OptionsResolver $options, $templateExtraVars) {

Tests/Unit/Gateway/PaymentGatewayTestCase.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22

33
namespace IDCI\Bundle\PaymentBundle\Tests\Unit\Gateway;
44

5-
use PHPUnit\Framework\TestCase;
6-
use Twig\Environment as TwigEnvironment;
7-
use Twig\Loader\FilesystemLoader;
85
use IDCI\Bundle\PaymentBundle\Model\PaymentGatewayConfiguration;
96
use IDCI\Bundle\PaymentBundle\Model\Transaction;
107
use IDCI\Bundle\PaymentBundle\Payment\TransactionFactory;
8+
use PHPUnit\Framework\TestCase;
9+
use Twig\Environment as TwigEnvironment;
10+
use Twig\Loader\FilesystemLoader;
1111

1212
class PaymentGatewayTestCase extends TestCase
1313
{
@@ -50,7 +50,7 @@ public function setUp()
5050
'amount' => 100,
5151
'currency_code' => 'EUR',
5252
'description' => 'Dummy description',
53-
'metadatas' => [],
53+
'metadata' => [],
5454
]);
5555
}
5656
}

0 commit comments

Comments
 (0)