Skip to content

Commit 2a89cc4

Browse files
Merge pull request #40 from buckaroo-it/BP-3791-Fix-Grand-Total-Not-Updating-on-Gift-Card-Payment
BP-3791-Fix-Grand-Total-Not-Updating-on-Gift-Card-Payment
2 parents fd1c52b + 6516ea7 commit 2a89cc4

File tree

2 files changed

+82
-29
lines changed

2 files changed

+82
-29
lines changed

Magewire/Payment/Method/Giftcards.php

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Buckaroo\Magento2\Model\Giftcard\Response\Giftcard as GiftcardResponse;
1717
use Buckaroo\Magento2\Model\Giftcard\Request\GiftcardInterface as GiftcardRequest;
1818
use Buckaroo\Magento2\Model\ConfigProvider\Method\Giftcards as MethodConfigProvider;
19+
use Magento\Framework\Pricing\Helper\Data as PricingHelper;
1920

2021
class Giftcards extends Component\Form implements EvaluationInterface
2122
{
@@ -34,6 +35,7 @@ class Giftcards extends Component\Form implements EvaluationInterface
3435
protected GiftcardResponse $giftcardResponse;
3536

3637
protected Log $logger;
38+
protected $pricingHelper;
3739

3840
public function __construct(
3941
UrlInterface $urlBuilder,
@@ -42,7 +44,8 @@ public function __construct(
4244
MethodConfigProvider $methodConfigProvider,
4345
GiftcardRequest $giftcardRequest,
4446
GiftcardResponse $giftcardResponse,
45-
Log $logger
47+
Log $logger,
48+
PricingHelper $pricingHelper
4649
) {
4750
$this->urlBuilder = $urlBuilder;
4851
$this->sessionCheckout = $sessionCheckout;
@@ -51,6 +54,7 @@ public function __construct(
5154
$this->giftcardRequest = $giftcardRequest;
5255
$this->giftcardResponse = $giftcardResponse;
5356
$this->logger = $logger;
57+
$this->pricingHelper = $pricingHelper;
5458
}
5559

5660
/**
@@ -150,7 +154,32 @@ public function applyGiftcard(
150154
}
151155
}
152156

157+
/**
158+
* Convert and format price value for current store
159+
*
160+
* @param float $price
161+
* @return float|string
162+
*/
163+
public function getFormattedPrice($price)
164+
{
165+
return $this->pricingHelper->currency($price, true, false);
166+
}
153167

168+
public function getRemainingAmount()
169+
{
170+
$quote = $this->sessionCheckout->getQuote();
171+
172+
$grandTotal = round(floatval($quote->getGrandTotal()), 2);
173+
174+
// Get the amount already paid through group transactions
175+
$alreadyPaid = $this->groupTransaction->getAlreadyPaid($quote->getReservedOrderId());
176+
177+
// Calculate the remaining amount
178+
$remainingAmount = $grandTotal - $alreadyPaid;
179+
180+
// Ensure the remaining amount is never negative
181+
$this->emit("remainingAmount", $this->getFormattedPrice($remainingAmount));
182+
}
154183
protected function getGiftcardResponse(Quote $quote, $response)
155184
{
156185
$this->giftcardResponse->set($response, $quote);
@@ -183,8 +212,8 @@ protected function getGiftcardResponse(Quote $quote, $response)
183212
$this->giftcardResponse->getCurrency()
184213
);
185214
}
186-
187215
return [
216+
'remainder_amount_currency' => $this->getFormattedPrice($remainingAmount),
188217
'remainder_amount' => $remainingAmount,
189218
'already_paid' => $this->giftcardResponse->getAlreadyPaid($quote),
190219
'remaining_amount_message' => $buttonMessage,

view/frontend/templates/component/payment/after.phtml

Lines changed: 51 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -373,24 +373,59 @@
373373
},
374374
giftcards($el) {
375375
return Object.assign(hyva.formValidation($el), {
376-
card:'',
376+
card: '',
377377
pin: '',
378378
cardNumber: '',
379-
canSubmit:false,
379+
canSubmit: false,
380+
381+
async update() {
382+
await this.$wire.getRemainingAmount();
383+
},
384+
listenToUpdate(){
385+
this.$wire.on('remainingAmount', async (response) => {
386+
this.updateGrandTotal(response);
387+
})
388+
},
380389
async formValid() {
381-
try {
382-
await this.validate();
383-
} catch (error) {
384-
return false;
385-
}
386-
return true;
390+
try {
391+
await this.validate();
392+
} catch (error) {
393+
return false;
394+
}
395+
return true;
387396
},
397+
388398
async submit() {
389-
if(!await this.formValid()) {
399+
if (!await this.formValid()) {
390400
return;
391401
}
392402
await this.$wire.applyGiftcard(this.card, this.cardNumber, this.pin);
393403
},
404+
405+
listenToSubmit() {
406+
this.$wire.on('giftcard_response', async (response) => {
407+
if (response.error) {
408+
this.displayError(response.error);
409+
} else if (response.remainder_amount === undefined) {
410+
this.displayError(response.message);
411+
} else if (response.remainder_amount == 0) {
412+
this.canSubmit = true;
413+
await hyvaCheckout.order.place();
414+
} else if (response.remainder_amount != 0) {
415+
this.displaySuccess(response);
416+
this.updateGrandTotal(response.remainder_amount_currency);
417+
}
418+
})
419+
},
420+
updateGrandTotal(remainder_amount_currency) {
421+
const grandTotalElement = document.querySelector('.grand_total .value');
422+
grandTotalElement.textContent = remainder_amount_currency
423+
424+
setTimeout(function () {
425+
grandTotalElement.textContent = remainder_amount_currency
426+
427+
}, 2000);
428+
},
394429
displaySuccess(response) {
395430
window.dispatchEvent(new CustomEvent('buckaroo-modal-show', {
396431
detail: {
@@ -405,6 +440,7 @@
405440
}
406441
}));
407442
},
443+
408444
displayError(message) {
409445
window.dispatchEvent(new CustomEvent('buckaroo-modal-show', {
410446
detail: {
@@ -415,30 +451,18 @@
415451
}
416452
}));
417453
},
418-
listenToSubmit() {
419-
this.$wire.on('giftcard_response', async (response) => {
420-
if (response.error) {
421-
this.displayError(response.error);
422-
} else if(response.remainder_amount === undefined) {
423-
this.displayError(response.message);
424-
} else if (response.remainder_amount == 0) {
425-
this.canSubmit = true;
426-
await hyvaCheckout.order.place();
427-
} else if (response.remainder_amount != 0) {
428-
this.displaySuccess(response);
429-
}
430-
})
431-
},
454+
432455
register() {
433456
this.listenToSubmit();
457+
this.update();
458+
this.listenToUpdate();
434459
window.buckarooTask = async () => {
435-
if(!this.canSubmit) {
460+
if (!this.canSubmit) {
436461
await this.submit();
437462
}
438463
};
439464
},
440-
441-
})
465+
});
442466
},
443467
mrCash($el) {
444468
return Object.assign(hyva.formValidation($el), {
@@ -566,4 +590,4 @@
566590
}
567591
}
568592

569-
</script>
593+
</script>

0 commit comments

Comments
 (0)