Skip to content

Instantly share code, notes, and snippets.

@shelling
Last active February 13, 2022 23:08
Show Gist options
  • Save shelling/76d05e5f7ac5d1ff5fbe1b3c08ef6696 to your computer and use it in GitHub Desktop.
Save shelling/76d05e5f7ac5d1ff5fbe1b3c08ef6696 to your computer and use it in GitHub Desktop.
var Func = function(e) {
"use strict";
var a, t = $("#ECPayPayment"),
n = e.sdkVersion,
c = e.token,
i = e.language,
d = e.cliPriKey,
r = e.serPubKey,
s = e.keyID,
p = e.uuid,
l = !1;
function o(e) {
var a = $(".ecpay-cc-card", e),
t = a.parent("dd").find(".ecpay-card-status-line > .ecpay-red");
l ? t.css("visibility", "hidden") : e.hasClass("ecpay-edit-box") ? t.css("visibility", "hidden") : e.hasClass("ecpay-add-box") && ("" == a.val().trim() ? t.text($(".ecpay-CardInputCardNumber").text()).css("visibility", "visible") : t.text($(".ecpay-CardEnterAgain").text()).css("visibility", "visible"));
var n = $(".ecpay-date", e),
c = n.parent("dd").find(".ecpay-card-status-line > .ecpay-red"),
i = n.val().replace(/\s*/g, "");
if (/^(0[1-9]|1[0-2])(\/)(\d{2})$/g.test(i)) {
var d = parseInt(i.split("/")[1]),
r = parseInt(i.split("/")[0]),
s = new Date,
p = parseInt(s.getFullYear().toString().substr(2, 2)),
o = s.getMonth() + 1;
d > p || d == p && r >= o ? c.hide() : c.show()
} else c.show();
var y = $(".ecpay-cc-cvc", e),
f = y.parent("dd").find(".ecpay-card-status-line > .ecpay-red");
return (4 == y.attr("maxlength") ? /^\d{4}$/ : /^\d{3}$/).test(y.val()) ? f.hide() : f.show(), "visible" != t.css("visibility") && "block" != c.css("display") && "block" != f.css("display")
}
function y(e) {
var a = r.encrypt(e);
return forge.util.encode64(a)
}
function f(e) {
var a = forge.md.sha256.create();
return a.update(e, "utf8"), forge.util.encode64(d.sign(a))
}
function u(e, a, t) {
var n = forge.cipher.createCipher("AES-CBC", a);
return n.start({
iv: t
}), n.update(forge.util.createBuffer(e)), n.finish(), forge.util.encode64(n.output.data)
}
function g(e, t, c, l, o) {
$.ajax({
url: a + e,
type: "post",
beforeSend: function(e) {
e.setRequestHeader("Language", i), e.setRequestHeader("UUID", p), e.setRequestHeader("AppName", location.host), e.setRequestHeader("AppInfo", function() {
var e = navigator.userAgent,
a = "",
t = {
ios: !!e.match(/\(i[^;] ;( U;)? CPU. Mac OS X/) || e.indexOf("iPhone") > -1 || e.indexOf("Mac") > -1 || e.indexOf("iPad") > -1,
android: e.indexOf("Android") > -1 || e.indexOf("Adr") > -1
};
a = e.indexOf("Mac OS X") > -1 ? (e.match(/OS X [\d._]*/gi) + "").replace(/[^0-9|_.]/gi, "").replace(/_/gi, ".") : e.indexOf("Android") > -1 || e.indexOf("Linux") > -1 ? e.substr(e.indexOf("Android") + 8, e.indexOf(";", e.indexOf("Android")) - e.indexOf("Android") - 8) : "Unknown";
var c = {
DeviceInfo: e,
IsSimulator: 0,
OS: t.ios ? 1 : t.android ? 2 : 0,
OSVersion: "" == a ? "Unknown" : a,
Vers: n
};
return JSON.stringify(c)
}()), e.setRequestHeader("KeyInfo", c), e.setRequestHeader("Signature", l), e.setRequestHeader("KeyID", s)
},
cache: !1,
contentType: "application/json",
data: JSON.stringify(t),
dataType: "json",
success: function(e, a, t) {
var n = t.getResponseHeader("KeyInfo"),
c = t.getResponseHeader("Signature");
if (null != c)
if (function(e, a) {
var t = forge.md.sha256.create();
return t.update(e, "utf8"), r.verify(t.digest().getBytes(), forge.util.decode64(a))
}(JSON.stringify(e), c))
if (1 == e.TransCode)
if (null != n) {
var i, s, p, l = (i = n, s = d.decrypt(forge.util.decode64(i)), p = forge.util.decodeUtf8(s), JSON.parse(p));
if (null != l) {
var y = function(e, a, t) {
var n = forge.cipher.createDecipher("AES-CBC", forge.util.encodeUtf8(a));
n.start({
iv: forge.util.encodeUtf8(t)
}), n.update(forge.util.createBuffer(forge.util.decode64(e))), n.finish();
var c = n.output.data;
return JSON.parse(decodeURIComponent(c))
}(e.Data, l.key, l.iv);
null != y ? o(y, null) : o(null, "The parameter [Data] cannot be empty")
} else o(null, "The parameter [KeyInfo] decrypt fail")
} else o(null, "The parameter [KeyInfo] cannot be empty");
else o(null, e.TransMsg);
else o(null, "Signature verify error");
else o(null, "The parameter [Signature] cannot be empty")
},
error: function(e, a, t) {
o(null, e.statusText)
}
})
}
function v(e, n) {
var d = a + "/Payment/BindResult?type=" + e + "&token=" + c + "&lang=" + i;
d = d + (null == n || 1 != n.RtnCode ? "&status=fail" : "&status=success") + "&rtnMsg=" + encodeURIComponent(n.RtnMsg), t.load(d)
}
this.Init = function(e) {
var n;
a = e, $(".ecpay-card-list > .ecpay-c-l-features > .ecpay-icon-ic_edit", t).bind("click", function() {
$(".ecpay-edit-box", t).hide(), $(".ecpay-add-box", t).find(".ecpay-card-info-tab,.ecpay-btn-box").hide().end().find(".ecpay-btnline").show(), $(".ecpay-card-list", t).removeClass("ecpay-dissable");
var e = $(this).closest(".ecpay-card-list").next(".ecpay-edit-box");
e.prev("ul").addClass("ecpay-dissable"), e.find(".ecpay-card-status-line > .ecpay-red").hide(), e.find(".ecpay-cc-card").parent("dd").find(".ecpay-card-status-line > .ecpay-red").css("display", "");
var a = e.find(".ecpay-cc-card").val().replace(/\s*/g, ""),
n = "",
c = 3;
15 == a.length ? (n = a.replace(/^(\d{4})(\d{2}\*{4})(\*\d{4})$/g, "$1 $2 $3").trim(), c = 4) : n = a.length >= 16 ? a.replace(/^(\d{4})(\d{2}\*{2})(\*{4})(\d{4})$/g, "$1 $2 $3 $4").trim() : a.replace(/^(\d{6})(\*{3,9}\d{4})$/g, "$1 $2").trim(), e.find(".ecpay-cc-card").val(n), e.find(".ecpay-date").val(e.find(".ecpay-date").prev("span").text()), e.find(".ecpay-cc-cvc").val(""), e.find(".ecpay-cc-cvc").attr("maxlength", c).attr("pattern", "^\\d{" + c + "}$"), e.show(), t.find(".ecpay-edit-box:visible .ecpay-cc-card").trigger("keyup")
}), $(".ecpay-card-list > .ecpay-c-l-features > .ecpay-icon-ic_del", t).bind("click", function() {
var e = $(this).closest(".ecpay-card-list");
if (!confirm($(".ecpay-UnbindConfirm").text())) return !1;
var a = e.next(".ecpay-edit-box").find(".ecpay-cc-card").prev("span").text(),
t = {
PayToken: a
},
n = forge.util.encode64(forge.random.getBytesSync(32)).substr(0, 32),
c = forge.util.encode64(forge.random.getBytesSync(16)).substr(0, 16),
i = {
Data: u(JSON.stringify(t), n, c)
},
d = y(JSON.stringify({
key: n,
iv: c
})),
r = f(JSON.stringify(i));
! function(e, a, t, n) {
g("/Payment/DeleteBindCard", e, a, t, n)
}(i, d, r, function(a, t) {
null != a && 1 == a.RtnCode || v("del", a), e.next(".ecpay-edit-box").remove().end().remove()
})
}), $(".ecpay-edit-box > .ecpay-btn-box > .ecpay-Cancel", t).bind("click", function() {
var e = $(this).closest(".ecpay-edit-box");
e.hide(), e.prev("ul").removeClass("ecpay-dissable")
}), $(".ecpay-edit-box > .ecpay-btn-box > .ecpay-Modified", t).bind("click", function() {
var e = $(this).closest(".ecpay-edit-box");
if (!o(e)) return !1;
var a = e.find(".ecpay-cc-card").prev("span").text(),
t = e.find(".ecpay-date").val().replace(/\s*/g, "").split("/"),
n = e.find(".ecpay-cc-cvc").val(),
c = {
CardInfo: {
PayToken: a,
CardValidYY: t[1],
CardValidMM: t[0],
CardCVV2: n
}
},
i = forge.util.encode64(forge.random.getBytesSync(32)).substr(0, 32),
d = forge.util.encode64(forge.random.getBytesSync(16)).substr(0, 16),
r = {
Data: u(JSON.stringify(c), i, d)
},
s = y(JSON.stringify({
key: i,
iv: d
})),
p = f(JSON.stringify(r));
! function(e, a, t, n) {
g("/Payment/EditBindCard", e, a, t, n)
}(r, s, p, function(e, a) {
v("edit", e)
})
}), $(".ecpay-add-box > .ecpay-btnline > .ecpay-icon-ic_add_card", t).bind("click", function() {
$(".ecpay-edit-box", t).hide(), $(".ecpay-card-list", t).removeClass("ecpay-dissable");
var e = $(this).closest(".ecpay-add-box");
e.find(".ecpay-btnline").hide(), e.find(".ecpay-card-status-line > .ecpay-red").hide(), e.find(".ecpay-cc-card").parent("dd").find(".ecpay-card-status-line > .ecpay-red").css("display", "").css("visibility", "hidden"), e.find(".ecpay-cc-card").parent("dd").find(".ecpay-card-status-line > .ecpay-card-detail").html("").append("<i></i>"), e.find(".ecpay-cc-card,.ecpay-date,.ecpay-cc-cvc").val(""), e.find(".ecpay-card-info-tab,.ecpay-btn-box").show()
}), $(".ecpay-add-box > .ecpay-btn-box > .ecpay-Cancel", t).bind("click", function() {
var e = $(this).closest(".ecpay-add-box");
e.find(".ecpay-card-info-tab,.ecpay-btn-box").hide(), e.find(".ecpay-btnline").show()
}), $(".ecpay-add-box > .ecpay-btn-box > .ecpay-Add", t).bind("click", function() {
var e = $(this).closest(".ecpay-add-box");
if (void 0 === c) return !1;
if (!o(e)) return !1;
var a = e.find(".ecpay-cc-card").val().replace(/\s*/g, ""),
t = e.find(".ecpay-date").val().replace(/\s*/g, "").split("/"),
n = e.find(".ecpay-cc-cvc").val(),
i = {
Token: c,
CardInfo: {
CardNumber: a,
CardValidYY: t[1],
CardValidMM: t[0],
CardCVV2: n,
BindingCard: 1
}
},
d = forge.util.encode64(forge.random.getBytesSync(32)).substr(0, 32),
r = forge.util.encode64(forge.random.getBytesSync(16)).substr(0, 16),
s = {
Data: u(JSON.stringify(i), d, r)
},
p = y(JSON.stringify({
key: d,
iv: r
})),
l = f(JSON.stringify(s));
! function(e, a, t, n) {
g("/Payment/BindCard", e, a, t, n)
}(s, p, l, function(e, a) {
v("add", e)
})
}), n = "", $(".ecpay-cc-card", t).bind("keyup keydown blur", function() {
var e = $(this),
c = e.closest(".ecpay-card-info-tab"),
i = e.val().replace(/\s*/g, "").replace(/\-*/g, "");
if (i != n) {
n = i, e.parent("dd").find(".ecpay-card-status-line > .ecpay-red").css("display", "").css("visibility", "hidden");
var d = 15,
r = "";
if (RegExp(/^\d*$/).test(i)) {
i.length >= 4 && (RegExp(/^4\d{3}$/).test(i.substr(0, 4)) ? (d = 16, r = "visa", i = i.substr(0, d), e.attr("maxlength", d + 3), e.val(i.replace(/(\d{4})(?=\d)/g, "$1 ").trim()), c.find(".ecpay-cc-cvc").attr("maxlength", 3).attr("pattern", "^\\d{3}$")) : RegExp(/^((5[1-5]\d{2})|(2(22[1-9]|[3-6]\d{2}|7[01]\d|720)))$/).test(i.substr(0, 4)) ? (d = 16, r = "master", i = i.substr(0, d), e.attr("maxlength", d + 3), e.val(i.replace(/(\d{4})(?=\d)/g, "$1 ").trim()), c.find(".ecpay-cc-cvc").attr("maxlength", 3).attr("pattern", "^\\d{3}$")) : RegExp(/^((35(2[89]|[3-8]\d))|1800|2131)$/).test(i.substr(0, 4)) ? (d = 16, r = "jcb", i = i.substr(0, d), e.attr("maxlength", d + 3), e.val(i.replace(/(\d{4})(?=\d)/g, "$1 ").trim()), c.find(".ecpay-cc-cvc").attr("maxlength", 3).attr("pattern", "^\\d{3}$")) : RegExp(/^(3[47])\d{2}$/).test(i.substr(0, 4)) ? (d = 15, r = "aecard", i = i.substr(0, d), e.attr("maxlength", d + 2), e.val(i.replace(/^(\d{4})(\d{0,6})(\d{0,5})$/g, "$1 $2 $3").trim()), c.find(".ecpay-cc-cvc").attr("maxlength", 4).attr("pattern", "^\\d{4}$")) : RegExp(/^62\d{2}$/).test(i.substr(0, 4)) ? (d = 19, r = "upay", i = i.substr(0, d), e.attr("maxlength", d + 4), e.val(i.replace(/(\d{4})(?=\d)/g, "$1 ").trim()), c.find(".ecpay-cc-cvc").attr("maxlength", 3).attr("pattern", "^\\d{3}$")) : (d = 16, i = i.substr(0, d), e.attr("maxlength", d + 3), e.val(i.replace(/(\d{4})(?=\d)/g, "$1 ").trim()), c.find(".ecpay-cc-cvc").attr("maxlength", 3).attr("pattern", "^\\d{3}$")));
var s = e.parent("dd").find(".ecpay-card-status-line > .ecpay-card-detail"),
p = c.find(".ecpay-pl-icon-type span");
"upay" != r && i.length < d || "upay" == r && i.length < 16 ? (s.empty(), l = !1) : t.find(".ecpay-pl-icon-type span").length > 0 && (19 == d ? i.length >= 16 : i.length == d) ? (function(e, t) {
$.ajax({
url: a + "/Payment/GetCardKind",
data: {
CardNo: e.replace(/\s*/g, "")
},
type: "post",
dataType: "json",
success: function(e, a, n) {
t(e, null)
},
error: function(e, a, n) {
t(null, e.statusText)
}
})
}(i, function(e, a) {
if (null != e) {
l = !1;
var n = e.BINCodeInfo;
null != n ? (s.empty().append("<i></i>").children("i").addClass("ecpay-" + n.CardType), "" != n.IssuingBank && (s.children("i").after(n.IssuingBank), l = !0)) : "0" == t.find(".ecpay-ForeignCard").text() ? (s.empty().append("<i></i>").children("i").addClass("ecpay-" + r), s.children("i").after(c.find(".ecpay-CardNoAcceptMsg").text()), l = !1) : "1" == t.find(".ecpay-ForeignCard").text() && p.hasClass("ecpay-" + r) ? (s.empty().append("<i></i>").children("i").addClass("ecpay-" + r), l = !0) : (s.empty().append("<i></i>").children("i").addClass("ecpay-" + r), s.children("i").after(c.find(".ecpay-CardNoSupportMsg").text()), l = !1)
} else s.children("i").after(a)
}), c.find(".ecpay-cc-cvc").val("")) : (s.empty().append("<i></i>"), s.children("i").after(c.find(".ecpay-CardNoSupportMsg").text()), l = !1)
}
}
}), $(".ecpay-date", t).bind("keyup keydown blur", function() {
var e = $(this);
e.parent("dd").find(".ecpay-card-status-line > .ecpay-red").hide(), e.val(e.val().replace(/^(0[1-9]|1[0-2])(\/)(\d{2})$/g, "$1 $2 $3"))
}), $(".ecpay-cc-cvc", t).bind("keyup keydown blur", function() {
$(this).parent("dd").find(".ecpay-card-status-line > .ecpay-red").hide()
})
}
};
var Func = function() {
"use strict";
var e, a = $("#ECPayPayment"),
t = !1;
this.Init = function(a) {
e = a, n(), i(), c(), d()
}, this.ChkPayData = function() {
var e = !0,
n = $(".ecpay-pl-act", a),
i = n.attr("id");
if (void 0 === i) e = !1;
else if ($.inArray(i, ["CreditCard", "CreditInstallment", "Period", "Travel", "FlexibleInstallment"]) >= 0) {
var c = $(".ecpay-cc-card", n).parent("dd").find(".ecpay-card-status-line > .ecpay-red");
t ? c.css("visibility", "hidden") : c.css("visibility", "visible");
var d = $(".ecpay-date", n),
r = d.parent("dd").find(".ecpay-card-status-line > .ecpay-red"),
l = /^(0[1-9]|1[0-2])(\/)(\d{2})$/g,
s = d.val().replace(/\s*/g, "");
if (l.test(s)) {
var p = parseInt(s.split("/")[1]),
y = parseInt(s.split("/")[0]),
f = new Date,
o = parseInt(f.getFullYear().toString().substr(2, 2)),
u = f.getMonth() + 1;
p > o || p == o && y >= u ? r.hide() : r.show()
} else r.show();
var h = $(".ecpay-cc-cvc", n),
v = h.parent("dd").find(".ecpay-card-status-line > .ecpay-red");
(l = 4 == h.attr("maxlength") ? /^\d{4}$/ : /^\d{3}$/).test(h.val()) ? v.hide() : v.show(), 0 == n.find(".ecpay-CardList option:selected").val() && "visible" == c.css("visibility") ? e = !1 : n.find(".ecpay-cit-item-box .ecpay-cit-item:visible .ecpay-card-status-line > .ecpay-red:visible").each(function() {
return e = !1, !1
})
} else if ("ATM" == i) {
var g = n.find(".ecpay-card-status-line > .ecpay-red");
0 == n.find(".ecpay-ATMBank :selected").val() ? (g.show(), e = !1) : g.hide()
}
return e
}, this.CreatePayData = function(e) {
var t, n, i = $(".ecpay-pl-act", a),
c = i.attr("id");
return $.inArray(c, ["CreditCard", "CreditInstallment", "Period", "Travel", "FlexibleInstallment"]) >= 0 ? n = 0 == (t = i.find(".ecpay-CardList option:selected").val().split(",")[0]) ? r(e) : l(t) : (t = i.find(".ecpay-payToken").text(), n = l(t)), {
PayType: c,
PayToken: t,
Data: JSON.stringify(n)
}
};
var n = function() {
var e = "";
$(".ecpay-CardList", a).bind("change", function() {
var t = $(".ecpay-pl-act", a),
n = $(this);
t.find(".ecpay-card-status-line > .ecpay-red").hide(), t.find(".ecpay-cc-card").parent("dd").find(".ecpay-card-status-line > .ecpay-red").css("display", "").css("visibility", "hidden"), t.find(".ecpay-cc-card").parent("dd").find(".ecpay-card-status-line > .ecpay-card-detail").empty().append("<i></i>"), 0 == $(" > option:selected", n).val() ? (t.find(".ecpay-cc-card").parent().show(), t.find(".ecpay-date").val("").closest("dl").show(), t.find(".ecpay-cc-cvc").val("").closest("dl").css("width", ""), t.find(".ecpay-cc-cvc").attr("maxlength", 3).attr("pattern", "^\\d{3}$"), t.find(".ecpay-pay-check").show()) : (t.find(".ecpay-cc-card").parent().hide(), t.find(".ecpay-date").val("").closest("dl").hide(), t.find(".ecpay-cc-cvc").val("").closest("dl").css("width", "100%"), "975" == $(" > option:selected", n).val().split(",")[1] ? t.find(".ecpay-cc-cvc").attr("maxlength", 4).attr("pattern", "^\\d{4}$") : t.find(".ecpay-cc-cvc").attr("maxlength", 3).attr("pattern", "^\\d{3}$"), t.find(".ecpay-pay-check").hide()), t.find(".ecpay-cc-cvc").trigger("keyup"), e = ""
}), $(".ecpay-cc-card", a).bind("keyup keydown blur", function() {
var n = $(this),
i = $(".ecpay-pl-act", a),
c = n.val().replace(/\s*/g, "").replace(/\-*/g, "");
if (c != e) {
e = c, n.parent("dd").find(".ecpay-card-status-line > .ecpay-red").css("visibility", "hidden");
var d = 16,
r = "";
if (RegExp(/^\d*$/).test(c)) {
c.length > 4 && (RegExp(/^4\d{3}$/).test(c.substr(0, 4)) ? (d = 16, r = "visa", c = c.substr(0, d), n.attr("maxlength", d + 3), n.val(c.replace(/(\d{4})(?=\d)/g, "$1 ").trim()), i.find(".ecpay-cc-cvc").attr("maxlength", 3).attr("pattern", "^\\d{3}$")) : RegExp(/^((5[1-5]\d{2})|(2(22[1-9]|[3-6]\d{2}|7[01]\d|720)))$/).test(c.substr(0, 4)) ? (d = 16, r = "master", c = c.substr(0, d), n.attr("maxlength", d + 3), n.val(c.replace(/(\d{4})(?=\d)/g, "$1 ").trim()), i.find(".ecpay-cc-cvc").attr("maxlength", 3).attr("pattern", "^\\d{3}$")) : RegExp(/^((35(2[89]|[3-8]\d))|1800|2131)$/).test(c.substr(0, 4)) ? (d = 16, r = "jcb", c = c.substr(0, d), n.attr("maxlength", d + 3), n.val(c.replace(/(\d{4})(?=\d)/g, "$1 ").trim()), i.find(".ecpay-cc-cvc").attr("maxlength", 3).attr("pattern", "^\\d{3}$")) : RegExp(/^(3[47])\d{2}$/).test(c.substr(0, 4)) ? (d = 15, r = "aecard", c = c.substr(0, d), n.attr("maxlength", d + 2), n.val(c.replace(/^(\d{4})(\d{0,6})(\d{0,5})$/g, "$1 $2 $3").trim()), i.find(".ecpay-cc-cvc").attr("maxlength", 4).attr("pattern", "^\\d{4}$")) : RegExp(/^62\d{2}$/).test(c.substr(0, 4)) ? (d = 19, r = "upay", c = c.substr(0, d), n.attr("maxlength", d + 4), n.val(c.replace(/(\d{4})(?=\d)/g, "$1 ").trim()), i.find(".ecpay-cc-cvc").attr("maxlength", 3).attr("pattern", "^\\d{3}$")) : (d = 16, c = c.substr(0, d), n.attr("maxlength", d + 3), n.val(c.replace(/(\d{4})(?=\d)/g, "$1 ").trim()), i.find(".ecpay-cc-cvc").attr("maxlength", 3).attr("pattern", "^\\d{3}$")));
var l = i.find(".ecpay-pl-icon-type span");
(l.length > 0 && 19 == d ? c.length >= 16 : c.length == d) ? (s(c, function(e, c) {
var d = n.parent("dd").find(".ecpay-card-status-line > .ecpay-card-detail");
if (null != e) {
t = !1;
var s = e.BINCodeInfo;
null != s ? (d.empty().append("<i></i>").children("i").addClass("ecpay-" + s.CardType), "" != s.IssuingBank && (d.children("i").after(s.IssuingBank), t = !0)) : "0" == a.find(".ecpay-ForeignCard").text() ? (d.empty().append("<i></i>").children("i").addClass("ecpay-" + r), d.children("i").after(i.find(".ecpay-CardNoAcceptMsg").text()), t = !0) : "1" == a.find(".ecpay-ForeignCard").text() && l.hasClass("ecpay-" + r) ? (d.empty().append("<i></i>").children("i").addClass("ecpay-" + r), t = !0) : (d.empty().append("<i></i>").children("i").addClass("ecpay-" + r), d.children("i").after(i.find(".ecpay-CardNoSupportMsg").text()), t = !0)
} else d.children("i").after(c)
}), i.find(".ecpay-cc-cvc").val("")) : t = !1
}
}
}), $(".ecpay-date", a).bind("keyup keydown blur", function() {
var e = $(this);
e.parent("dd").find(".ecpay-card-status-line > .ecpay-red").hide(), e.val(e.val().replace(/^(0[1-9]|1[0-2])(\/)(\d{2})$/g, "$1 $2 $3"))
}), $(".ecpay-cc-cvc", a).bind("keyup keydown blur", function() {
$(this).parent("dd").find(".ecpay-card-status-line > .ecpay-red").hide()
}), $(".ecpay-Installment", a).bind("change", function() {
var e = $(".ecpay-pl-act", a),
t = $(this).find("option:selected"),
n = JSON.parse(e.find(".ecpay-SupportList").text());
if (null !== n) {
var i = n.filter(function(e) {
return e.Number == t.val()
}).map(function(e) {
return e.BankList
})[0];
t.closest("dl").find(".ecpay-card-info .ecpay-tp-txt").text(i.map(function(e) {
return e.Name
}).join("/"));
var c = $(".ecpay-CardList", e);
c.find("option").filter(function() {
return "0" != $(this).val()
}).each(function() {
var e = $(this);
i.map(function(e) {
return e.Code
}).filter(function(a) {
return 0 != e.length && a == e.val().split(",")[1]
}).length > 0 ? (e.show(), y() && e.parent().is("span") && e.unwrap()) : (e.hide(), y() && (e.parent().is("span") || e.wrap("<span>").parent().hide()))
});
var d = $(" > option", c).filter(function() {
return "block" == $(this).css("display") || "inline" == $(this).css("display")
});
d.first().attr("selected", !0), 1 == d.length && "0" == d.val() ? c.parent().parent().hide() : "0" != d.val() && e.find(".ecpay-cc-card").parent().hide()
}
})
},
i = function() {
$(".ecpay-ATMBank", a).bind("change", function() {
$(this).parent().parent().next().find(".ecpay-card-status-line > .ecpay-red").hide()
})
},
c = function() {
if (1 == a.find(".ecpay-pay-list").length) $(".ecpay-pl-intro", a).on("click", function() {
var e = $(this).parent("li");
if (e.hasClass("ecpay-pl-act") || e.addClass("ecpay-pl-act").siblings("li").removeClass("ecpay-pl-act"), !$(this).parent("li").hasClass("ecpay-pl-act")) return !1;
var t = $(".ecpay-pl-act", a),
n = t.attr("id");
t.find(".ecpay-card-status-line > .ecpay-red").hide();
var i = $(".ecpay-CardList", t);
"CreditInstallment" == n ? t.find(".ecpay-Installment").trigger("change") : (i.find("option:selected").is(":hidden") && i.find("option").attr("selected", !1).filter(function() {
return "block" == $(this).css("display") || "inline" == $(this).css("display")
}).first().attr("selected", !0), 1 == i.find("option").length && "0" == i.find(":selected").val() && i.parent().parent().hide()), i.trigger("change")
});
else {
var e = $(".ecpay-CardList", a);
e.find("option:selected").is(":hidden") && e.find("option").attr("selected", !1).filter(function() {
return "block" == $(this).css("display") || "inline" == $(this).css("display")
}).first().attr("selected", !0), 1 == e.find("option").length && "0" == e.find(":selected").val() && e.parent().parent().hide(), e.trigger("change")
}
},
d = function() {
a.find(".ecpay-pl-intro").trigger("click"), a.find(".ecpay-pay-list > li").length > 1 && ($(".ecpay-pay-list > li").first().addClass("ecpay-pl-act"), $(".ecpay-pay-list > li").last().removeClass("ecpay-pl-act"))
},
r = function(e) {
var t = $(".ecpay-pl-act", a),
n = t.find(".ecpay-cc-card").val().replace(/\s/g, ""),
i = t.find(".ecpay-date").val().replace(/\s/g, "").split("/"),
c = t.find(".ecpay-cc-cvc").val(),
d = t.attr("id");
if ("FlexibleInstallment" == d) var r = 0,
l = t.find(".ecpay-FlexibleInstallment option:selected").val();
if ("CreditInstallment" == d) r = t.find(".ecpay-Installment option:selected").val(), l = 0;
var s = "true" == t.find(".ecpay-pay-check :checked").val() ? 1 : 0;
return {
Token: e,
CardInfo: {
CardNumber: n,
CardValidYY: i[1],
CardValidMM: i[0],
CardCVV2: c,
BindingCard: s
},
CreditInstallment: r,
FlexibleInstallment: l
}
},
l = function(e) {
var t = $(".ecpay-pl-act", a),
n = t.attr("id"),
i = 0,
c = 0;
return "FlexibleInstallment" == n && (c = t.find(".ecpay-FlexibleInstallment option:selected").val()), "CreditInstallment" == n && (i = t.find(".ecpay-Installment option:selected").val()), {
PayToken: e,
CreditInstallment: i,
ATMBankCode: t.find(".ecpay-ATMBank option:selected").val(),
CardCVV2: t.find(".ecpay-cc-cvc").val(),
FlexibleInstallment: c
}
},
s = function(a, t) {
p(e + "/Payment/GetCardKind", {
CardNo: a
}, function(e, a, n) {
t(e, null)
}, function(e, a, n) {
t(null, "System error")
})
},
p = function(e, a, t, n) {
$.ajax({
url: e,
data: a,
type: "post",
dataType: "json",
success: t,
error: n
})
},
y = function() {
return navigator.userAgent.indexOf("MSIE") > 0 || navigator.userAgent.indexOf("Trident") > 0
}
},
Pay = new Func;
if ("undefined" == typeof jQuery) throw new Error("ECPG sdk's JavaScript requires jQuery");
! function(e) {
"use strict";
var n = jQuery.fn.jquery.split(" ")[0].split(".");
if (n[0] < 3 && n[1] < 4 || 3 == n[0] && 4 == n[1] && n[2] < 1) throw new Error("ECPG sdk's JavaScript requires jQuery version 3.4.1 or higher")
}();
var ECP = function() {
"use strict";
var e, n, t, r, a, i = "1.0.0",
o = $("#ECPayPayment"),
u = "zh-TW",
f = "",
s = "",
l = 1,
c = "https://ecpg-stage.ecpay.com.tw",
y = "https://ecpg.ecpay.com.tw";
function p(e) {
var t = n.encrypt(e);
return forge.util.encode64(t)
}
function d(n) {
var t = e.decrypt(forge.util.decode64(n)),
r = forge.util.decodeUtf8(t);
return JSON.parse(r)
}
function g(n) {
var t = forge.md.sha256.create();
return t.update(n, "utf8"), forge.util.encode64(e.sign(t))
}
function S(e, n, t) {
var r = forge.cipher.createCipher("AES-CBC", n);
return r.start({
iv: t
}), r.update(forge.util.createBuffer(e)), r.finish(), forge.util.encode64(r.output.data)
}
function v(e, n, t) {
var r = forge.cipher.createDecipher("AES-CBC", forge.util.encodeUtf8(n));
r.start({
iv: forge.util.encodeUtf8(t)
}), r.update(forge.util.createBuffer(forge.util.decode64(e))), r.finish();
var a = r.output.data;
return JSON.parse(decodeURIComponent(a))
}
function h(e, n, t, r, i) {
$.ajax({
url: a + e,
type: "post",
beforeSend: t,
cache: !1,
contentType: "application/json",
data: JSON.stringify(n),
dataType: "json",
success: r,
error: i
})
}
function P(e) {
return e.split("-----")[2]
}
function r() {
var e = Date.now();
return "undefined" != typeof performance && "function" == typeof performance.now && (e += performance.now()), "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function(n) {
var t = (e + 16 * Math.random()) % 16 | 0;
return e = Math.floor(e / 16), ("x" === n ? t : 3 & t | 8).toString(16)
})
}
function m() {
var e = navigator.userAgent,
n = "";
n = e.indexOf("Mac OS X") > -1 ? (e.match(/OS X [\d._]*/gi) + "").replace(/[^0-9|_.]/gi, "").replace(/_/gi, ".") : e.indexOf("Android") > -1 || e.indexOf("Linux") > -1 ? e.substr(e.indexOf("Android") + 8, e.indexOf(";", e.indexOf("Android")) - e.indexOf("Android") - 8) : "Unknown";
var t = {
DeviceInfo: e,
IsSimulator: 0,
OS: 0,
OSVersion: "" == n ? "Unknown" : n,
Vers: i
};
return JSON.stringify(t)
}
function x(e, n, i, o) {
e.setRequestHeader("Language", o), e.setRequestHeader("UUID", r), e.setRequestHeader("AppName", a), e.setRequestHeader("AppInfo", m()), e.setRequestHeader("KeyInfo", n), e.setRequestHeader("Signature", i), e.setRequestHeader("KeyID", t)
}
function T(e, t, r) {
if (1 == e.TransCode) {
var a = t.getResponseHeader("Signature");
if (null != a)
if (function(e, t) {
var r = forge.md.sha256.create();
return r.update(e, "utf8"), n.verify(r.digest().getBytes(), forge.util.decode64(t))
}(JSON.stringify(e), a)) {
var i = t.getResponseHeader("KeyInfo");
if (null != i) {
var o = d(i);
if (null != o) {
var u = v(e.Data, o.key, o.iv);
null != u ? r(u, null) : r(null, "The parameter [Data] decrypt fail")
} else r(null, "The parameter [KeyInfo] decrypt fail")
} else r(null, "The parameter [KeyInfo] cannot be empty")
} else r(null, "Signature verify error");
else r(null, "The parameter [Signature] cannot be empty")
} else r(null, e.TransMsg)
}
function C(e, n, t, r) {
r(null, e.statusText)
}
function I(i) {
r = r();
var o = forge.pki.rsa.generateKeyPair({
bits: 1024,
e: 65537
});
e = o.privateKey,
function(e, n) {
h("/Gateway/ExchangeKey", {
clientPublicKey: P(e)
}, function(e) {
e.setRequestHeader("UUID", r), e.setRequestHeader("AppName", a), e.setRequestHeader("AppInfo", m())
}, function(e, t, r) {
if (1 == e.TransCode) {
var a = r.getResponseHeader("KeyInfo");
if (null != a) {
var i = d(a);
if (null != i) {
var o = v(e.Data, i.key, i.iv);
if (null != o)
if (1 == o.RtnCode) {
var u = {
ServerPublicKey: forge.pki.publicKeyFromPem("-----BEGIN PUBLIC KEY-----" + o.ServerPublicKey + "-----END PUBLIC KEY-----"),
KeyID: o.KeyID
};
n(u, null)
} else n(null, o.RtnMsg);
else n(null, "The parameter [Data] decrypt fail")
} else n(null, "The parameter [KeyInfo] decrypt fail")
} else n(null, "The parameter [KeyInfo] cannot be empty")
} else n(null, e.TransMsg)
}, function(e, t, r) {
C(e, 0, 0, n)
})
}(forge.pki.publicKeyToPem(o.publicKey).replace(/\r\n/g, ""), function(e, r) {
void 0 === i || null === r ? (n = e.ServerPublicKey, t = e.KeyID, void 0 !== i && i(r)) : i(r)
})
}
this.ServerType = {
Stage: "STAGE",
Prod: "PROD"
}, this.Language = {
zhTW: "zh-TW",
enUS: "en-US"
}, this.initialize = function(e, n, t) {
if (void 0 === e) throw "ServerType Error: ServerType is not defined";
switch (f = e.toUpperCase()) {
case this.ServerType.Stage:
a = c;
break;
default:
a = y
}
if (void 0 !== n && "" !== n && null !== n || (n = l), 1 == (l = n) && $.ajax({
type: "GET",
url: a + "/Payment/Loading",
success: function(e) {
$("#ECPayPayment").html(e)
}
}), "undefined" == typeof forge) {
var r = a + "/Scripts/forge-0.10.0.min.js";
$.getScript(r).done(function() {
I(t)
}).fail(function() {
throw new Error("Forge load failed!!")
})
} else I(t)
}, this.createPayment = function(c, y, d) {
if ("" === f) throw "Initialize Error: Initialize is not setup";
if (void 0 === y) throw "Language Error: Language is not defined";
if (!new RegExp(/^[0-9a-z]{32}$/).test(c)) throw "Initialize Error: Invalid Token Format";
u = y;
var v = {
Token: s = c
},
P = forge.util.encode64(forge.random.getBytesSync(32)).substr(0, 32),
m = forge.util.encode64(forge.random.getBytesSync(16)).substr(0, 16),
I = {
Data: S(JSON.stringify(v), P, m)
};
! function(e, n, t, r, a) {
h("/Gateway/GetUIInfo", e, function(e) {
x(e, n, t, a)
}, function(e, n, t) {
T(e, t, r)
}, function(e, n, t) {
C(e, n, t, r)
})
}(I, p(JSON.stringify({
key: P,
iv: m
})), g(JSON.stringify(I)), function(f, c) {
null != f ? 1 == f.RtnCode ? function(f, l, c, y) {
$.ajax({
url: a + "/Payment/UIProcess",
type: "post",
beforeSend: function(e) {
e.setRequestHeader("Language", y)
},
cache: !1,
contentType: "application/json",
data: JSON.stringify(f)
}).done(function(f) {
if (1 == c && $("#ecpay-loading").hide(), o.html(f), "pay" == $(".ecpay-JsType").text());
else if ("cardinfo" == $(".ecpay-JsType").text()) {
var y = document.createElement("script");
y.type = "text/javascript", y.src = a + "/Scripts/Payment/actions." + $(".ecpay-JsType").text() + ".min.js?v=" + (new Date).getMonth().toString() + (new Date).getDate().toString() + (new Date).getHours().toString(), $("head")[0].appendChild(y), y.addEventListener("load", function() {
var o = new Func({
sdkVersion: i,
token: s,
language: u,
cliPriKey: e,
serPubKey: n,
keyID: t,
uuid: r
});
o.Init(a)
})
}
l(null)
})
}(f, d, l, y) : d(f.RtnMsg) : d(c)
}, y)
}, this.getPayToken = function(e) {
if ("" === f) throw "Initialize Error: Initialize is not setup";
if (Pay.ChkPayData()) {
var n = Pay.CreatePayData(s),
t = forge.util.encode64(forge.random.getBytesSync(32)).substr(0, 32),
r = forge.util.encode64(forge.random.getBytesSync(16)).substr(0, 16),
a = {
Data: S(n.Data, t, r)
},
i = p(JSON.stringify({
key: t,
iv: r
})),
o = g(JSON.stringify(a)),
l = {
PayToken: null,
PaymentType: n.PayType
};
0 == n.PayToken ? function(e, n, t, r) {
h("/Gateway/GetPayToken", e, function(e) {
x(e, n, t, u)
}, function(e, n, t) {
T(e, t, r)
}, function(e, n, t) {
C(e, n, t, r)
})
}(a, i, o, function(n, t) {
null != n ? 1 == n.RtnCode ? (l.PayToken = n.PayToken, e(l, null)) : e(l, n.RtnMsg) : e(l, t)
}) : $.inArray(n.PayType, ["CreditCard", "CreditInstallment", "Period", "Travel", "ATM"]) >= 0 ? function(e, n, t, r) {
h("/Gateway/BindPaymentInfo", e, function(e) {
x(e, n, t, u)
}, function(e, n, t) {
T(e, t, r)
}, function(e, n, t) {
C(e, n, t, r)
})
}(a, i, o, function(t, r) {
null != t ? 1 == t.RtnCode ? (l.PayToken = n.PayToken, e(l, null)) : e(l, t.RtnMsg) : e(l, r)
}) : (l.PayToken = n.PayToken, e(l, null))
} else e(null, null)
}, this.getLanguage = function() {
return u
}, this.CreateApplePayment = function(e) {
return function(e) {
try {
var n = forge.util.encode64(forge.random.getBytesSync(32)).substr(0, 32),
t = forge.util.encode64(forge.random.getBytesSync(16)).substr(0, 16),
r = ECApplePay.ObjAppleSession(),
a = new ApplePaySession(2, r);
a.onvalidatemerchant = function(e) {
var r, i, o, f = {
Data: S(ECApplePay.CreateMAuthData(e.validationURL), n, t)
},
s = p(JSON.stringify({
key: n,
iv: t
})),
l = g(JSON.stringify(f));
r = s, i = l, o = function(e, n) {
null != e && 1 == e.RtnCode ? a.completeMerchantValidation(e.AppleSession) : a.completeMerchantValidation("")
}, h("/Gateway/ApplePayValidate", f, function(e) {
x(e, r, i, u)
}, function(e, n, t) {
T(e, t, o)
}, function(e, n, t) {
C(e, 0, 0, o)
})
}, a.onpaymentauthorized = function(r) {
var i = {
Data: S(ECApplePay.CreatePayAuthData(JSON.stringify(r.payment)), n, t)
},
o = p(JSON.stringify({
key: n,
iv: t
})),
f = g(JSON.stringify(i));
! function(e, n, t, r) {
h("/Gateway/ApplePayPayment", e, function(e) {
x(e, n, t, u)
}, function(e, n, t) {
T(e, t, r)
}, function(e, n, t) {
C(e, 0, 0, r)
})
}(i, o, f, function(n, t) {
if (null != n && 1 == n.RtnCode) a.completePayment(ApplePaySession.STATUS_SUCCESS);
else if (a.completePayment(ApplePaySession.STATUS_FAILURE), null == n) return e(null, t), !1;
e(n, null)
})
}, a.oncancel = function(n) {
a = null, e(null, "Cancel")
}, a.begin()
} catch (e) {}
}(e), !1
}
},
ECPay = new ECP;
ECP.prototype = function() {
return this.Language
}, ECP.prototype = function() {
return this.ServerType
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment