|
| 1 | +<?php |
| 2 | + |
| 3 | +/** @var AbstractPaymentMethodWire $magewire */ |
| 4 | +/** @var Template $block */ |
| 5 | +/** @var Escaper $escaper */ |
| 6 | + |
| 7 | +use Adyen\Hyva\Magewire\Payment\Method\AbstractPaymentMethodWire; |
| 8 | +use Magento\Framework\Escaper; |
| 9 | +use Magento\Framework\View\Element\Template; |
| 10 | + |
| 11 | +?> |
| 12 | + |
| 13 | +<div> |
| 14 | + <div id="<?= $magewire->getContainerName() ?>ActionContainer" wire:ignore></div> |
| 15 | + |
| 16 | + <script> |
| 17 | + class amazonPayComponentHandler extends componentHandler { |
| 18 | + constructor(paymentMethodCode, wire, elementLabel) { |
| 19 | + super(paymentMethodCode, wire, elementLabel); |
| 20 | + |
| 21 | + this.amazonPayStatus = null; |
| 22 | + } |
| 23 | + |
| 24 | + buildConfiguration (paymentMethod, paymentMethodsExtraInfo) { |
| 25 | + let baseComponentConfiguration = super.buildConfiguration(paymentMethod, paymentMethodsExtraInfo); |
| 26 | + |
| 27 | + let formattedShippingAddress = {}; |
| 28 | + let formattedBillingAddress = {}; |
| 29 | + |
| 30 | + const shippingAddress = this.wire.get('shippingAddress'); |
| 31 | + const billingAddress = this.wire.get('billingAddress'); |
| 32 | + |
| 33 | + if (shippingAddress) { |
| 34 | + formattedShippingAddress = this.getFormattedAddress(JSON.parse(shippingAddress)); |
| 35 | + } |
| 36 | + |
| 37 | + if (billingAddress) { |
| 38 | + formattedBillingAddress = this.getFormattedAddress(JSON.parse(billingAddress)); |
| 39 | + } |
| 40 | + |
| 41 | + baseComponentConfiguration.showPayButton = true; |
| 42 | + baseComponentConfiguration.onClick = function(resolve, reject) { |
| 43 | + resolve(); |
| 44 | + } |
| 45 | + |
| 46 | + baseComponentConfiguration.productType = 'PayAndShip'; |
| 47 | + baseComponentConfiguration.checkoutMode = 'ProcessOrder'; |
| 48 | + let url = new URL(location.href); |
| 49 | + url.searchParams.delete('amazonCheckoutSessionId'); |
| 50 | + baseComponentConfiguration.returnUrl = url.href; |
| 51 | + |
| 52 | + if (formattedShippingAddress && |
| 53 | + formattedShippingAddress.telephone) { |
| 54 | + baseComponentConfiguration.addressDetails = { |
| 55 | + name: formattedShippingAddress.firstName + |
| 56 | + ' ' + |
| 57 | + formattedShippingAddress.lastName, |
| 58 | + addressLine1: formattedShippingAddress.street, |
| 59 | + addressLine2: formattedShippingAddress.houseNumber, |
| 60 | + city: formattedShippingAddress.city, |
| 61 | + postalCode: formattedShippingAddress.postalCode, |
| 62 | + countryCode: formattedShippingAddress.country, |
| 63 | + phoneNumber: formattedShippingAddress.telephone |
| 64 | + }; |
| 65 | + if (baseComponentConfiguration.addressDetails.countryCode === 'US') { |
| 66 | + baseComponentConfiguration.addressDetails.stateOrRegion = quote.shippingAddress().regionCode |
| 67 | + } |
| 68 | + } else if (formattedBillingAddress && |
| 69 | + formattedBillingAddress.telephone) { |
| 70 | + baseComponentConfiguration.addressDetails = { |
| 71 | + name: formattedBillingAddress.firstName + |
| 72 | + ' ' + |
| 73 | + formattedBillingAddress.lastName, |
| 74 | + addressLine1: formattedBillingAddress.street, |
| 75 | + addressLine2: formattedBillingAddress.houseNumber, |
| 76 | + |
| 77 | + city: formattedBillingAddress.city, |
| 78 | + postalCode: formattedBillingAddress.postalCode, |
| 79 | + countryCode: formattedBillingAddress.country, |
| 80 | + phoneNumber: formattedBillingAddress.telephone |
| 81 | + }; |
| 82 | + if (baseComponentConfiguration.addressDetails.countryCode === 'US') { |
| 83 | + baseComponentConfiguration.addressDetails.stateOrRegion = quote.billingAddress().regionCode |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return baseComponentConfiguration; |
| 88 | + } |
| 89 | + |
| 90 | + mountComponent(checkoutComponent, paymentMethodType, configuration, result = undefined) { |
| 91 | + let self = this; |
| 92 | + const containerId = "<?= $magewire->getContainerName() ?>ActionContainer"; |
| 93 | + |
| 94 | + let url = new URL(location.href); |
| 95 | + // Handles the redirect back to checkout page with amazonSessionKey in url |
| 96 | + // If previous attempt has failed, component is mounted again and session ID is ignored. |
| 97 | + if (this.amazonPayStatus !== false && url.searchParams.has('amazonCheckoutSessionId')) { |
| 98 | + let componentConfig = { |
| 99 | + amazonCheckoutSessionId: url.searchParams.get('amazonCheckoutSessionId'), |
| 100 | + showOrderButton: false, |
| 101 | + amount: { |
| 102 | + currency: configuration.amount.currency, |
| 103 | + value: configuration.amount.value |
| 104 | + }, |
| 105 | + showChangePaymentDetailsButton: false, |
| 106 | + onSubmit: async (state, amazonPayComponent) => { |
| 107 | + self.placeOrder(state.data, null, amazonPayComponent); |
| 108 | + }, |
| 109 | + onError: (error) => { |
| 110 | + unmountAdyenComponent(); |
| 111 | + super.mountComponent(checkoutComponent, paymentMethodType, configuration, result); |
| 112 | + } |
| 113 | + } |
| 114 | + try { |
| 115 | + let amazonPayComponent = checkoutComponent.create( |
| 116 | + paymentMethodType, |
| 117 | + componentConfig |
| 118 | + ); |
| 119 | + |
| 120 | + amazonPayComponent.mount('#' + containerId); |
| 121 | + amazonPayComponent.submit(); |
| 122 | + } catch (err) { |
| 123 | + console.log(err); |
| 124 | + } |
| 125 | + } else { |
| 126 | + super.mountComponent(checkoutComponent, paymentMethodType, configuration, result); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + placeOrder(data, publicHash = null, amazonPayComponent) { |
| 131 | + let self = this; |
| 132 | + let wire = self.wire; |
| 133 | + |
| 134 | + wire.placeOrder({ |
| 135 | + stateData: data, |
| 136 | + ccType: this.getCreditCardType(), |
| 137 | + publicHash: publicHash |
| 138 | + }) |
| 139 | + .then(() => { |
| 140 | + let paymentStatus = JSON.parse(wire.get('paymentStatus')); |
| 141 | + self.handleAdyenResult(paymentStatus, amazonPayComponent); |
| 142 | + }).catch(() => { |
| 143 | + console.log('Error occurred during order placement') |
| 144 | + }); |
| 145 | + } |
| 146 | + |
| 147 | + handleAdyenResult(responseJSON, amazonPayComponent) { |
| 148 | + let self = this; |
| 149 | + |
| 150 | + if (responseJSON.isRefused) { |
| 151 | + let message = "<?= __("The Payment is Refused") ?>"; |
| 152 | + |
| 153 | + this.amazonPayStatus = false; |
| 154 | + |
| 155 | + self.renderMessage(message); |
| 156 | + setTimeout(() => { |
| 157 | + self.clearMessage(); |
| 158 | + }, 4000); |
| 159 | + |
| 160 | + amazonPayComponent.handleDeclineFlow(); |
| 161 | + } else { |
| 162 | + super.handleAdyenResult(responseJSON); |
| 163 | + } |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + window.addEventListener('checkout:payment:method-list-boot', async (event) => { |
| 168 | + unmountAdyenComponent(); |
| 169 | + await init(event.detail.method); |
| 170 | + }); |
| 171 | + |
| 172 | + window.addEventListener('checkout:payment:method-activate', async (event) => { |
| 173 | + await init(event.detail.method); |
| 174 | + }); |
| 175 | + |
| 176 | + async function init(methodCode) { |
| 177 | + try { |
| 178 | + let wire = Magewire.find('checkout.payment.methods.' + methodCode); |
| 179 | + wire.refreshProperties() |
| 180 | + .then(() => { |
| 181 | + let amazonPayHandler = new amazonPayComponentHandler( |
| 182 | + methodCode, |
| 183 | + wire, |
| 184 | + '<?= $magewire->getContainerName() ?>ActionContainer' |
| 185 | + ); |
| 186 | + |
| 187 | + window.AdyenPaymentHandler = amazonPayHandler; |
| 188 | + |
| 189 | + if (methodCode !== 'adyen_amazonpay') { |
| 190 | + amazonPayHandler.renderMethodUnavailableMessage(); |
| 191 | + return; |
| 192 | + } |
| 193 | + |
| 194 | + if (wire.get('requiresShipping')) { |
| 195 | + amazonPayHandler.renderMessage('Please select shipping method.'); |
| 196 | + } else { |
| 197 | + let rawResponse = wire.get('paymentResponse'); |
| 198 | + let paymentMethods = JSON.parse(rawResponse); |
| 199 | + amazonPayHandler.activatePaymentMethod(methodCode, paymentMethods); |
| 200 | + hidePrimaryButton(); |
| 201 | + } |
| 202 | + }).catch(() => { |
| 203 | + console.log('Error occurred during Amazon Pay component load') |
| 204 | + }); |
| 205 | + } catch (e) { |
| 206 | + } |
| 207 | + } |
| 208 | + </script> |
| 209 | +</div> |
0 commit comments