Skip to content

Commit

Permalink
Add & Update: raw/metadata/paymentMethod field in Transaction & Gatew…
Browse files Browse the repository at this point in the history
…ay Response
  • Loading branch information
BeBlood committed Oct 7, 2019
1 parent d9c4477 commit 144deda
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 39 deletions.
3 changes: 3 additions & 0 deletions Gateway/PaypalPaymentGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public function getResponse(
}

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

$gatewayResponse
->setPaymentMethod($paypalPayment->getPayer()->getPaymentMethod())
->setTransactionUuid($request->get('transactionID'))
->setAmount($amount->total * 100)
->setCurrencyCode($amount->currency)
->setRaw($paypalPayment->toArray())
;

$execution = new PaymentExecution();
Expand Down
1 change: 1 addition & 0 deletions Gateway/SystemPayPaymentGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ public function getResponse(
->setCurrencyCode((new ISO4217())->findByNumeric($requestData->get('vads_currency'))->getAlpha3())
->setDate(new \DateTime())
->setStatus(PaymentStatus::STATUS_FAILED)
->setPaymentMethod($requestData->get('vads_card_brand'))
;

if ($requestData->get('vads_ctx_mode') != $paymentGatewayConfiguration->get('ctx_mode')) {
Expand Down
23 changes: 18 additions & 5 deletions Model/GatewayResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ class GatewayResponse
*/
private $transactionUuid;

/**
* @var string
*/
private $paymentMethod;

/**
* @var int
*/
Expand Down Expand Up @@ -62,6 +67,18 @@ public function setTransactionUuid(string $transactionUuid): self
return $this;
}

public function getPaymentMethod(): ?string
{
return $this->paymentMethod;
}

public function setPaymentMethod(?string $paymentMethod): self
{
$this->paymentMethod = $paymentMethod;

return $this;
}

public function getAmount(): ?int
{
return $this->amount;
Expand Down Expand Up @@ -122,17 +139,13 @@ public function setDate(\DateTime $date): self
return $this;
}

public function getRaw(): ?string
public function getRaw(): ?array
{
return $this->raw;
}

public function setRaw($raw): self
{
if (is_array($raw)) {
$raw = json_encode($raw);
}

$this->raw = $raw;

return $this;
Expand Down
61 changes: 48 additions & 13 deletions Model/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ class Transaction
*/
protected $gatewayConfigurationAlias;

/**
* @var string
*/
protected $paymentMethod;

/**
* @var string
*/
Expand Down Expand Up @@ -59,7 +64,12 @@ class Transaction
/**
* @var array
*/
protected $metadatas;
protected $metadata;

/**
* @var array
*/
protected $raw;

/**
* @var \DateTime
Expand All @@ -86,14 +96,16 @@ public function toArray(): array
return [
'id' => $this->id,
'gateway_configuration_alias' => $this->gatewayConfigurationAlias,
'payment_method' => $this->paymentMethod,
'item_id' => $this->itemId,
'customer_id' => $this->customerId,
'customer_email' => $this->customerEmail,
'status' => $this->status,
'amount' => $this->amount,
'currency_code' => $this->currencyCode,
'description' => $this->description,
'metadatas' => $this->metadatas,
'metadata' => $this->metadata,
'raw' => $this->raw,
'createdAt' => $this->createdAt,
'updatedAt' => $this->updatedAt,
];
Expand Down Expand Up @@ -135,6 +147,18 @@ public function setGatewayConfigurationAlias(string $gatewayConfigurationAlias):
return $this;
}

public function getPaymentMethod(): ?string
{
return $this->paymentMethod;
}

public function setPaymentMethod(?string $paymentMethod): self
{
$this->paymentMethod = $paymentMethod;

return $this;
}

public function getItemId(): ?string
{
return $this->itemId;
Expand Down Expand Up @@ -221,31 +245,42 @@ public function setDescription(?string $description): self

public function hasMetadata(string $key)
{
return isset($this->metadatas[$key]);
return isset($this->metadata[$key]);
}

public function getMetadata(string $key)
public function getMetadata(?string $key = null)
{
return $this->metadatas[$key];
if (null === $key) {
return $this->metadata;
}

return $this->hasMetadata($key) ? $this->metadata[$key] : null;
}

public function addMetadata(string $key, $value)
{
$this->metadatas[$key] = $value;
$this->metadata[$key] = $value;
}

public function getMetadatas(): ?array
public function setMetadata(array $metadata): self
{
return $this->metadatas;
$this->metadata = [];

foreach ($metadata as $key => $value) {
$this->addMetadata($key, $value);
}

return $this;
}

public function setMetadatas(array $metadatas): self
public function getRaw(): ?array
{
$this->metadatas = [];
return $this->raw;
}

foreach ($metadatas as $key => $value) {
$this->addMetadata($key, $value);
}
public function setRaw(?array $raw = []): self
{
$this->raw = $raw;

return $this;
}
Expand Down
6 changes: 5 additions & 1 deletion Payment/PaymentContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,11 @@ public function handleGatewayCallback(Request $request): Transaction
]);
}

return $transaction->setStatus($status);
return $transaction
->setStatus($status)
->setPaymentMethod($gatewayResponse->getPaymentMethod())
->setRaw($gatewayResponse->getRaw())
;
}

public function hasTransaction(): bool
Expand Down
12 changes: 9 additions & 3 deletions Payment/TransactionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,16 @@ public function create(array $parameters): Transaction
->setId($resolvedParameters['id'])
->setNumber($resolvedParameters['number'])
->setItemId($resolvedParameters['item_id'])
->setPaymentMethod($resolvedParameters['payment_method'])
->setGatewayConfigurationAlias($resolvedParameters['gateway_configuration_alias'])
->setCustomerId($resolvedParameters['customer_id'])
->setCustomerEmail($resolvedParameters['customer_email'])
->setStatus($resolvedParameters['status'])
->setAmount($resolvedParameters['amount'])
->setCurrencyCode($resolvedParameters['currency_code'])
->setDescription($resolvedParameters['description'])
->setMetadatas($resolvedParameters['metadatas'])
->setMetadata($resolvedParameters['metadata'])
->setRaw($resolvedParameters['raw'])
;
}

Expand All @@ -63,21 +65,25 @@ protected function configureParameters(OptionsResolver $resolver)
'id' => Flaky::id(62),
'number' => null,
'gateway_configuration_alias' => null,
'payment_method' => null,
'customer_id' => null,
'customer_email' => null,
'description' => null,
'metadatas' => [],
'metadata' => [],
'raw' => [],
'status' => PaymentStatus::STATUS_CREATED,
])
->setAllowedTypes('item_id', ['int', 'string'])
->setAllowedTypes('number', ['null', 'int'])
->setAllowedTypes('gateway_configuration_alias', ['null', 'string'])
->setAllowedTypes('payment_method', ['null', 'string'])
->setAllowedTypes('amount', ['int', 'double', 'string'])
->setAllowedTypes('currency_code', 'string')
->setAllowedTypes('customer_id', ['null', 'int', 'string'])
->setAllowedTypes('customer_email', ['null', 'string'])
->setAllowedTypes('description', ['null', 'string'])
->setAllowedTypes('metadatas', ['null', 'array'])
->setAllowedTypes('metadata', ['null', 'array'])
->setAllowedTypes('raw', ['null', 'array'])
->setAllowedTypes('status', ['string'])
->setAllowedValues('status', [
PaymentStatus::STATUS_APPROVED,
Expand Down
4 changes: 3 additions & 1 deletion Resources/config/doctrine/Transaction.orm.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@
<id name="id" type="string" column="id"></id>
<field name="itemId" type="string" column="item_id" />
<field name="gatewayConfigurationAlias" type="string" column="gateway_configuration_alias" />
<field name="paymentMethod" type="string" column="payment_method" nullable="true" />
<field name="customerId" type="string" column="customer_id" nullable="true" />
<field name="customerEmail" type="string" column="customer_email" nullable="true" />
<field name="status" type="string" column="status" />
<field name="amount" type="integer" column="amount" />
<field name="currencyCode" type="string" column="currency_code" />
<field name="description" type="text" column="description" nullable="true" />
<field name="metadatas" type="json_array" column="metadatas" />
<field name="metadata" type="json_array" column="metadata" />
<field name="raw" type="json_array" column="raw" />
<field name="createdAt" type="datetime" column="created_at" />
<field name="updatedAt" type="datetime" column="updated_at" />
<lifecycle-callbacks>
Expand Down
1 change: 1 addition & 0 deletions Resources/config/event_actions.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ idci_step:
customer_id: {extra_form_type: "text", options: {required: false}}
customer_email: {extra_form_type: "text", options: {required: false}}
description: {extra_form_type: "text", options: {required: false}}
metadata: {extra_form_type: "text", options: {required: false}}
success_message: {extra_form_type: "text", options: {required: false}}
error_message: {extra_form_type: "text", options: {required: false}}
template_extra_vars: {extra_form_type: "text", options: {required: false}}
13 changes: 13 additions & 0 deletions Step/Event/Action/ManageTransactionStepEventAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ private function prepareInitializeTransaction(StepEventInterface $event, array $
'customer_id' => $parameters['customer_id'],
'customer_email' => $parameters['customer_email'],
'description' => $parameters['description'],
'metadata' => $parameters['metadata'],
]);

$paymentGatewayConfiguration = $paymentContext->getPaymentGatewayConfiguration();
Expand Down Expand Up @@ -225,6 +226,7 @@ protected function setDefaultParameters(OptionsResolver $resolver)
'customer_id' => null,
'customer_email' => null,
'description' => null,
'metadata' => [],
'success_message' => 'Your transaction succeeded.',
'error_message' => 'There was a problem with your transaction, please try again.',
'template_extra_vars' => [],
Expand All @@ -237,6 +239,7 @@ protected function setDefaultParameters(OptionsResolver $resolver)
->setAllowedTypes('customer_id', ['null', 'string'])
->setAllowedTypes('customer_email', ['null', 'string'])
->setAllowedTypes('description', ['null', 'string'])
->setAllowedTypes('metadata', ['array'])
->setAllowedTypes('success_message', ['null', 'string'])
->setAllowedTypes('error_message', ['null', 'string'])
->setAllowedTypes('template_extra_vars', ['array'])
Expand All @@ -246,6 +249,16 @@ function (OptionsResolver $options, $value) {
return (bool) $value;
}
)
->setNormalizer(
'metadata',
function (OptionsResolver $options, $metadata) {
array_walk_recursive($metadata, function (&$value, $key) {
$value = json_decode($value, true) ?? $value;
});

return $metadata;
}
)
->setNormalizer(
'template_extra_vars',
function (OptionsResolver $options, $templateExtraVars) {
Expand Down
8 changes: 4 additions & 4 deletions Tests/Unit/Gateway/PaymentGatewayTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

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

use PHPUnit\Framework\TestCase;
use Twig\Environment as TwigEnvironment;
use Twig\Loader\FilesystemLoader;
use IDCI\Bundle\PaymentBundle\Model\PaymentGatewayConfiguration;
use IDCI\Bundle\PaymentBundle\Model\Transaction;
use IDCI\Bundle\PaymentBundle\Payment\TransactionFactory;
use PHPUnit\Framework\TestCase;
use Twig\Environment as TwigEnvironment;
use Twig\Loader\FilesystemLoader;

class PaymentGatewayTestCase extends TestCase
{
Expand Down Expand Up @@ -50,7 +50,7 @@ public function setUp()
'amount' => 100,
'currency_code' => 'EUR',
'description' => 'Dummy description',
'metadatas' => [],
'metadata' => [],
]);
}
}
10 changes: 5 additions & 5 deletions Tests/Unit/Payment/PaymentContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public function testCreateTransaction()
'amount' => 100,
'currency_code' => 'EUR',
'description' => 'Dummy description',
'metadatas' => [],
'metadata' => [],
];

$this->dispatcher
Expand All @@ -106,7 +106,7 @@ public function testCreateTransaction()
$this->assertEquals($transaction->getAmount(), $parameters['amount']);
$this->assertEquals($transaction->getCurrencyCode(), $parameters['currency_code']);
$this->assertEquals($transaction->getDescription(), $parameters['description']);
$this->assertEquals($transaction->getMetadatas(), $parameters['metadatas']);
$this->assertEquals($transaction->getMetadata(), $parameters['metadata']);
$this->assertEquals($transaction->getStatus(), PaymentStatus::STATUS_CREATED);
$this->assertEquals(
$transaction->getGatewayConfigurationAlias(),
Expand All @@ -126,7 +126,7 @@ public function testTransactionNotCreated()
'amount' => 100,
'currency_code' => 'EUR',
'description' => 'Dummy description',
'metadatas' => [],
'metadata' => [],
];

$this->dispatcher
Expand Down Expand Up @@ -296,7 +296,7 @@ public function getHandleGatewayCallbackDataProvider()
'amount' => 100,
'currency_code' => 'EUR',
'description' => 'Dummy description',
'metadatas' => [],
'metadata' => [],
];

return [
Expand All @@ -319,7 +319,7 @@ public function getTransactionDataProvider()
'amount' => 100,
'currency_code' => 'EUR',
'description' => 'Dummy description',
'metadatas' => [],
'metadata' => [],
];

return [
Expand Down
Loading

0 comments on commit 144deda

Please sign in to comment.