From 88ff7460b55b0db7c43e84784d3119ad54095982 Mon Sep 17 00:00:00 2001 From: Ram Prakash Singh <45222925+ramth05@users.noreply.github.com> Date: Tue, 1 Sep 2020 10:49:25 +0530 Subject: [PATCH] =?UTF-8?q?PON-556:=20Webhook=20orders=20shipping=20and=20?= =?UTF-8?q?billing=20address=20marked=20as=20default=E2=80=A6=20(#195)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * PON-556:Webhook orders shipping and billing address marked as default user address. * fixed issue with rigistered user trying guest checkout and amount rounding issue * amount rounding fix * fixed billing addres not saved in quote --- Controller/Payment/Order.php | 25 +++++++++++++-- Controller/Payment/Webhook.php | 32 ++++++------------- composer.json | 2 +- etc/module.xml | 2 +- .../method-renderer/razorpay-method.js | 3 +- 5 files changed, 36 insertions(+), 28 deletions(-) diff --git a/Controller/Payment/Order.php b/Controller/Payment/Order.php index e45d75d0..94bd1a5d 100644 --- a/Controller/Payment/Order.php +++ b/Controller/Payment/Order.php @@ -167,8 +167,29 @@ public function execute() } else { + //setting updated billing address + if(empty($_POST['billing_address']) === false) + { + $billing_address = json_decode($_POST['billing_address'], 1); + + $this->getQuote()->getBillingAddress() + ->setFirstname($billing_address['firstname']) + ->setLastname($billing_address['lastname']) + ->setCountryId($billing_address['countryId']) + ->setPostcode($billing_address['postcode']) + ->setCity($billing_address['city']) + ->setTelephone($billing_address['telephone']) + ->setCompany($billing_address['company']) + ->setStreet($billing_address['street']) + ->setRegionId($billing_address['regionId']) + ->setRegionCode($billing_address['regionCode']) + ->setRegion($billing_address['region']) + ->setSaveInAddressBook($billing_address['saveInAddressBook']); + + $this->getQuote()->save(); + } - $amount = (int) (round($this->getQuote()->getGrandTotal(), 2) * 100); + $amount = (int) (number_format($this->getQuote()->getGrandTotal() * 100, 0, ".", "")); $payment_action = $this->config->getPaymentAction(); @@ -215,7 +236,7 @@ public function execute() 'order_id' => $receipt_id, 'amount' => $order->amount, 'quote_currency' => $this->getQuote()->getQuoteCurrencyCode(), - 'quote_amount' => round($this->getQuote()->getGrandTotal(), 2), + 'quote_amount' => number_format($this->getQuote()->getGrandTotal(), 2, ".", ""), 'maze_version' => $maze_version, 'module_version' => $module_version, 'is_hosted' => $merchantPreferences['is_hosted'], diff --git a/Controller/Payment/Webhook.php b/Controller/Payment/Webhook.php index 7da681ac..9ed9709f 100644 --- a/Controller/Payment/Webhook.php +++ b/Controller/Payment/Webhook.php @@ -209,7 +209,7 @@ protected function orderPaid(array $post) } //validate amount before placing order - $quoteAmount = (int) (round($quote->getGrandTotal(), 2) * 100); + $quoteAmount = (int) (number_format($quote->getGrandTotal() * 100, 0, ".", "")); if ($quoteAmount !== $post['payload']['payment']['entity']['amount']) { @@ -257,14 +257,11 @@ protected function orderPaid(array $post) protected function getQuoteObject($post, $quoteId) { - $email = $post['payload']['payment']['entity']['email']; - $quote = $this->quoteRepository->get($quoteId); - $firstName = $quote->getBillingAddress()->getFirstname() ?? 'null'; - $lastName = $quote->getBillingAddress()->getLastname() ?? 'null'; - + $lastName = $quote->getBillingAddress()->getLastname() ?? 'null'; + $email = $quote->getBillingAddress()->getEmail() ?? $post['payload']['payment']['entity']['email']; $quote->getPayment()->setMethod(PaymentMethod::METHOD_CODE); @@ -277,35 +274,24 @@ protected function getQuoteObject($post, $quoteId) $customer->setWebsiteId($websiteId); //get customer from quote , otherwise from payment email - if (empty($quote->getBillingAddress()->getEmail()) === false) - { - $customer = $customer->loadByEmail($quote->getBillingAddress()->getEmail()); - } - else - { - $customer = $customer->loadByEmail($email); - } + $customer = $customer->loadByEmail($email); //if quote billing address doesn't contains address, set it as customer default billing address - if ((empty($quote->getBillingAddress()->getFirstname()) === true) and (empty($customer->getEntityId()) === false)) + if ((empty($quote->getBillingAddress()->getFirstname()) === true) and + (empty($customer->getEntityId()) === false)) { $quote->getBillingAddress()->setCustomerAddressId($customer->getDefaultBillingAddress()['id']); } - //If need to insert new customer - if (empty($customer->getEntityId()) === true) + //If need to insert new customer as guest + if ((empty($customer->getEntityId()) === true) or + (empty($quote->getBillingAddress()->getCustomerId()) === true)) { $quote->setCustomerFirstname($firstName); $quote->setCustomerLastname($lastName); $quote->setCustomerEmail($email); $quote->setCustomerIsGuest(true); } - else - { - $customer = $this->customerRepository->getById($customer->getEntityId()); - - $quote->assignCustomer($customer); - } $quote->setStore($store); diff --git a/composer.json b/composer.json index a52cec2d..a52f7be7 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "razorpay/magento", "description": "Razorpay Magento 2.0 plugin for accepting payments.", - "version": "3.1.0", + "version": "3.1.1", "require": { "php": "~5.5.0|~5.6.0|^7.0", "razorpay/razorpay": "2.*" diff --git a/etc/module.xml b/etc/module.xml index ad48fe06..83512aa5 100644 --- a/etc/module.xml +++ b/etc/module.xml @@ -1,6 +1,6 @@ - + diff --git a/view/frontend/web/js/view/payment/method-renderer/razorpay-method.js b/view/frontend/web/js/view/payment/method-renderer/razorpay-method.js index 463083f8..00fa2d72 100644 --- a/view/frontend/web/js/view/payment/method-renderer/razorpay-method.js +++ b/view/frontend/web/js/view/payment/method-renderer/razorpay-method.js @@ -129,7 +129,8 @@ define( type: 'POST', url: url.build('razorpay/payment/order?' + Math.random().toString(36).substring(10)), data: { - email: this.user.email + email: this.user.email, + billing_address: JSON.stringify(quote.billingAddress()) }, /**