fix(orders): restore frontend pay REST and harden order-center client
Add AKSRT\Payment\OrderCenterRest so /shop/frontend/orders/{n}/pay|cancel
always register on rest_api_init; widen order_number route charset; make
order-center.js use credentials, encodeURIComponent, and clearer 404 errors.
This commit is contained in:
+40
-14
@@ -51,18 +51,44 @@
|
||||
}
|
||||
|
||||
function request(path, body, method) {
|
||||
return fetch(restRoot() + path, {
|
||||
var opts = {
|
||||
method: method || "POST",
|
||||
credentials: "same-origin",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"X-WP-Nonce": aksrtData.restNonce
|
||||
},
|
||||
body: body === undefined ? undefined : JSON.stringify(body || {})
|
||||
}).then(function (response) {
|
||||
return response.json();
|
||||
"X-WP-Nonce": (aksrtData && aksrtData.restNonce) || ""
|
||||
}
|
||||
};
|
||||
if (body !== undefined) {
|
||||
opts.body = JSON.stringify(body || {});
|
||||
}
|
||||
return fetch(restRoot() + path, opts).then(function (response) {
|
||||
return response.text().then(function (text) {
|
||||
var data = {};
|
||||
try {
|
||||
data = text ? JSON.parse(text) : {};
|
||||
} catch (e) {
|
||||
data = {
|
||||
success: false,
|
||||
message: response.status === 404
|
||||
? "支付接口未找到(404),请刷新页面或联系管理员。"
|
||||
: ("请求失败(HTTP " + response.status + ")")
|
||||
};
|
||||
}
|
||||
if (typeof data.success === "undefined" && response.ok === false) {
|
||||
data.success = false;
|
||||
data.message = data.message || data.code || ("HTTP " + response.status);
|
||||
}
|
||||
return data;
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function orderPath(orderNumber, suffix) {
|
||||
var base = "/shop/frontend/orders/" + encodeURIComponent(orderNumber || "");
|
||||
return suffix ? base + suffix : base;
|
||||
}
|
||||
|
||||
function cartUrl() {
|
||||
return aksrtData.cartUrl || (aksrtData.routes && aksrtData.routes.cart) || "/";
|
||||
}
|
||||
@@ -217,7 +243,7 @@
|
||||
return;
|
||||
}
|
||||
if (triggerBtn) triggerBtn.disabled = true;
|
||||
var url = restRoot() + "/shop/frontend/orders/" + encodeURIComponent(orderNumber) + "/review-modal";
|
||||
var url = restRoot() + orderPath(orderNumber, "/review-modal");
|
||||
window.AksrtRemoteModal.openRemote({
|
||||
url: url,
|
||||
className: "modal-mini full-sm",
|
||||
@@ -274,7 +300,7 @@
|
||||
return item.file ? fileToDataUrl(item.file) : Promise.resolve(item.url || "");
|
||||
})).then(function (images) {
|
||||
images = images.filter(Boolean);
|
||||
return request("/shop/frontend/orders/" + orderNumber + "/review", {
|
||||
return request(orderPath(orderNumber, "/review"), {
|
||||
product_id: productId,
|
||||
content: comment,
|
||||
rating_product: scoreProduct,
|
||||
@@ -349,7 +375,7 @@
|
||||
close();
|
||||
// 关闭后查询订单是否已到账
|
||||
if (orderNumber) {
|
||||
request("/shop/frontend/orders/" + orderNumber, {}).then(function () {
|
||||
request(orderPath(orderNumber), {}, "GET").then(function () {
|
||||
window.location.reload();
|
||||
}).catch(function () {});
|
||||
}
|
||||
@@ -424,7 +450,7 @@
|
||||
submitBtn.textContent = "提交中...";
|
||||
}
|
||||
|
||||
request("/shop/frontend/orders/" + orderNumber + "/after-sale", {
|
||||
request(orderPath(orderNumber, "/after-sale"), {
|
||||
type: formData.get("type") || "refund",
|
||||
reason: reason,
|
||||
note: (formData.get("note") || "").trim()
|
||||
@@ -486,7 +512,7 @@
|
||||
if (!receiptOrder) return;
|
||||
if (!window.confirm("确认已收到货物?")) return;
|
||||
receiptBtn.disabled = true;
|
||||
request("/shop/frontend/orders/" + receiptOrder + "/confirm-receipt", {})
|
||||
request(orderPath(receiptOrder, "/confirm-receipt"), {})
|
||||
.then(function (result) {
|
||||
receiptBtn.disabled = false;
|
||||
if (!result || !result.success) {
|
||||
@@ -509,7 +535,7 @@
|
||||
var rebuyOrder = rebuyBtn.getAttribute("data-aksrt-order-rebuy");
|
||||
if (!rebuyOrder) return;
|
||||
rebuyBtn.disabled = true;
|
||||
request("/shop/frontend/orders/" + rebuyOrder + "/rebuy", {})
|
||||
request(orderPath(rebuyOrder, "/rebuy"), {})
|
||||
.then(function (result) {
|
||||
rebuyBtn.disabled = false;
|
||||
if (!result || !result.success) {
|
||||
@@ -544,7 +570,7 @@
|
||||
var payMethod = payButton.getAttribute("data-aksrt-order-method") || "";
|
||||
payButton.disabled = true;
|
||||
payButton.textContent = "处理中...";
|
||||
request("/shop/frontend/orders/" + payOrderNumber + "/pay", payMethod ? { payment_method: payMethod } : {})
|
||||
request(orderPath(payOrderNumber, "/pay"), payMethod ? { payment_method: payMethod } : {})
|
||||
.then(function (result) {
|
||||
if (!result.success) {
|
||||
payButton.disabled = false;
|
||||
@@ -589,7 +615,7 @@
|
||||
var cancelOrderNumber = cancelButton.getAttribute("data-aksrt-order-cancel");
|
||||
cancelButton.disabled = true;
|
||||
cancelButton.textContent = "取消中...";
|
||||
request("/shop/frontend/orders/" + cancelOrderNumber + "/cancel")
|
||||
request(orderPath(cancelOrderNumber, "/cancel"))
|
||||
.then(function (result) {
|
||||
if (!result.success) {
|
||||
cancelButton.disabled = false;
|
||||
|
||||
Reference in New Issue
Block a user