| 1 |
import axios from "axios"; |
| 2 |
import { paymentAuthorized, updateContact, updateShippingMethod } from "./dispatcher"; |
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
export const useFetch = (url) => { |
| 7 |
|
| 8 |
const content_type = url == "hyperpay_process_checkout" ? "application/x-www-form-urlencoded" : "multipart/form-data" |
| 9 |
|
| 10 |
const instance = axios.create({ |
| 11 |
baseURL: `/?wc-ajax=${url}`, |
| 12 |
withCredentials: true, |
| 13 |
headers: { |
| 14 |
"Content-Type": content_type |
| 15 |
} |
| 16 |
}); |
| 17 |
|
| 18 |
return instance |
| 19 |
} |
| 20 |
|
| 21 |
export const raiseError = (props, message = null, Json = true) => { |
| 22 |
|
| 23 |
if (Json) { |
| 24 |
props.onError(message ?? "could not create order") |
| 25 |
} else { |
| 26 |
const container = document.querySelector('.wc-block-components-notices') |
| 27 |
container.innerHTML = message; |
| 28 |
} |
| 29 |
|
| 30 |
return { status: "ABORT" } |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* |
| 35 |
* @param {dispatcher} dispatcher |
| 36 |
* @param {string} checkoutId |
| 37 |
* @param {object} shippingMethod |
| 38 |
* @returns |
| 39 |
*/ |
| 40 |
export const generateOptions = (props, shippingMethod, checkoutId) => { |
| 41 |
|
| 42 |
return { |
| 43 |
|
| 44 |
applePay: { |
| 45 |
version: "3", |
| 46 |
buttonType: props.setting.extraScriptData.action_type, |
| 47 |
buttonStyle: props.setting.extraScriptData.action_style, |
| 48 |
requiredBillingContactFields: ['postalAddress', 'email', "phone"], |
| 49 |
requiredShippingContactFields: ['postalAddress', 'email', "phone"], |
| 50 |
currencyCode: props.billing.currency.code, |
| 51 |
onCancel: props.onClose, |
| 52 |
total: { |
| 53 |
label: "By HyperPay", |
| 54 |
amount: (props.billing.cartTotal.value ?? 0) / (10 ** props.billing.currency.minorUnit ?? 0) |
| 55 |
}, |
| 56 |
onShippingMethodSelected: async function (shipping_method) { |
| 57 |
return updateShippingMethod(props, shippingMethod, shipping_method) |
| 58 |
}, |
| 59 |
onPaymentAuthorized: async function (payment) { |
| 60 |
return paymentAuthorized(props, shippingMethod, payment, checkoutId) |
| 61 |
}, |
| 62 |
onShippingContactSelected: async function (shippingContact) { |
| 63 |
return updateContact(props, shippingContact) |
| 64 |
}, |
| 65 |
}, |
| 66 |
|
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
export const paymentResult = (props, response) => { |
| 71 |
|
| 72 |
if (response.data.result != "success") |
| 73 |
return raiseError(props, response.data.result) |
| 74 |
return { |
| 75 |
newTotal: response.data.total, |
| 76 |
newLineItems: response.data.displayItems, |
| 77 |
newShippingMethods: response.data.shipping_options |
| 78 |
} |
| 79 |
} |
| 80 |
|
| 81 |
export const clearUpCopyAndPay = () => { |
| 82 |
if (window.wpwl !== undefined && window.wpwl.unload !== undefined) { |
| 83 |
window.wpwl.unload(); |
| 84 |
document.querySelectorAll("script").forEach(i => { |
| 85 |
if (i.src.includes('static.min.js') || i.src.includes('paymentWidgets.js')) { |
| 86 |
i.remove() |
| 87 |
} |
| 88 |
}) |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
|