Skip to content

Commit 0fa88e3

Browse files
cedrancontardiThiago ContardiGabrielAntalcarolineesteves
authored
Bugfix - Erro no valores de desconto e frete (#19)
* feat: added new credit card form * feat: added setup for old settings and minor bugfixes * fix: error with 1i18n on setting's page * fix: allow all countris by default * i18n: add new terms for language file * feat: added max installments and minum installment value * feat: added reseller token to transactions * feat: added reseller token to transactions * feat: send tracking code to Vindi * fix: mask also reseller token and token account * fix: when the card number comes with 4 digits, don't add 20 before it * fix: when the card number comes with 4 digits, don't add 20 before it * doc: added pull request template * fix: observer to bankslippix * fix: cancel unapproved orders without config * feat: adjust with phpstan * fix: error monitoring transactions * fix: worng price additional * fix: update terms to ask for a CPF on checkout * feat: don't auto select TaxVat when is not a CPF number * feat: don't auto select TaxVat when is not a CPF number * Payment link functionality created * Vindi payment method verification to send the payment link has been added * The payment link email template changed and made an email configuration to set the template * Template Id changed * Email path and list fixed * feat/VINDI-143: estilo da pagina de link de pagamento com boleto, pix, bolepix e cartao * feat/VINDI-143: estilizacao pagina link de pagamento * Use strict added to js file * ko library added * float cast removed * Applying the requested changes on payment link files * adding the vindi_customer_taxvat to additionalInformation * Fixing the bugs about payment link * VP module version has been updated * payment link email template has been changed * Deleting the payment link when it is expired * Fixing the payment link summary details * Adding a discount verification * Making the discount value positive * fix: change fingerprint loader * VINDI-143: pagina de sucesso * VINDI-143: mensagem de desconto no link de pagamento * Remove linha comentada * The payment link delete has been removed from the payment link page * fix: add fingerprint method in define * fix: customer without taxvat * fix: preventing conflict with the vindi recurrence module * feat: adding security validations and better payment link management * fix: phpstan fix * feat: creating mass sending of payment link * fix: remove await vindi fingerprint * feat: add translate * feat: automatically cancels orders whose payment link has expired more than 30 days ago * feat: custom email template * fix: remove await vindi fingerprint * fix: remove await vindi fingerprint * refactor: adding validation on payment methods and payment link success page * fix: change form with get status * feat: implementing single view system on success page * feat: translate * fix: remove await vindi fingerprint * fix: remove await vindi fingerprint * feat: expire_at field * update * update * update * update * fix: processing payment link when canceling * fix: Adjust payment data displays if the order has already been invoiced * feat: added refund functionality * fix: allow to refund * fix: remove default consumer-key, consumer secret * fix: remove default consumer-key, consumer secret * fix: remove default consumer-key, consumer secret * feat: create authentication button * refactor: add logging in refund flow * fix: address saving repeated * fix: address saving repeated * fix: logger in helper * fix: logger in helper * fix: logger in helper * refactor: add log in RefundRequest * refactor: add log in RefundRequest * refactor: add log in RefundRequest * fix: correcting the way the access_token is taken * fix: generate new access_token * refactor: refactoring duplicate code and creating tests * refactor: refactoring duplicate code and creating tests * refactor: refactoring duplicate code and creating tests * fix: call fingerprint script in all pages * fix: regenerate acces_token * v1.4.1 * fix: refresh token error * fix: it should not be possible to generate payment link via the frontend * feat: added text to return URL * fix: round the discount tag * Fix Vindi transaction payload: include price_discount and shipping_price to reflect Magento discounts and shipping * fix: Enhance getTransactionShipping: add fallback to getShippingInclTax, enforce two-decimal string format, and default to 'SEM_FRETE' when no description * fix * fix * fix * fix * fix --------- Co-authored-by: Thiago Contardi <thiagocontardi@hotmail.com> Co-authored-by: Thiago Contardi <thiago@bizcommerce.com.br> Co-authored-by: Contardi <contardi@users.noreply.github.com> Co-authored-by: Gabriel Antal <gabrielantalsilva@gmail.com> Co-authored-by: Caroline Esteves <carol@bizcommerce.com.br> Co-authored-by: Antal <gabriel.antal@bizcommerce.com.br> Co-authored-by: Antal <44688111+GabrielAntal@users.noreply.github.com> Co-authored-by: carolineesteves <131886821+carolineesteves@users.noreply.github.com> Co-authored-by: Iago Cedran <iago@bizcommerce.com.br>
1 parent f949ba2 commit 0fa88e3

File tree

2 files changed

+323
-108
lines changed

2 files changed

+323
-108
lines changed

Gateway/Request/PaymentsRequest.php

Lines changed: 110 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*
1818
*
1919
*/
20+
declare(strict_types=1);
2021

2122
namespace Vindi\VP\Gateway\Request;
2223

@@ -30,6 +31,7 @@
3031
use Magento\Catalog\Api\ProductRepositoryInterface;
3132
use Magento\Sales\Api\Data\OrderItemInterface;
3233
use Magento\Sales\Model\Order;
34+
use Magento\Framework\Exception\LocalizedException;
3335

3436
class PaymentsRequest
3537
{
@@ -73,16 +75,24 @@ class PaymentsRequest
7375
*/
7476
protected $categoryRepository;
7577

76-
/**
77-
* @var string
78-
*/
79-
protected $currencyCode;
80-
8178
/**
8279
* @var Api
8380
*/
8481
protected $api;
8582

83+
/**
84+
* PaymentsRequest constructor.
85+
*
86+
* @param ManagerInterface $eventManager
87+
* @param Data $helper
88+
* @param DateTime $date
89+
* @param ConfigInterface $config
90+
* @param CustomerSession $customerSession
91+
* @param DateTime $dateTime
92+
* @param ProductRepositoryInterface $productRepository
93+
* @param CategoryRepositoryInterface $categoryRepository
94+
* @param Api $api
95+
*/
8696
public function __construct(
8797
ManagerInterface $eventManager,
8898
Data $helper,
@@ -94,34 +104,46 @@ public function __construct(
94104
CategoryRepositoryInterface $categoryRepository,
95105
Api $api
96106
) {
97-
$this->eventManager = $eventManager;
98-
$this->helper = $helper;
99-
$this->date = $date;
100-
$this->config = $config;
101-
$this->customerSession = $customerSession;
102-
$this->dateTime = $dateTime;
103-
$this->productRepository = $productRepository;
107+
$this->eventManager = $eventManager;
108+
$this->helper = $helper;
109+
$this->date = $date;
110+
$this->config = $config;
111+
$this->customerSession = $customerSession;
112+
$this->dateTime = $dateTime;
113+
$this->productRepository = $productRepository;
104114
$this->categoryRepository = $categoryRepository;
105-
$this->api = $api;
115+
$this->api = $api;
106116
}
107117

118+
/**
119+
* Get the payment method data.
120+
*
121+
* @param Order $order
122+
* @return array
123+
*/
108124
protected function getPaymentMethod(Order $order): array
109125
{
110126
return [
111127
'payment_method_id' => $this->helper->getMethodId('PIX'),
112-
'split' => 1
128+
'split' => 1
113129
];
114130
}
115131

132+
/**
133+
* Get the transaction data.
134+
*
135+
* @param Order $order
136+
* @param float $amount
137+
* @return array
138+
*/
116139
protected function getTransaction(Order $order, float $amount): array
117140
{
118141
$transaction = [
119-
'token_account' => $this->helper->getToken($order->getStoreId()),
120-
'finger_print' => $order->getPayment()->getAdditionalInformation('finger_print'),
121-
'customer' => $this->getCustomerData($order),
122-
'transaction' => $this->getTransactionInfo($order, $amount),
123-
'transaction_shipping'=> $this->getTransactionShipping($order),
124-
'transaction_product' => $this->getItemsData($order)
142+
'token_account' => $this->helper->getToken($order->getStoreId()),
143+
'finger_print' => $order->getPayment()->getAdditionalInformation('finger_print'),
144+
'customer' => $this->getCustomerData($order),
145+
'transaction' => $this->getTransactionInfo($order, $amount),
146+
'transaction_product' => $this->getItemsData($order)
125147
];
126148

127149
$resellerToken = $this->helper->getResellerToken($order->getStoreId());
@@ -133,38 +155,29 @@ protected function getTransaction(Order $order, float $amount): array
133155
}
134156

135157
/**
136-
* Get the transaction information.
137-
*
138158
* @param Order $order
139159
* @param float $orderAmount
140160
* @return array
141161
*/
142162
protected function getTransactionInfo(Order $order, float $orderAmount): array
143163
{
164+
$shippingDescription = $order->getShippingDescription();
165+
$shippingType = $shippingDescription ?: 'SEM_FRETE';
166+
167+
$shippingAmount = (float) $order->getShippingAmount();
168+
if ($shippingAmount <= 0 && method_exists($order, 'getShippingInclTax')) {
169+
$shippingAmount = (float) $order->getShippingInclTax();
170+
}
171+
144172
return [
145173
'customer_ip' => $order->getRemoteIp(),
146174
'order_number' => $order->getIncrementId(),
147-
'price_discount' => (string) $this->getDiscountAmount($order, $orderAmount),
148-
'price_additional' => (string) $this->getPriceAdditional($order, $orderAmount),
175+
'price_discount' => number_format($this->getDiscountAmount($order, $orderAmount), 2, '.', ''),
176+
'price_additional' => number_format($this->getPriceAdditional($order, $orderAmount), 2, '.', ''),
149177
'url_notification' => $this->helper->getPaymentsNotificationUrl($order),
150-
'free' => 'MAGENTO_API_' . $this->helper->getModuleVersion()
151-
];
152-
}
153-
154-
/**
155-
* Get the shipping information for the transaction.
156-
*
157-
* @param Order $order
158-
* @return array
159-
*/
160-
protected function getTransactionShipping(Order $order): array
161-
{
162-
$shippingDescription = $order->getShippingDescription();
163-
$shippingType = $shippingDescription ? $shippingDescription : 'SEM_FRETE';
164-
165-
return [
166-
'type_shipping' => $shippingType,
167-
'shipping_price'=> (string) $order->getShippingAmount()
178+
'free' => 'MAGENTO_API_' . $this->helper->getModuleVersion(),
179+
'shipping_type' => $shippingType,
180+
'shipping_price' => number_format($shippingAmount, 2, '.', '')
168181
];
169182
}
170183

@@ -177,10 +190,10 @@ protected function getTransactionShipping(Order $order): array
177190
*/
178191
public function getDiscountAmount(Order $order, $orderAmount): float
179192
{
180-
$discountAmount = (float) $order->getDiscountAmount();
181-
$transactionAmount = $order->getBaseSubtotal() + $order->getShippingAmount() + $discountAmount;
182-
if ($transactionAmount > $orderAmount) {
183-
$discountAmount = $transactionAmount - $orderAmount;
193+
$discountAmount = (float)$order->getDiscountAmount();
194+
$transactionTotal = $order->getBaseSubtotal() + $order->getShippingAmount() + $discountAmount;
195+
if ($transactionTotal > $orderAmount) {
196+
$discountAmount = $transactionTotal - $orderAmount;
184197
}
185198
return round(abs($discountAmount), 2);
186199
}
@@ -194,12 +207,11 @@ public function getDiscountAmount(Order $order, $orderAmount): float
194207
*/
195208
protected function getPriceAdditional(Order $order, float $orderAmount): float
196209
{
197-
$priceAdditional = 0;
198-
$transactionAmount = (float) $order->getBaseSubtotal() + (float) $order->getShippingAmount() + (float) $order->getDiscountAmount();
199-
if ($transactionAmount < $orderAmount) {
200-
$priceAdditional = $orderAmount - $transactionAmount;
210+
$transactionTotal = $order->getBaseSubtotal() + $order->getShippingAmount() + $order->getDiscountAmount();
211+
if ($transactionTotal < $orderAmount) {
212+
return round($orderAmount - $transactionTotal, 2);
201213
}
202-
return round((float) $priceAdditional, 2);
214+
return 0.00;
203215
}
204216

205217
/**
@@ -210,37 +222,29 @@ protected function getPriceAdditional(Order $order, float $orderAmount): float
210222
*/
211223
public function getCustomerData(Order $order): array
212224
{
213-
$address = $order->getBillingAddress();
214-
$customerTaxVat = $address->getVatId() ?: $order->getCustomerTaxvat();
225+
$address = $order->getBillingAddress();
226+
$customerTaxVat = $address->getVatId() ?: $order->getCustomerTaxvat();
215227
$vindiCustomerTaxVat = $order->getPayment()->getAdditionalInformation('vindi_customer_taxvat');
216228
if ($vindiCustomerTaxVat) {
217229
$customerTaxVat = $vindiCustomerTaxVat;
218230
}
219231

220232
$firstName = $address->getFirstname() ?: $order->getCustomerFirstname();
221-
$lastName = $address->getLastname() ?: $order->getCustomerLastname();
222-
$fullName = $order->getCustomerName() ?: $firstName . ' ' . $lastName;
233+
$lastName = $address->getLastname() ?: $order->getCustomerLastname();
234+
$fullName = $order->getCustomerName() ?: trim("$firstName $lastName");
223235

224236
$customerData = [
225-
'name' => $fullName,
226-
'cpf' => preg_replace('/\D/', '', (string) $customerTaxVat),
227-
'email' => $order->getCustomerEmail(),
228-
'contacts' => [
229-
[
230-
'type_contact' => 'M',
231-
'number_contact'=> $this->helper->formatPhoneNumber($address->getTelephone())
232-
]
233-
],
237+
'name' => $fullName,
238+
'cpf' => preg_replace('/\D/', '', (string)$customerTaxVat),
239+
'email' => $order->getCustomerEmail(),
240+
'contacts' => [[
241+
'type_contact' => 'M',
242+
'number_contact' => $this->helper->formatPhoneNumber($address->getTelephone())
243+
]],
234244
'addresses' => $this->getAddresses($order)
235245
];
236246

237-
$customerData = $this->helper->getCompanyData($order, $customerData);
238-
239-
if ($order->getCustomerDob()) {
240-
$customerData['birth_date'] = $this->helper->formatDate($order->getCustomerDob());
241-
}
242-
243-
return $customerData;
247+
return $this->helper->getCompanyData($order, $customerData);
244248
}
245249

246250
/**
@@ -251,31 +255,30 @@ public function getCustomerData(Order $order): array
251255
*/
252256
protected function getAddresses($order): array
253257
{
258+
$addresses = [];
254259
$billingAddress = $order->getBillingAddress();
255-
$addresses = [
256-
[
257-
'type_address' => 'B',
258-
'postal_code' => $billingAddress->getPostcode(),
259-
'street' => $billingAddress->getStreetLine($this->getStreetField('street')),
260-
'number' => $billingAddress->getStreetLine($this->getStreetField('number')),
261-
'completion' => $billingAddress->getStreetLine($this->getStreetField('complement')),
262-
'neighborhood' => $billingAddress->getStreetLine($this->getStreetField('district')),
263-
'city' => $billingAddress->getCity(),
264-
'state' => $billingAddress->getRegionCode()
265-
]
260+
261+
$addresses[] = [
262+
'type_address' => 'B',
263+
'postal_code' => $billingAddress->getPostcode(),
264+
'street' => $billingAddress->getStreetLine($this->getStreetField('street')),
265+
'number' => $billingAddress->getStreetLine($this->getStreetField('number')),
266+
'completion' => $billingAddress->getStreetLine($this->getStreetField('complement')),
267+
'neighborhood' => $billingAddress->getStreetLine($this->getStreetField('district')),
268+
'city' => $billingAddress->getCity(),
269+
'state' => $billingAddress->getRegionCode()
266270
];
267271

268-
if ($order->getShippingAddress()) {
269-
$shippingAddress = $order->getShippingAddress();
272+
if ($shipping = $order->getShippingAddress()) {
270273
$addresses[] = [
271274
'type_address' => 'D',
272-
'postal_code' => $shippingAddress->getPostcode(),
273-
'street' => $shippingAddress->getStreetLine($this->getStreetField('street')),
274-
'number' => $shippingAddress->getStreetLine($this->getStreetField('number')),
275-
'completion' => $shippingAddress->getStreetLine($this->getStreetField('complement')),
276-
'neighborhood' => $shippingAddress->getStreetLine($this->getStreetField('district')),
277-
'city' => $shippingAddress->getCity(),
278-
'state' => $shippingAddress->getRegionCode()
275+
'postal_code' => $shipping->getPostcode(),
276+
'street' => $shipping->getStreetLine($this->getStreetField('street')),
277+
'number' => $shipping->getStreetLine($this->getStreetField('number')),
278+
'completion' => $shipping->getStreetLine($this->getStreetField('complement')),
279+
'neighborhood' => $shipping->getStreetLine($this->getStreetField('district')),
280+
'city' => $shipping->getCity(),
281+
'state' => $shipping->getRegionCode()
279282
];
280283
}
281284

@@ -290,7 +293,7 @@ protected function getAddresses($order): array
290293
*/
291294
public function getStreetField(string $config): int
292295
{
293-
return (int) $this->helper->getConfig($config, 'address', 'vindi_vp') + 1;
296+
return (int)$this->helper->getConfig($config, 'address', 'vindi_vp') + 1;
294297
}
295298

296299
/**
@@ -302,23 +305,26 @@ public function getStreetField(string $config): int
302305
protected function getItemsData(Order $order): array
303306
{
304307
$items = [];
305-
$quoteItems = $order->getAllItems();
306-
307-
/** @var OrderItemInterface $quoteItem */
308-
foreach ($quoteItems as $quoteItem) {
308+
foreach ($order->getAllItems() as $quoteItem) {
309309
if ($quoteItem->getParentItemId() || $quoteItem->getParentItem() || $quoteItem->getPrice() == 0) {
310310
continue;
311311
}
312312

313-
$item = [];
314-
$item['description'] = $quoteItem->getName();
315-
$item['quantity'] = (string) $quoteItem->getQtyOrdered();
316-
$item['price_unit'] = (string) $quoteItem->getPrice();
317-
$item['code'] = $quoteItem->getProductId();
318-
$item['sku_code'] = $quoteItem->getSku();
319-
$item['extra'] = $quoteItem->getItemId();
313+
$unitPrice = $quoteItem->getRowTotalInclTax() / (float)$quoteItem->getQtyOrdered();
314+
315+
$item = [
316+
'description' => $quoteItem->getName(),
317+
'quantity' => (string)$quoteItem->getQtyOrdered(),
318+
'price_unit' => number_format($unitPrice, 2, '.', ''),
319+
'code' => $quoteItem->getProductId(),
320+
'sku_code' => $quoteItem->getSku(),
321+
'extra' => $quoteItem->getItemId()
322+
];
320323

321-
$this->eventManager->dispatch('vindi_payment_get_item', ['item' => &$item, 'quote_item' => $quoteItem]);
324+
$this->eventManager->dispatch(
325+
'vindi_payment_get_item',
326+
['item' => &$item, 'quote_item' => $quoteItem]
327+
);
322328

323329
$items[] = $item;
324330
}

0 commit comments

Comments
 (0)