From 144deda8acfb21c60a0604f4d6255b0c3919f4fa Mon Sep 17 00:00:00 2001 From: Benjamin BOUDIER Date: Mon, 7 Oct 2019 11:29:45 +0200 Subject: [PATCH] Add & Update: raw/metadata/paymentMethod field in Transaction & Gateway Response --- Gateway/PaypalPaymentGateway.php | 3 + Gateway/SystemPayPaymentGateway.php | 1 + Model/GatewayResponse.php | 23 +++++-- Model/Transaction.php | 61 +++++++++++++++---- Payment/PaymentContext.php | 6 +- Payment/TransactionFactory.php | 12 +++- Resources/config/doctrine/Transaction.orm.xml | 4 +- Resources/config/event_actions.yml | 1 + .../ManageTransactionStepEventAction.php | 13 ++++ Tests/Unit/Gateway/PaymentGatewayTestCase.php | 8 +-- Tests/Unit/Payment/PaymentContextTest.php | 10 +-- Tests/Unit/Payment/TransactionFactoryTest.php | 14 ++--- 12 files changed, 117 insertions(+), 39 deletions(-) diff --git a/Gateway/PaypalPaymentGateway.php b/Gateway/PaypalPaymentGateway.php index 9df9d2b..250cf5a 100644 --- a/Gateway/PaypalPaymentGateway.php +++ b/Gateway/PaypalPaymentGateway.php @@ -47,6 +47,7 @@ public function getResponse( } $gatewayResponse = (new GatewayResponse()) + ->setRaw($request->request->all()) ->setDate(new \DateTime()) ->setStatus(PaymentStatus::STATUS_FAILED) ; @@ -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(); diff --git a/Gateway/SystemPayPaymentGateway.php b/Gateway/SystemPayPaymentGateway.php index fd094c1..1876133 100644 --- a/Gateway/SystemPayPaymentGateway.php +++ b/Gateway/SystemPayPaymentGateway.php @@ -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')) { diff --git a/Model/GatewayResponse.php b/Model/GatewayResponse.php index 41c101b..e5a1ccd 100644 --- a/Model/GatewayResponse.php +++ b/Model/GatewayResponse.php @@ -9,6 +9,11 @@ class GatewayResponse */ private $transactionUuid; + /** + * @var string + */ + private $paymentMethod; + /** * @var int */ @@ -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; @@ -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; diff --git a/Model/Transaction.php b/Model/Transaction.php index 808f07d..c3c6151 100644 --- a/Model/Transaction.php +++ b/Model/Transaction.php @@ -21,6 +21,11 @@ class Transaction */ protected $gatewayConfigurationAlias; + /** + * @var string + */ + protected $paymentMethod; + /** * @var string */ @@ -59,7 +64,12 @@ class Transaction /** * @var array */ - protected $metadatas; + protected $metadata; + + /** + * @var array + */ + protected $raw; /** * @var \DateTime @@ -86,6 +96,7 @@ 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, @@ -93,7 +104,8 @@ public function toArray(): array '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, ]; @@ -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; @@ -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; } diff --git a/Payment/PaymentContext.php b/Payment/PaymentContext.php index 08b4723..ae30277 100644 --- a/Payment/PaymentContext.php +++ b/Payment/PaymentContext.php @@ -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 diff --git a/Payment/TransactionFactory.php b/Payment/TransactionFactory.php index 1913f2e..c66118d 100644 --- a/Payment/TransactionFactory.php +++ b/Payment/TransactionFactory.php @@ -34,6 +34,7 @@ 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']) @@ -41,7 +42,8 @@ public function create(array $parameters): Transaction ->setAmount($resolvedParameters['amount']) ->setCurrencyCode($resolvedParameters['currency_code']) ->setDescription($resolvedParameters['description']) - ->setMetadatas($resolvedParameters['metadatas']) + ->setMetadata($resolvedParameters['metadata']) + ->setRaw($resolvedParameters['raw']) ; } @@ -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, diff --git a/Resources/config/doctrine/Transaction.orm.xml b/Resources/config/doctrine/Transaction.orm.xml index 521fe45..e9a477a 100644 --- a/Resources/config/doctrine/Transaction.orm.xml +++ b/Resources/config/doctrine/Transaction.orm.xml @@ -7,13 +7,15 @@ + - + + diff --git a/Resources/config/event_actions.yml b/Resources/config/event_actions.yml index 5f55a03..c113801 100644 --- a/Resources/config/event_actions.yml +++ b/Resources/config/event_actions.yml @@ -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}} diff --git a/Step/Event/Action/ManageTransactionStepEventAction.php b/Step/Event/Action/ManageTransactionStepEventAction.php index b01ff7d..cdef93e 100644 --- a/Step/Event/Action/ManageTransactionStepEventAction.php +++ b/Step/Event/Action/ManageTransactionStepEventAction.php @@ -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(); @@ -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' => [], @@ -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']) @@ -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) { diff --git a/Tests/Unit/Gateway/PaymentGatewayTestCase.php b/Tests/Unit/Gateway/PaymentGatewayTestCase.php index 5f38dc9..6c034a4 100644 --- a/Tests/Unit/Gateway/PaymentGatewayTestCase.php +++ b/Tests/Unit/Gateway/PaymentGatewayTestCase.php @@ -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 { @@ -50,7 +50,7 @@ public function setUp() 'amount' => 100, 'currency_code' => 'EUR', 'description' => 'Dummy description', - 'metadatas' => [], + 'metadata' => [], ]); } } diff --git a/Tests/Unit/Payment/PaymentContextTest.php b/Tests/Unit/Payment/PaymentContextTest.php index f7200a4..5722bca 100644 --- a/Tests/Unit/Payment/PaymentContextTest.php +++ b/Tests/Unit/Payment/PaymentContextTest.php @@ -89,7 +89,7 @@ public function testCreateTransaction() 'amount' => 100, 'currency_code' => 'EUR', 'description' => 'Dummy description', - 'metadatas' => [], + 'metadata' => [], ]; $this->dispatcher @@ -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(), @@ -126,7 +126,7 @@ public function testTransactionNotCreated() 'amount' => 100, 'currency_code' => 'EUR', 'description' => 'Dummy description', - 'metadatas' => [], + 'metadata' => [], ]; $this->dispatcher @@ -296,7 +296,7 @@ public function getHandleGatewayCallbackDataProvider() 'amount' => 100, 'currency_code' => 'EUR', 'description' => 'Dummy description', - 'metadatas' => [], + 'metadata' => [], ]; return [ @@ -319,7 +319,7 @@ public function getTransactionDataProvider() 'amount' => 100, 'currency_code' => 'EUR', 'description' => 'Dummy description', - 'metadatas' => [], + 'metadata' => [], ]; return [ diff --git a/Tests/Unit/Payment/TransactionFactoryTest.php b/Tests/Unit/Payment/TransactionFactoryTest.php index eb3936a..cf9ebd8 100644 --- a/Tests/Unit/Payment/TransactionFactoryTest.php +++ b/Tests/Unit/Payment/TransactionFactoryTest.php @@ -2,9 +2,9 @@ namespace IDCI\Bundle\PaymentBundle\Test\Unit\Payment; -use PHPUnit\Framework\TestCase; -use IDCI\Bundle\PaymentBundle\Payment\TransactionFactory; use IDCI\Bundle\PaymentBundle\Model\Transaction; +use IDCI\Bundle\PaymentBundle\Payment\TransactionFactory; +use PHPUnit\Framework\TestCase; class TransactionFactoryTest extends TestCase { @@ -21,7 +21,7 @@ public function testCreateWithWrongCurrencyCode() 'amount' => 100, 'currency_code' => 'wrong_currency_code', 'description' => 'Dummy description', - 'metadatas' => [], + 'metadata' => [], ]); } @@ -38,7 +38,7 @@ public function testCreateWithWrongAmount() 'amount' => 'wrong_amount', 'currency_code' => 'EUR', 'description' => 'Dummy description', - 'metadatas' => [], + 'metadata' => [], ]); } @@ -52,7 +52,7 @@ public function testCreate() 'amount' => 10, 'currency_code' => 'EUR', 'description' => 'Dummy description', - 'metadatas' => [], + 'metadata' => [], ]); $this->assertInstanceOf(Transaction::class, $transaction); @@ -63,7 +63,7 @@ public function testCreate() $this->assertEquals(10, $transaction->getAmount()); $this->assertEquals('EUR', $transaction->getCurrencyCode()); $this->assertEquals('Dummy description', $transaction->getDescription()); - $this->assertInternalType('array', $transaction->getMetadatas()); - $this->assertEquals(0, count($transaction->getMetadatas())); + $this->assertInternalType('array', $transaction->getMetadata()); + $this->assertEquals(0, count($transaction->getMetadata())); } }