changed plugin name to cipf
This commit is contained in:
30
plugins/cipf_plugin/js/form_builder_patch/multiple_modals.js
Normal file
30
plugins/cipf_plugin/js/form_builder_patch/multiple_modals.js
Normal file
@@ -0,0 +1,30 @@
|
||||
|
||||
let modal_wrapper_UNIQ_ID_7623 = document.querySelector('#de-fb-modal-wrapper-');
|
||||
|
||||
// create an observer on first #de-fb-modal-wrapper- to check if child nodes are added
|
||||
const observer_UNIQ_ID_7623 = new MutationObserver(wait_for_close_button_UNIQ_ID_7623);
|
||||
observer_UNIQ_ID_7623.observe(modal_wrapper_UNIQ_ID_7623, {
|
||||
subtree: true,
|
||||
childList: true,
|
||||
});
|
||||
|
||||
// observe mutations to see if they include the creation of the button .modal-close
|
||||
// if the button is created, add an eventListener to it
|
||||
function wait_for_close_button_UNIQ_ID_7623(mutationsList) {
|
||||
mutationsList.forEach((mutation) => {
|
||||
// check if nodes were added
|
||||
if (mutation.type !== 'childList')
|
||||
return;
|
||||
// check if added nodes includes the button .modal-close
|
||||
let modal_close = document.querySelector('#de-fb-modal-wrapper- .modal-close');
|
||||
if (modal_close !== null) {
|
||||
modal_close.addEventListener("click", delete_modal_UNIQ_ID_7623);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// when triggered, the .modal-close button will remove all childs from #de-fb-modal-wrapper-
|
||||
function delete_modal_UNIQ_ID_7623() {
|
||||
modal_wrapper_UNIQ_ID_7623.innerHTML = '';
|
||||
}
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
// https://stackoverflow.com/questions/12741517/how-to-make-url-validation-without-http-or-add-it-after-validation-passed/44848476#44848476
|
||||
|
||||
let old_url = jQuery.validator.methods.url;
|
||||
jQuery.validator.addMethod( 'url', function(value, element) {
|
||||
let url = old_url.bind(this);
|
||||
return url(value, element) || url('http://' + value, element);
|
||||
}, 'Please enter a valid URL'
|
||||
);
|
||||
1463
plugins/cipf_plugin/js/images/image-edit.js
Normal file
1463
plugins/cipf_plugin/js/images/image-edit.js
Normal file
File diff suppressed because it is too large
Load Diff
381
plugins/cipf_plugin/js/images/image_editor.js
Normal file
381
plugins/cipf_plugin/js/images/image_editor.js
Normal file
@@ -0,0 +1,381 @@
|
||||
|
||||
console.log("---------------inside image_editor.js--------------");
|
||||
const edit_image_button = document.getElementById('edit_image');
|
||||
const image_id_field = document.getElementById('image_id');
|
||||
const media_editor = document.getElementById('media_editor');
|
||||
|
||||
edit_image_button.addEventListener('click', () => {
|
||||
const image_id = image_id_field.value;
|
||||
|
||||
//PLGNTLS_ajax("postid", image_id, 'image-editor')
|
||||
const data_obj = {
|
||||
'postid': image_id,
|
||||
};
|
||||
PLGNTLS_ajax(data_obj, 'image_editor_PLGNTLS')
|
||||
.then((response) => response.json())
|
||||
.then((data) => {
|
||||
console.log("data: ");
|
||||
console.log(data);
|
||||
media_editor.innerHTML = data.data.html;
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log("error: ");
|
||||
console.log(error);
|
||||
});
|
||||
});
|
||||
|
||||
/*
|
||||
open: function (e, t, i) { // t: nonce
|
||||
this._view = i; // div: #media-editor-114
|
||||
var a = c('#image-editor-' + e), // e: image id
|
||||
o = c('#media-head-' + e),
|
||||
r = c('#imgedit-open-btn-' + e),
|
||||
s = r.siblings('.spinner');
|
||||
if (!r.hasClass('button-activated')) return s.addClass('is-active'),
|
||||
c.ajax({
|
||||
url: ajaxurl,
|
||||
type: 'post',
|
||||
data: {
|
||||
action: 'image-editor',
|
||||
_ajax_nonce: t,
|
||||
postid: e,
|
||||
do : 'open'},
|
||||
beforeSend: function () {
|
||||
r.addClass('button-activated')
|
||||
}
|
||||
}).done(
|
||||
function (t) {
|
||||
var i;
|
||||
'-1' === t &&
|
||||
(
|
||||
i = n('Could not load the preview image.'),
|
||||
a.html(
|
||||
'<div class="notice notice-error" tabindex="-1" role="alert"><p>' + i + '</p></div>'
|
||||
)
|
||||
),
|
||||
t.data &&
|
||||
t.data.html &&
|
||||
a.html(t.data.html),
|
||||
o.fadeOut(
|
||||
'fast',
|
||||
function () {
|
||||
a.fadeIn(
|
||||
'fast',
|
||||
function () {
|
||||
i &&
|
||||
c(document).trigger('image-editor-ui-ready')
|
||||
}
|
||||
),
|
||||
r.removeClass('button-activated'),
|
||||
s.removeClass('is-active')
|
||||
}
|
||||
),
|
||||
l.init(e)
|
||||
}
|
||||
) },
|
||||
imgLoaded: function (t) {
|
||||
var i = c('#image-preview-' + t),
|
||||
e = c('#imgedit-crop-' + t);
|
||||
void 0 === this.hold.sizer &&
|
||||
this.init(t),
|
||||
this.initCrop(t, i, e),
|
||||
this.setCropSelection(
|
||||
t,
|
||||
{
|
||||
x1: 0,
|
||||
y1: 0,
|
||||
x2: 0,
|
||||
y2: 0,
|
||||
width: i.innerWidth(),
|
||||
height: i.innerHeight()
|
||||
}
|
||||
),
|
||||
this.toggleEditor(t, 0, !0)
|
||||
},
|
||||
focusManager: function () {
|
||||
setTimeout(
|
||||
function () {
|
||||
var t = c('.notice[role="alert"]');
|
||||
(t = t.length ? t : c('.imgedit-wrap').find(':tabbable:first')).attr('tabindex', '-1').trigger('focus')
|
||||
},
|
||||
100
|
||||
)
|
||||
},
|
||||
initCrop: function (a, t, i) {
|
||||
var o = this,
|
||||
r = c('#imgedit-sel-width-' + a),
|
||||
s = c('#imgedit-sel-height-' + a),
|
||||
n = c('#imgedit-start-x-' + a),
|
||||
d = c('#imgedit-start-y-' + a),
|
||||
t = c(t);
|
||||
t.data('imgAreaSelect') ||
|
||||
(
|
||||
o.iasapi = t.imgAreaSelect({
|
||||
parent: i,
|
||||
instance: !0,
|
||||
handles: !0,
|
||||
keys: !0,
|
||||
minWidth: 3,
|
||||
minHeight: 3,
|
||||
onInit: function (t) {
|
||||
c(t).next().css('position', 'absolute').nextAll('.imgareaselect-outer').css('position', 'absolute'),
|
||||
i.children().on(
|
||||
'mousedown, touchstart',
|
||||
function (t) {
|
||||
var i,
|
||||
e = !1;
|
||||
t.shiftKey &&
|
||||
(
|
||||
t = o.iasapi.getSelection(),
|
||||
i = o.getSelRatio(a),
|
||||
e = t &&
|
||||
t.width &&
|
||||
t.height ? t.width + ':' + t.height : i
|
||||
),
|
||||
o.iasapi.setOptions({
|
||||
aspectRatio: e
|
||||
})
|
||||
}
|
||||
)
|
||||
},
|
||||
onSelectStart: function () {
|
||||
l.setDisabled(c('#imgedit-crop-sel-' + a), 1),
|
||||
l.setDisabled(c('.imgedit-crop-clear'), 1),
|
||||
l.setDisabled(c('.imgedit-crop-apply'), 1)
|
||||
},
|
||||
onSelectEnd: function (t, i) {
|
||||
l.setCropSelection(a, i),
|
||||
c('#imgedit-crop > *').is(':visible') ||
|
||||
l.toggleControls(c('.imgedit-crop.button'))
|
||||
},
|
||||
onSelectChange: function (t, i) {
|
||||
var e = l.hold.sizer;
|
||||
r.val(l.round(i.width / e)),
|
||||
s.val(l.round(i.height / e)),
|
||||
n.val(l.round(i.x1 / e)),
|
||||
d.val(l.round(i.y1 / e))
|
||||
}
|
||||
})
|
||||
)
|
||||
},
|
||||
setCropSelection: function (t, i) {
|
||||
if (!(i = i || 0) || i.width < 3 && i.height < 3) return this.setDisabled(c('.imgedit-crop', '#imgedit-panel-' + t), 1),
|
||||
this.setDisabled(c('#imgedit-crop-sel-' + t), 1),
|
||||
c('#imgedit-sel-width-' + t).val(''),
|
||||
c('#imgedit-sel-height-' + t).val(''),
|
||||
c('#imgedit-start-x-' + t).val('0'),
|
||||
c('#imgedit-start-y-' + t).val('0'),
|
||||
c('#imgedit-selection-' + t).val(''),
|
||||
!1;
|
||||
i = {
|
||||
x: i.x1,
|
||||
y: i.y1,
|
||||
w: i.width,
|
||||
h: i.height
|
||||
},
|
||||
this.setDisabled(c('.imgedit-crop', '#imgedit-panel-' + t), 1),
|
||||
c('#imgedit-selection-' + t).val(JSON.stringify(i))
|
||||
},
|
||||
close: function (t, i) {
|
||||
if ((i = i || !1) && this.notsaved(t)) return !1;
|
||||
this.iasapi = {},
|
||||
this.hold = {},
|
||||
this._view ? this._view.back() : c('#image-editor-' + t).fadeOut(
|
||||
'fast',
|
||||
function () {
|
||||
c('#media-head-' + t).fadeIn(
|
||||
'fast',
|
||||
function () {
|
||||
c('#imgedit-open-btn-' + t).trigger('focus')
|
||||
}
|
||||
),
|
||||
c(this).empty()
|
||||
}
|
||||
)
|
||||
},
|
||||
notsaved: function (t) {
|
||||
var i = c('#imgedit-history-' + t).val(),
|
||||
i = '' !== i ? JSON.parse(i) : [];
|
||||
return this.intval(c('#imgedit-undone-' + t).val()) < i.length &&
|
||||
!confirm(c('#imgedit-leaving-' + t).text())
|
||||
},
|
||||
addStep: function (t, i, e) {
|
||||
for (
|
||||
var a = this,
|
||||
o = c('#imgedit-history-' + i),
|
||||
r = '' !== o.val() ? JSON.parse(o.val()) : [],
|
||||
s = c('#imgedit-undone-' + i),
|
||||
n = a.intval(s.val());
|
||||
0 < n;
|
||||
) r.pop(),
|
||||
n--;
|
||||
s.val(0),
|
||||
r.push(t),
|
||||
o.val(JSON.stringify(r)),
|
||||
a.refreshEditor(
|
||||
i,
|
||||
e,
|
||||
function () {
|
||||
a.setDisabled(c('#image-undo-' + i), !0),
|
||||
a.setDisabled(c('#image-redo-' + i), !1)
|
||||
}
|
||||
)
|
||||
},
|
||||
rotate: function (t, i, e, a) {
|
||||
if (c(a).hasClass('disabled')) return !1;
|
||||
this.closePopup(a),
|
||||
this.addStep({
|
||||
r: {
|
||||
r: t,
|
||||
fw: this.hold.h,
|
||||
fh: this.hold.w
|
||||
}
|
||||
}, i, e)
|
||||
},
|
||||
flip: function (t, i, e, a) {
|
||||
if (c(a).hasClass('disabled')) return !1;
|
||||
this.closePopup(a),
|
||||
this.addStep({
|
||||
f: {
|
||||
f: t,
|
||||
fw: this.hold.w,
|
||||
fh: this.hold.h
|
||||
}
|
||||
}, i, e)
|
||||
},
|
||||
crop: function (t, i, e) {
|
||||
var a = c('#imgedit-selection-' + t).val(),
|
||||
o = this.intval(c('#imgedit-sel-width-' + t).val()),
|
||||
r = this.intval(c('#imgedit-sel-height-' + t).val());
|
||||
if (c(e).hasClass('disabled') || '' === a) return !1;
|
||||
0 < (a = JSON.parse(a)).w &&
|
||||
0 < a.h &&
|
||||
0 < o &&
|
||||
0 < r &&
|
||||
(a.fw = o, a.fh = r, this.addStep({
|
||||
c: a
|
||||
}, t, i)),
|
||||
c('#imgedit-sel-width-' + t).val(''),
|
||||
c('#imgedit-sel-height-' + t).val(''),
|
||||
c('#imgedit-start-x-' + t).val('0'),
|
||||
c('#imgedit-start-y-' + t).val('0')
|
||||
},
|
||||
undo: function (i, t) {
|
||||
var e = this,
|
||||
a = c('#image-undo-' + i),
|
||||
o = c('#imgedit-undone-' + i),
|
||||
r = e.intval(o.val()) + 1;
|
||||
a.hasClass('disabled') ||
|
||||
(
|
||||
o.val(r),
|
||||
e.refreshEditor(
|
||||
i,
|
||||
t,
|
||||
function () {
|
||||
var t = c('#imgedit-history-' + i),
|
||||
t = '' !== t.val() ? JSON.parse(t.val()) : [];
|
||||
e.setDisabled(c('#image-redo-' + i), !0),
|
||||
e.setDisabled(a, r < t.length),
|
||||
t.length === r &&
|
||||
c('#image-redo-' + i).trigger('focus')
|
||||
}
|
||||
)
|
||||
)
|
||||
},
|
||||
redo: function (t, i) {
|
||||
var e = this,
|
||||
a = c('#image-redo-' + t),
|
||||
o = c('#imgedit-undone-' + t),
|
||||
r = e.intval(o.val()) - 1;
|
||||
a.hasClass('disabled') ||
|
||||
(
|
||||
o.val(r),
|
||||
e.refreshEditor(
|
||||
t,
|
||||
i,
|
||||
function () {
|
||||
e.setDisabled(c('#image-undo-' + t), !0),
|
||||
e.setDisabled(a, 0 < r),
|
||||
0 == r &&
|
||||
c('#image-undo-' + t).trigger('focus')
|
||||
}
|
||||
)
|
||||
)
|
||||
},
|
||||
setNumSelection: function (t, i) {
|
||||
var e = c('#imgedit-sel-width-' + t),
|
||||
a = c('#imgedit-sel-height-' + t),
|
||||
o = c('#imgedit-start-x-' + t),
|
||||
r = c('#imgedit-start-y-' + t),
|
||||
o = this.intval(o.val()),
|
||||
r = this.intval(r.val()),
|
||||
s = this.intval(e.val()),
|
||||
n = this.intval(a.val()),
|
||||
d = c('#image-preview-' + t),
|
||||
l = d.height(),
|
||||
d = d.width(),
|
||||
h = this.hold.sizer,
|
||||
g = this.iasapi;
|
||||
if (!1 !== this.validateNumeric(i)) return s < 1 ? (e.val(''), !1) : n < 1 ? (a.val(''), !1) : void (
|
||||
(s && n || o && r) &&
|
||||
(i = g.getSelection()) &&
|
||||
(
|
||||
s = i.x1 + Math.round(s * h),
|
||||
n = i.y1 + Math.round(n * h),
|
||||
o = o === i.x1 ? i.x1 : Math.round(o * h),
|
||||
i = r === i.y1 ? i.y1 : Math.round(r * h),
|
||||
d < s &&
|
||||
(o = 0, s = d, e.val(Math.round(s / h))),
|
||||
l < n &&
|
||||
(i = 0, n = l, a.val(Math.round(n / h))),
|
||||
g.setSelection(o, i, s, n),
|
||||
g.update(),
|
||||
this.setCropSelection(t, g.getSelection())
|
||||
)
|
||||
)
|
||||
},
|
||||
round: function (t) {
|
||||
var i;
|
||||
return t = Math.round(t),
|
||||
0.6 < this.hold.sizer ? t : '1' === (i = t.toString().slice( - 1)) ? t - 1 : '9' === i ? t + 1 : t
|
||||
},
|
||||
setRatioSelection: function (t, i, e) {
|
||||
var a = this.intval(c('#imgedit-crop-width-' + t).val()),
|
||||
o = this.intval(c('#imgedit-crop-height-' + t).val()),
|
||||
r = c('#image-preview-' + t).height();
|
||||
!1 === this.validateNumeric(e) ? this.iasapi.setOptions({
|
||||
aspectRatio: null
|
||||
}) : a &&
|
||||
o &&
|
||||
(
|
||||
this.iasapi.setOptions({
|
||||
aspectRatio: a + ':' + o
|
||||
}),
|
||||
e = this.iasapi.getSelection(!0)
|
||||
) &&
|
||||
(
|
||||
r < (a = Math.ceil(e.y1 + (e.x2 - e.x1) / (a / o))) ? (
|
||||
a = r,
|
||||
o = n(
|
||||
'Selected crop ratio exceeds the boundaries of the image. Try a different ratio.'
|
||||
),
|
||||
c('#imgedit-crop-' + t).prepend(
|
||||
'<div class="notice notice-error" tabindex="-1" role="alert"><p>' + o + '</p></div>'
|
||||
),
|
||||
wp.a11y.speak(o, 'assertive'),
|
||||
c(i ? '#imgedit-crop-height-' + t : '#imgedit-crop-width-' + t).val('')
|
||||
) : void 0 !== (r = c('#imgedit-crop-' + t).find('.notice-error')) &&
|
||||
r.remove(),
|
||||
this.iasapi.setSelection(e.x1, e.y1, e.x2, a),
|
||||
this.iasapi.update()
|
||||
)
|
||||
},
|
||||
validateNumeric: function (t) {
|
||||
if (!1 === this.intval(c(t).val())) return c(t).val(''),
|
||||
!1
|
||||
}
|
||||
}
|
||||
}(jQuery);
|
||||
|
||||
|
||||
*/
|
||||
4
plugins/cipf_plugin/js/menu/example_menu.js
Normal file
4
plugins/cipf_plugin/js/menu/example_menu.js
Normal file
@@ -0,0 +1,4 @@
|
||||
|
||||
import { test_fetch } from './example_submenu.js';
|
||||
|
||||
test_fetch();
|
||||
29
plugins/cipf_plugin/js/menu/example_menu_2.js
Normal file
29
plugins/cipf_plugin/js/menu/example_menu_2.js
Normal file
@@ -0,0 +1,29 @@
|
||||
|
||||
const inputElement2 = document.getElementById('mytext_2');
|
||||
const sendButton2 = document.getElementById('mybutton_2');
|
||||
|
||||
sendButton2.addEventListener('click', () => {
|
||||
let inputValue = inputElement2.value;
|
||||
inputValue = {
|
||||
key1: 'value1',
|
||||
key2: 'value2'
|
||||
};
|
||||
inputValue = JSON.stringify(inputValue);
|
||||
console.log("inputValue:");
|
||||
console.log(inputValue);
|
||||
PLGNTLS_fetch('/plgntls/get_data', {
|
||||
method: "POST",
|
||||
})
|
||||
//PLGNTLS_fetch('get_data', {body: {inputValue}})
|
||||
.then((response) => response.json())
|
||||
.then((data) => {
|
||||
console.log("dataaa: ");
|
||||
console.log(data);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log("error: ");
|
||||
console.log(error);
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
33
plugins/cipf_plugin/js/menu/example_submenu.js
Normal file
33
plugins/cipf_plugin/js/menu/example_submenu.js
Normal file
@@ -0,0 +1,33 @@
|
||||
|
||||
import { PLGNTLS_fetch } from '../../utils/plgntls_fetch.js';
|
||||
|
||||
export function test_fetch() {
|
||||
const inputElement = document.getElementById('mytext');
|
||||
const sendButton = document.getElementById('mybutton');
|
||||
|
||||
sendButton.addEventListener('click', () => {
|
||||
let inputValue = inputElement.value;
|
||||
inputValue = {
|
||||
key1: 'value1',
|
||||
key2: 'value2'
|
||||
};
|
||||
inputValue = JSON.stringify(inputValue);
|
||||
console.log("inputValue:");
|
||||
console.log(inputValue);
|
||||
PLGNTLS_fetch('/plgntls/get_data', {
|
||||
method: "POST",
|
||||
})
|
||||
//PLGNTLS_fetch('get_data', {body: {inputValue}})
|
||||
.then((response) => response.json())
|
||||
.then((data) => {
|
||||
console.log("dataaa: ");
|
||||
console.log(data);
|
||||
})
|
||||
.catch((error) => {
|
||||
console.log("error: ");
|
||||
console.log(error);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
65
plugins/cipf_plugin/js/paypal/create_order.js
Normal file
65
plugins/cipf_plugin/js/paypal/create_order.js
Normal file
@@ -0,0 +1,65 @@
|
||||
|
||||
import { resultMessage } from './result_message.js';
|
||||
import { PLGNTLS_fetch } from '../../utils/plgntls_fetch.js';
|
||||
|
||||
/**
|
||||
* @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend
|
||||
*/
|
||||
//async function createOrder() {
|
||||
export async function createOrder() {
|
||||
try {
|
||||
//const fetch_create_url = PLGNTLS_data.fetch_url + "/cipf_plugin/api/v1/orders";
|
||||
//console.log("fetch_create_url:", fetch_create_url);
|
||||
//const response = await fetch(fetch_create_url, {
|
||||
const response = await PLGNTLS_fetch('/cipf_plugin/api/v1/orders', {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
//// use the "body" param to optionally pass additional order information
|
||||
//// like product ids and quantities
|
||||
//body: JSON.stringify({
|
||||
// cart: [
|
||||
// {
|
||||
// id: "1234",
|
||||
// quantity: "1",
|
||||
// },
|
||||
// ],
|
||||
//}),
|
||||
});
|
||||
|
||||
const orderData = await response.json();
|
||||
|
||||
if (orderData.id) {
|
||||
return orderData.id;
|
||||
} else {
|
||||
const errorDetail = orderData?.details?.[0];
|
||||
const errorMessage = errorDetail
|
||||
? `${errorDetail.issue} ${errorDetail.description} (${orderData.debug_id})`
|
||||
: JSON.stringify(orderData);
|
||||
|
||||
throw new Error(errorMessage);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
resultMessage(`Could not initiate PayPal Checkout...<br><br>${error}`);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see https://developer.paypal.com/demo/checkout/#/pattern/server
|
||||
*
|
||||
// Call your server to set up the transaction
|
||||
function createOrder(data, actions) {
|
||||
const fetch_create_url = PLGNTLS_data.fetch_url + "/cipf_plugin/api/v1/orders";
|
||||
console.log("fetch_create_url:", fetch_create_url);
|
||||
return fetch(fetch_create_url, {
|
||||
method: 'post'
|
||||
}).then(function(res) {
|
||||
return res.json();
|
||||
}).then(function(orderData) {
|
||||
return orderData.id;
|
||||
});
|
||||
},
|
||||
*/
|
||||
|
||||
114
plugins/cipf_plugin/js/paypal/on_approve.js
Normal file
114
plugins/cipf_plugin/js/paypal/on_approve.js
Normal file
@@ -0,0 +1,114 @@
|
||||
|
||||
import { resultMessage } from './result_message.js';
|
||||
import { PLGNTLS_fetch } from '../../utils/plgntls_fetch.js';
|
||||
|
||||
/**
|
||||
* @see https://developer.paypal.com/docs/checkout/standard/integrate/#link-integratebackend
|
||||
*/
|
||||
//async function onApprove(data, actions) {
|
||||
export async function onApprove(data, actions) {
|
||||
try {
|
||||
const fetch_approve_url = PLGNTLS_data.fetch_url + "/cipf_plugin/api/v1/orders/" + data.orderID + "/capture";
|
||||
console.log("fetch_approve_url:", fetch_approve_url);
|
||||
//const response = await fetch(fetch_approve_url, {
|
||||
const response = await PLGNTLS_fetch('/cipf_plugin/api/v1/orders/' + data.orderID + '/capture', {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
//"X-WP-Nonce": PLGNTLS_data.rest_nonce,
|
||||
},
|
||||
});
|
||||
|
||||
const orderData = await response.json();
|
||||
// Three cases to handle:
|
||||
// (1) Recoverable INSTRUMENT_DECLINED -> call actions.restart()
|
||||
// (2) Other non-recoverable errors -> Show a failure message
|
||||
// (3) Successful transaction -> Show confirmation or thank you message
|
||||
|
||||
const errorDetail = orderData?.details?.[0];
|
||||
|
||||
if (errorDetail?.issue === "INSTRUMENT_DECLINED") {
|
||||
// (1) Recoverable INSTRUMENT_DECLINED -> call actions.restart()
|
||||
// recoverable state, per https://developer.paypal.com/docs/checkout/standard/customize/handle-funding-failures/
|
||||
return actions.restart();
|
||||
} else if (errorDetail) {
|
||||
// (2) Other non-recoverable errors -> Show a failure message
|
||||
console.log("errorDetail:");
|
||||
console.log(errorDetail);
|
||||
throw new Error(`${errorDetail.description} (${orderData.debug_id})`);
|
||||
} else if (!orderData.purchase_units) {
|
||||
throw new Error(JSON.stringify(orderData));
|
||||
} else {
|
||||
try {
|
||||
// (3) Successful transaction -> Show confirmation or thank you message
|
||||
// Or go to another URL: actions.redirect('thank_you.html');
|
||||
const transaction =
|
||||
orderData?.purchase_units?.[0]?.payments?.captures?.[0] ||
|
||||
orderData?.purchase_units?.[0]?.payments?.authorizations?.[0];
|
||||
// to show a message on page
|
||||
//resultMessage(`Transaction ${transaction.status}: ${transaction.id}<br><br>See console for all available details`);
|
||||
resultMessage(eval(PLGNTLS_data.paypal_message_success));
|
||||
console.log(
|
||||
"Capture result",
|
||||
orderData,
|
||||
JSON.stringify(orderData, null, 2),
|
||||
);
|
||||
actions.redirect(PLGNTLS_data.paypal_redirection_success);
|
||||
} catch (error) {
|
||||
console.error("payment ok but error on traitment afterwards : ", error);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(error);
|
||||
//resultMessage(`Sorry, your transaction could not be processed...<br><br>${error}`);
|
||||
resultMessage(eval(PLGNTLS_data.paypal_message_failure));
|
||||
//actions.redirect(PLGNTLS_data.paypal_redirection_failure);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @see https://developer.paypal.com/demo/checkout/#/pattern/server
|
||||
*
|
||||
// Call your server to finalize the transaction
|
||||
function onApprove (data, actions) {
|
||||
const fetch_approve_url = PLGNTLS_data.fetch_url + "/cipf_plugin/api/v1/orders/" + data.orderID + "/capture";
|
||||
console.log("fetch_approve_url:", fetch_approve_url);
|
||||
return fetch(fetch_approve_url, {
|
||||
method: 'post'
|
||||
}).then(function(res) {
|
||||
return res.json();
|
||||
}).then(function(orderData) {
|
||||
// Three cases to handle:
|
||||
// (1) Recoverable INSTRUMENT_DECLINED -> call actions.restart()
|
||||
// (2) Other non-recoverable errors -> Show a failure message
|
||||
// (3) Successful transaction -> Show confirmation or thank you
|
||||
|
||||
// This example reads a v2/checkout/orders capture response, propagated from the server
|
||||
// You could use a different API or structure for your 'orderData'
|
||||
var errorDetail = Array.isArray(orderData.details) && orderData.details[0];
|
||||
|
||||
if (errorDetail && errorDetail.issue === 'INSTRUMENT_DECLINED') {
|
||||
return actions.restart(); // Recoverable state, per:
|
||||
// https://developer.paypal.com/docs/checkout/integration-features/funding-failure/
|
||||
}
|
||||
|
||||
if (errorDetail) {
|
||||
var msg = 'Sorry, your transaction could not be processed.';
|
||||
if (errorDetail.description) msg += '\n\n' + errorDetail.description;
|
||||
if (orderData.debug_id) msg += ' (' + orderData.debug_id + ')';
|
||||
return alert(msg); // Show a failure message (try to avoid alerts in production environments)
|
||||
}
|
||||
|
||||
// Successful capture! For demo purposes:
|
||||
console.log('Capture result', orderData, JSON.stringify(orderData, null, 2));
|
||||
var transaction = orderData.purchase_units[0].payments.captures[0];
|
||||
alert('Transaction '+ transaction.status + ': ' + transaction.id + '\n\nSee console for all available details');
|
||||
|
||||
// Replace the above to show a success message within this page, e.g.
|
||||
// const element = document.getElementById('paypal-button-container');
|
||||
// element.innerHTML = '';
|
||||
// element.innerHTML = '<h3>Thank you for your payment!</h3>';
|
||||
// Or go to another URL: actions.redirect('thank_you.html');
|
||||
});
|
||||
}
|
||||
*/
|
||||
56
plugins/cipf_plugin/js/paypal/paypal.js
Normal file
56
plugins/cipf_plugin/js/paypal/paypal.js
Normal file
@@ -0,0 +1,56 @@
|
||||
import { createOrder } from './create_order.js';
|
||||
import { onApprove } from './on_approve.js';
|
||||
|
||||
|
||||
window.paypal.Buttons({
|
||||
/*
|
||||
style: {
|
||||
layout: 'vertical',
|
||||
color: 'blue',
|
||||
shape: 'rect',
|
||||
label: 'paypal',
|
||||
},
|
||||
*/
|
||||
createOrder: createOrder,
|
||||
onApprove: onApprove,
|
||||
})
|
||||
.render("#paypal-button-container");
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* customize card fields
|
||||
* from : https://developer.paypal.com/docs/checkout/advanced/integrate#link-addpaypalbuttonsandcardfields
|
||||
*
|
||||
// Create the Card Fields Component and define callbacks
|
||||
const cardField = paypal.CardFields({
|
||||
createOrder: createOrder,
|
||||
onApprove: onApprove,
|
||||
});
|
||||
|
||||
// Render each field after checking for eligibility
|
||||
if (cardField.isEligible()) {
|
||||
const nameField = cardField.NameField();
|
||||
nameField.render("#card-name-field-container");
|
||||
|
||||
const numberField = cardField.NumberField();
|
||||
numberField.render("#card-number-field-container");
|
||||
|
||||
const cvvField = cardField.CVVField();
|
||||
cvvField.render("#card-cvv-field-container");
|
||||
|
||||
const expiryField = cardField.ExpiryField();
|
||||
expiryField.render("#card-expiry-field-container");
|
||||
|
||||
// Add click listener to submit button and call the submit function on the CardField component
|
||||
document
|
||||
.getElementById("card-field-submit-button")
|
||||
.addEventListener("click", () => {
|
||||
cardField.submit().then(() => {
|
||||
// submit successful
|
||||
});
|
||||
});
|
||||
}
|
||||
*/
|
||||
|
||||
|
||||
13
plugins/cipf_plugin/js/paypal/plgntls_fetch.js
Normal file
13
plugins/cipf_plugin/js/paypal/plgntls_fetch.js
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
//function PLGNTLS_fetch(url, options = {}) {
|
||||
export function PLGNTLS_fetch(url, options = {}) {
|
||||
console.log("inside PLGNTLS_fetch");
|
||||
url = PLGNTLS_data.fetch_url + url;
|
||||
|
||||
options.headers = options.headers || {};
|
||||
options.headers['X-WP-Nonce'] = PLGNTLS_data.fetch_nonce,
|
||||
console.log("options:", options);
|
||||
|
||||
return fetch(url, options);
|
||||
}
|
||||
|
||||
9
plugins/cipf_plugin/js/paypal/result_message.js
Normal file
9
plugins/cipf_plugin/js/paypal/result_message.js
Normal file
@@ -0,0 +1,9 @@
|
||||
|
||||
// Example function to show a result to the user. Your site's UI library can be used instead.
|
||||
//function resultMessage(message) {
|
||||
export function resultMessage(message) {
|
||||
const buttons = document.querySelector("#paypal-button-container");
|
||||
const container = document.querySelector("#result-message");
|
||||
buttons.style.display = 'none';
|
||||
container.innerHTML = message;
|
||||
}
|
||||
Reference in New Issue
Block a user