| 1 |
import { getSetting } from '@woocommerce/settings'; |
| 2 |
import $ from 'jquery'; |
| 3 |
const settings = getSetting('payplug_data', {}); |
| 4 |
|
| 5 |
// jQuery's .fail() callback receives a jqXHR object, not an Error - normalize it so |
| 6 |
// callers doing `catch (error) { error.message }` get a usable string instead of undefined. |
| 7 |
function toRequestError(jqXHR, textStatus, errorThrown) { |
| 8 |
const data = jqXHR?.responseJSON?.data; |
| 9 |
const message = (typeof data === 'string' && data) || data?.message || errorThrown || textStatus || 'Request failed'; |
| 10 |
return new Error(message); |
| 11 |
} |
| 12 |
|
| 13 |
export const getPayment = (props, _settings, order_id) => { |
| 14 |
const data = getPaymentData(props); |
| 15 |
return new Promise((resolve, reject) => { |
| 16 |
return $.ajax({ |
| 17 |
type: 'POST', |
| 18 |
data: data, |
| 19 |
url: settings.payplug_create_intent_payment, |
| 20 |
|
| 21 |
}).done(function (response) { |
| 22 |
resolve(response); |
| 23 |
|
| 24 |
}).fail(function (jqXHR, textStatus, errorThrown) { |
| 25 |
reject(toRequestError(jqXHR, textStatus, errorThrown)); |
| 26 |
|
| 27 |
}); |
| 28 |
}); |
| 29 |
|
| 30 |
function getPaymentData(props) { |
| 31 |
return { |
| 32 |
"order_id": order_id, |
| 33 |
"woocommerce-process-checkout-nonce": settings.wp_nonce, |
| 34 |
"gateway": _settings?.payment_method, |
| 35 |
"order_pay_key": _settings?.order_pay_key || '' |
| 36 |
} |
| 37 |
} |
| 38 |
}; |
| 39 |
|
| 40 |
export const check_payment = (data) => { |
| 41 |
return new Promise((resolve, reject) => { |
| 42 |
$.ajax({ |
| 43 |
type: 'POST', |
| 44 |
data: data, |
| 45 |
url: settings.payplug_integrated_payment_check_payment_url |
| 46 |
}).done(function (response) { |
| 47 |
resolve(response); |
| 48 |
|
| 49 |
}).fail(function (jqXHR, textStatus, errorThrown) { |
| 50 |
reject(toRequestError(jqXHR, textStatus, errorThrown)); |
| 51 |
|
| 52 |
}); |
| 53 |
}); |
| 54 |
}; |
| 55 |
|