|
| 1 | +<?php |
| 2 | +use Adyen\Hyva\Magewire\Payment\Method\AbstractPaymentMethodWire; |
| 3 | +use Magento\Framework\Escaper; |
| 4 | +use Magento\Framework\View\Element\Template; |
| 5 | + |
| 6 | +/** @var AbstractPaymentMethodWire $magewire */ |
| 7 | +/** @var Template $block */ |
| 8 | +/** @var Escaper $escaper */ |
| 9 | + |
| 10 | +?> |
| 11 | + |
| 12 | +<div> |
| 13 | + |
| 14 | + <div id="<?= $magewire->getContainerName() ?>ActionContainer" wire:ignore></div> |
| 15 | + |
| 16 | + <script> |
| 17 | + class facilypay3xComponentHandler extends componentHandler { |
| 18 | + constructor(paymentMethodCode, wire, elementLabel) { |
| 19 | + super(paymentMethodCode, wire, elementLabel); |
| 20 | + } |
| 21 | + |
| 22 | + buildConfiguration(paymentMethod, paymentMethodsExtraInfo) { |
| 23 | + let baseComponentConfiguration = super.buildConfiguration(paymentMethod, paymentMethodsExtraInfo); |
| 24 | + |
| 25 | + let formattedShippingAddress = {}; |
| 26 | + let formattedBillingAddress = {}; |
| 27 | + let shopperDateOfBirth = ''; |
| 28 | + let email = ''; |
| 29 | + |
| 30 | + const shippingAddress = this.wire.get('shippingAddress'); |
| 31 | + const billingAddress = this.wire.get('billingAddress'); |
| 32 | + const quoteData = this.wire.get('quoteData'); |
| 33 | + const customerData = this.wire.get('customerData'); |
| 34 | + |
| 35 | + |
| 36 | + if (customerData) { |
| 37 | + const parsedCustomerData = JSON.parse(customerData); |
| 38 | + shopperDateOfBirth = parsedCustomerData.shopper_date_of_birth || ''; |
| 39 | + email = parsedCustomerData.email || ''; |
| 40 | + } |
| 41 | + |
| 42 | + if (!email && quoteData) { |
| 43 | + const parsedQuoteData = JSON.parse(quoteData); |
| 44 | + email = parsedQuoteData.shopper_email || ''; |
| 45 | + } |
| 46 | + |
| 47 | + if (shippingAddress) { |
| 48 | + formattedShippingAddress = this.getFormattedAddress(JSON.parse(shippingAddress)); |
| 49 | + } |
| 50 | + |
| 51 | + if (billingAddress) { |
| 52 | + formattedBillingAddress = this.getFormattedAddress(JSON.parse(billingAddress)); |
| 53 | + } |
| 54 | + |
| 55 | + baseComponentConfiguration.data = {}; |
| 56 | + |
| 57 | + if (formattedShippingAddress) { |
| 58 | + baseComponentConfiguration.data.deliveryAddress = { |
| 59 | + city: formattedShippingAddress.city, |
| 60 | + country: formattedShippingAddress.country, |
| 61 | + houseNumberOrName: formattedShippingAddress.houseNumber, |
| 62 | + postalCode: formattedShippingAddress.postalCode, |
| 63 | + street: formattedShippingAddress.street |
| 64 | + }; |
| 65 | + } |
| 66 | + |
| 67 | + if (formattedBillingAddress) { |
| 68 | + baseComponentConfiguration.data.personalDetails = { |
| 69 | + firstName: formattedBillingAddress.firstName, |
| 70 | + lastName: formattedBillingAddress.lastName, |
| 71 | + telephoneNumber: formattedBillingAddress.telephone, |
| 72 | + shopperEmail: email, |
| 73 | + dateOfBirth: shopperDateOfBirth, |
| 74 | + }; |
| 75 | + baseComponentConfiguration.data.billingAddress = { |
| 76 | + city: formattedBillingAddress.city, |
| 77 | + country: formattedBillingAddress.country, |
| 78 | + houseNumberOrName: formattedBillingAddress.houseNumber, |
| 79 | + postalCode: formattedBillingAddress.postalCode, |
| 80 | + street: formattedBillingAddress.street, |
| 81 | + }; |
| 82 | + } |
| 83 | + |
| 84 | + return baseComponentConfiguration; |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + window.addEventListener('checkout:payment:method-list-boot', async (event) => { |
| 89 | + unmountAdyenComponent(); |
| 90 | + await init(event.detail.method); |
| 91 | + }); |
| 92 | + |
| 93 | + window.addEventListener('checkout:payment:method-activate', async (event) => { |
| 94 | + await init(event.detail.method); |
| 95 | + }); |
| 96 | + |
| 97 | + window.addEventListener('checkout:init:evaluation', event => { |
| 98 | + hyvaCheckout.evaluation.registerValidator('validate-adyen-component-state', (element, component) => { |
| 99 | + let isValid; |
| 100 | + if (!window.AdyenPaymentHandler.actionComponent.isValid) { |
| 101 | + window.AdyenPaymentHandler.actionComponent.showValidation(); |
| 102 | + isValid = false; |
| 103 | + } else { |
| 104 | + isValid = true; |
| 105 | + } |
| 106 | + return isValid; |
| 107 | + }); |
| 108 | + }); |
| 109 | + |
| 110 | + async function init(methodCode) { |
| 111 | + try { |
| 112 | + let wire = Magewire.find('checkout.payment.methods.' + methodCode); |
| 113 | + |
| 114 | + wire.refreshProperties() |
| 115 | + .then(() => { |
| 116 | + let methodHandler = new facilypay3xComponentHandler( |
| 117 | + methodCode, |
| 118 | + wire, |
| 119 | + '<?= $magewire->getContainerName() ?>ActionContainer' |
| 120 | + ); |
| 121 | + window.AdyenPaymentHandler = methodHandler; |
| 122 | + if (methodCode !== '<?= $magewire->getMethodCode() ?>') { |
| 123 | + methodHandler.renderMethodUnavailableMessage(); |
| 124 | + return; |
| 125 | + } |
| 126 | + if (wire.get('requiresShipping')) { |
| 127 | + methodHandler.renderMessage('Please select shipping method.'); |
| 128 | + } else { |
| 129 | + let rawResponse = wire.get('paymentResponse'); |
| 130 | + let paymentMethods = JSON.parse(rawResponse); |
| 131 | + methodHandler.activatePaymentMethod(methodCode, paymentMethods, '<?= $magewire->getContainerName() ?>ActionContainer'); |
| 132 | + showPrimaryButton(); |
| 133 | + } |
| 134 | + }).catch(() => { |
| 135 | + console.error(`Error occurred during ${methodCode} component load`); |
| 136 | + }); |
| 137 | + } catch (e) { |
| 138 | + console.error('Error in init function:', e); |
| 139 | + } |
| 140 | + } |
| 141 | + </script> |
| 142 | +</div> |
0 commit comments