From 4dc04a6818753096efc2e85050b2c0ac684586bc Mon Sep 17 00:00:00 2001 From: Fabiano Mallmann <25328512+fabiano-mallmann@users.noreply.github.com> Date: Tue, 11 Jun 2024 15:08:19 -0300 Subject: [PATCH 1/3] fix: manual order and emails --- src/Controller/Gateways/AbstractGateway.php | 2 +- src/Controller/Orders.php | 2 +- src/Helper/Utils.php | 7 ++- src/Model/Checkout.php | 4 ++ src/Model/Order.php | 9 ++-- .../AbstractPlatformOrderDecorator.php | 10 ++++ .../src/Kernel/Services/OrderService.php | 2 +- .../src/Payment/Aggregates/Order.php | 52 ++++++++++++------- 8 files changed, 60 insertions(+), 28 deletions(-) diff --git a/src/Controller/Gateways/AbstractGateway.php b/src/Controller/Gateways/AbstractGateway.php index fddcc2a4..f86f50eb 100644 --- a/src/Controller/Gateways/AbstractGateway.php +++ b/src/Controller/Gateways/AbstractGateway.php @@ -399,7 +399,7 @@ protected function saveAdminOptionsInCoreConfig($values) public function pagarme_email_payment_info($order, $sent_to_admin) { if ($sent_to_admin - || $this->id !== $order->get_meta('payment_method') + || $this->id !== $order->get_payment_method() || !in_array($order->get_status(), $this->sendEmailStatus)) { return; } diff --git a/src/Controller/Orders.php b/src/Controller/Orders.php index aef252dc..d0e7f246 100644 --- a/src/Controller/Orders.php +++ b/src/Controller/Orders.php @@ -59,7 +59,7 @@ public function create_order(WC_Order $wc_order, $payment_method, $form_fields) $paymentMethodDecorator->setPaymentMethod($orderDecorator); $orderDecorator->setPaymentMethod($paymentMethodDecorator->getPaymentMethod()); - + $orderDecorator->setAttempts($wc_order->get_meta('_pagarme_attempts')); $orderService = new OrderService(); $response = $orderService->createOrderAtPagarme($orderDecorator); diff --git a/src/Helper/Utils.php b/src/Helper/Utils.php index a01687cb..d868a34b 100644 --- a/src/Helper/Utils.php +++ b/src/Helper/Utils.php @@ -128,6 +128,7 @@ public static function is_request_ajax() return ( strtolower(self::server('HTTP_X_REQUESTED_WITH')) === 'xmlhttprequest' || (0 === strpos(self::server('QUERY_STRING'), 'wc-ajax')) || strtolower(self::server('HTTP_X_REQUEST_TYPE')) === 'ajax'); + // || ; } /** @@ -138,9 +139,11 @@ public static function is_request_ajax() */ public static function isCheckoutRequest() { - return strpos(strtolower(self::server('REQUEST_URI')), 'checkout') !== false; + if(function_exists('is_checkout')) { + return is_checkout(); + } + return false; } - /** * Get value by array index * diff --git a/src/Model/Checkout.php b/src/Model/Checkout.php index 0b0b718d..13cccf1c 100644 --- a/src/Model/Checkout.php +++ b/src/Model/Checkout.php @@ -123,6 +123,8 @@ public function process(WC_Order $wc_order = null, string $type = CheckoutTypes: if ($type === CheckoutTypes::TRANSPARENT_VALUE) { $fields = $this->convertCheckoutObject($_POST[PaymentRequestInterface::PAGARME_PAYMENT_REQUEST_KEY]); $fields['recurrence_cycle'] = Subscription::getRecurrenceCycle(); + $attempts = intval($wc_order->get_meta('_pagarme_attempts')) + 1; + $wc_order->update_meta_data("_pagarme_attempts", $attempts); $response = $this->orders->create_order( $wc_order, $fields['payment_method'], @@ -137,6 +139,7 @@ public function process(WC_Order $wc_order = null, string $type = CheckoutTypes: ); $order->getWcOrder()->set_total($this->getTotalValue($wc_order, $totalWithInstallments)); $order->update_meta('payment_method', $fields['payment_method']); + $order->update_meta("attempts", $attempts); $this->addAuthenticationOnMetaData($order, $fields); WC()->cart->empty_cart(); if ($response) { @@ -151,6 +154,7 @@ public function process(WC_Order $wc_order = null, string $type = CheckoutTypes: } $order->update_meta('pagarme_status', 'failed'); $order->update_by_pagarme_status('failed'); + $order->getWcOrder()->save(); return false; } } diff --git a/src/Model/Order.php b/src/Model/Order.php index eb1f44e3..74d148ce 100644 --- a/src/Model/Order.php +++ b/src/Model/Order.php @@ -49,10 +49,11 @@ class Order extends Meta // == END WC ORDER == public $with_prefix = array( - 'payment_method' => 1, - 'response_data' => 1, - 'pagarme_status' => 1, - 'pagarme_id' => 1, + 'payment_method' => 1, + 'response_data' => 1, + 'pagarme_status' => 1, + 'pagarme_id' => 1, + 'attempts' => 1 ); /** phpcs:disable */ diff --git a/vendor/pagarme/ecommerce-module-core/src/Kernel/Abstractions/AbstractPlatformOrderDecorator.php b/vendor/pagarme/ecommerce-module-core/src/Kernel/Abstractions/AbstractPlatformOrderDecorator.php index ec2c32fc..44d206d9 100644 --- a/vendor/pagarme/ecommerce-module-core/src/Kernel/Abstractions/AbstractPlatformOrderDecorator.php +++ b/vendor/pagarme/ecommerce-module-core/src/Kernel/Abstractions/AbstractPlatformOrderDecorator.php @@ -15,12 +15,22 @@ abstract class AbstractPlatformOrderDecorator implements PlatformOrderInterface protected $platformOrder; private $logService; private $paymentMethod; + private $attempts = 1; public function __construct() { $this->logService = new OrderLogService(); } + + public function getAttempts() + { + return $this->attempts; + } + public function setAttempts($attempts) + { + $this->attempts = $attempts; + } public function addHistoryComment($message, $notifyCustomer = false) { $message = 'PGM - ' . $message; diff --git a/vendor/pagarme/ecommerce-module-core/src/Kernel/Services/OrderService.php b/vendor/pagarme/ecommerce-module-core/src/Kernel/Services/OrderService.php index ee1431ef..58a85887 100644 --- a/vendor/pagarme/ecommerce-module-core/src/Kernel/Services/OrderService.php +++ b/vendor/pagarme/ecommerce-module-core/src/Kernel/Services/OrderService.php @@ -257,7 +257,7 @@ public function createOrderAtPagarme(PlatformOrderInterface $platformOrder) ); $i18n = new LocalizationService(); - + $paymentOrder->setAttempts($platformOrder->getAttempts()); //Send through the APIService to pagarme $apiService = new APIService(); $response = $apiService->createOrder($paymentOrder); diff --git a/vendor/pagarme/ecommerce-module-core/src/Payment/Aggregates/Order.php b/vendor/pagarme/ecommerce-module-core/src/Payment/Aggregates/Order.php index c78e2856..c13a68c8 100644 --- a/vendor/pagarme/ecommerce-module-core/src/Payment/Aggregates/Order.php +++ b/vendor/pagarme/ecommerce-module-core/src/Payment/Aggregates/Order.php @@ -33,7 +33,9 @@ final class Order extends AbstractEntity implements ConvertibleToSDKRequestsInte private $payments; /** @var boolean */ private $closed; - + /** @var int */ + private $attempts = 1; + /** @var array */ private $splitData; /** @var boolean */ @@ -46,6 +48,17 @@ public function __construct() $this->closed = true; } + + public function getAttempts() + { + return $this->attempts; + } + + public function setAttempts($attempts) + { + $this->attempts = $attempts; + } + /** * @return string */ @@ -138,7 +151,7 @@ public function addPayment(AbstractPayment $payment) */ public function generateIdempotencyKey() { - return sha1($this->getCustomer()->getDocument() . $this->getCode()); + return sha1($this->getCustomer()->getDocument() . $this->getCode() . '-attempt-' . $this->getAttempts()); } /** @@ -154,8 +167,7 @@ public function isPaymentSumCorrect() } $sum = 0; - foreach ($this->payments as $payment) - { + foreach ($this->payments as $payment) { $sum += $payment->getAmount(); } @@ -207,7 +219,7 @@ private function validatePaymentInvariants(AbstractPayment $payment) private function discoverPaymentMethod(AbstractPayment $payment) { $paymentClass = get_class($payment); - $paymentClass = explode ('\\', $paymentClass ?? ''); + $paymentClass = explode('\\', $paymentClass ?? ''); $paymentClass = end($paymentClass); return $paymentClass; } @@ -217,7 +229,7 @@ private function validateSavedCreditCardPayment(SavedCreditCardPayment $payment) if ($this->customer === null) { throw new \Exception( 'To use a saved credit card payment in an order ' . - 'you must add a customer to it.', + 'you must add a customer to it.', 400 ); } @@ -335,7 +347,7 @@ public function setSplitData($splitData) * @return mixed data which can be serialized by json_encode, * which is a value of any type other than a resource. * @since 5.4.0 - */ + */ #[\ReturnTypeWillChange] public function jsonSerialize() @@ -375,7 +387,7 @@ public function convertToSDKRequest() $orderRequest->payments[] = $payment->convertToSDKRequest(); } - if (!empty($this->getSplitData())){ + if (!empty($this->getSplitData())) { $orderRequest = $this->fixRoundedValuesInCharges($orderRequest); } @@ -392,16 +404,17 @@ public function convertToSDKRequest() return $orderRequest; } - private function fixRoundedValuesInCharges(&$orderRequest){ + private function fixRoundedValuesInCharges(&$orderRequest) + { - if(count($orderRequest->payments) < 2){ + if (count($orderRequest->payments) < 2) { return $orderRequest; } $firstChargeAmount = $orderRequest->payments[0]->amount; $firstChargePercentageOfTotal = $firstChargeAmount / $this->getAmount(); - if ($firstChargePercentageOfTotal !== 0.5){ + if ($firstChargePercentageOfTotal !== 0.5) { return $orderRequest; } @@ -409,17 +422,17 @@ private function fixRoundedValuesInCharges(&$orderRequest){ $wrongValuesPerRecipient = $this->getRecipientWrongValuesMap($orderRequest, $orderSplitData); - if (!$wrongValuesPerRecipient){ + if (!$wrongValuesPerRecipient) { return $orderRequest; } $orderRequest = $this->fixRoundedValues($wrongValuesPerRecipient, $orderRequest); return $orderRequest; - } - private function getRecipientWrongValuesMap($orderRequest, $splitData){ + private function getRecipientWrongValuesMap($orderRequest, $splitData) + { $map = []; $marketplaceId = $splitData->getMainRecipientOptionConfig(); @@ -445,7 +458,7 @@ private function getRecipientWrongValuesMap($orderRequest, $splitData){ } foreach ($map as $recipientId => $wrongValue) { - if ($wrongValue !== 0){ + if ($wrongValue !== 0) { return $map; } } @@ -453,7 +466,8 @@ private function getRecipientWrongValuesMap($orderRequest, $splitData){ return false; } - private function fixRoundedValues($wrongValuesMap, &$orderRequest){ + private function fixRoundedValues($wrongValuesMap, &$orderRequest) + { foreach ($wrongValuesMap as $recipientId => $wrongValue) { $payments = $orderRequest->payments; @@ -466,12 +480,12 @@ private function fixRoundedValues($wrongValuesMap, &$orderRequest){ foreach ($paymentRequest->split as $key => &$splitRequest) { $splitedAmount += $splitRequest->amount; - if($splitRequest->recipientId === $recipientId){ + if ($splitRequest->recipientId === $recipientId) { $recipientSplitData = $splitRequest; } } - if ($splitedAmount === $paymentRequestAmount){ + if ($splitedAmount === $paymentRequestAmount) { continue; } @@ -481,7 +495,7 @@ private function fixRoundedValues($wrongValuesMap, &$orderRequest){ $mustRemoveFromOtherCharges = $wrongValue + $amountRemovableFromCharge; - if (!$mustRemoveFromOtherCharges){ + if (!$mustRemoveFromOtherCharges) { break; } } From 4cf581a83b00cf516a162cf1aca667bde3c928b6 Mon Sep 17 00:00:00 2001 From: Fabiano Mallmann <25328512+fabiano-mallmann@users.noreply.github.com> Date: Tue, 11 Jun 2024 15:22:56 -0300 Subject: [PATCH 2/3] remove unecessary code --- src/Helper/Utils.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Helper/Utils.php b/src/Helper/Utils.php index d868a34b..d87d7c03 100644 --- a/src/Helper/Utils.php +++ b/src/Helper/Utils.php @@ -128,7 +128,6 @@ public static function is_request_ajax() return ( strtolower(self::server('HTTP_X_REQUESTED_WITH')) === 'xmlhttprequest' || (0 === strpos(self::server('QUERY_STRING'), 'wc-ajax')) || strtolower(self::server('HTTP_X_REQUEST_TYPE')) === 'ajax'); - // || ; } /** From 65d6c03afed4f1b01787c78509a245ee84eba5cf Mon Sep 17 00:00:00 2001 From: Fabiano Mallmann <25328512+fabiano-mallmann@users.noreply.github.com> Date: Tue, 11 Jun 2024 16:42:27 -0300 Subject: [PATCH 3/3] fix: tests and wrong behavior --- src/Model/Checkout.php | 2 +- tests/Model/CheckoutTest.php | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/Model/Checkout.php b/src/Model/Checkout.php index 13cccf1c..8df4a3e4 100644 --- a/src/Model/Checkout.php +++ b/src/Model/Checkout.php @@ -123,7 +123,7 @@ public function process(WC_Order $wc_order = null, string $type = CheckoutTypes: if ($type === CheckoutTypes::TRANSPARENT_VALUE) { $fields = $this->convertCheckoutObject($_POST[PaymentRequestInterface::PAGARME_PAYMENT_REQUEST_KEY]); $fields['recurrence_cycle'] = Subscription::getRecurrenceCycle(); - $attempts = intval($wc_order->get_meta('_pagarme_attempts')) + 1; + $attempts = intval($wc_order->get_meta('_pagarme_attempts') ?? 0) + 1; $wc_order->update_meta_data("_pagarme_attempts", $attempts); $response = $this->orders->create_order( $wc_order, diff --git a/tests/Model/CheckoutTest.php b/tests/Model/CheckoutTest.php index 334b5a1c..deecd95f 100644 --- a/tests/Model/CheckoutTest.php +++ b/tests/Model/CheckoutTest.php @@ -103,6 +103,10 @@ public function testProcessWithTdsAuthenticatedCreditCardPaymentMethodShouldSetA ->andReturn(1); $wcOrderMock->shouldReceive('set_total') ->andReturnSelf(); + $wcOrderMock->shouldReceive('get_meta') + ->andReturn(""); + $wcOrderMock->shouldReceive('update_meta_data') + ->andReturnSelf(); $ordersMock->shouldReceive('create_order') ->withArgs(function ($wcOrder, $paymentMethod, $fields) use ($wcOrderMock) { @@ -144,7 +148,7 @@ public function testProcessWithTdsAuthenticatedCreditCardPaymentMethodShouldSetA ->once(); $checkout = new Checkout($gatewayMock, $configMock, $ordersMock, $wooOrderRepositoryMock); - + $this->assertTrue($checkout->process($wcOrderMock)); } }