Files
wordpress-theme-aksrt/assets/js/pay-cashier.js
T
kate522andClaude Opus 5 35bb96e82f refactor: rename the theme brand and every identifier from AKSRT to Jio
Full-project rename. `git grep -i aksrt` is now empty apart from the
DBMigrator 3.3.0 changelog note that documents the rename itself.

Code identifiers
- PHP namespace AKSRT\ -> Jio\ (composer PSR-4 + 378 declarations)
- global constants AKSRT_* -> JIO_* (VERSION/DIR/URI, PAY_TYPE_*, CAPTCHA_*)
- procedural functions, hooks, options, meta keys, table names,
  cookies, query vars, ajax actions and nonces: aksrt_ -> jio_
- text domain aksrt -> jio; Theme Name -> Jio
- JS globals: aksrtData/aksrtAdminData/... -> jioData/jioAdminData/...,
  window.AKSRTCaptcha & friends -> window.JioCaptcha, Aksrt* -> Jio*
- Vue components Aks*.vue -> Jio*.vue (15 files renamed + imports)
- CSS classes/data-attrs/custom properties: aksrt- -> jio-
- REST namespace aksrt/v1 -> jio/v1 (incl. payment gateway callbacks)

Storage layer
- DB tables {prefix}aksrt_* -> {prefix}jio_*; SCHEMA_VERSION 3.2.0 -> 3.3.0
  so dbDelta creates the new names on install
- widget id_base aksrt_widget_* -> jio_widget_*
- uninstall.php drop list rebuilt from the actual migrator tables
  (dropped 3 stale entries, added the 17 missing ones)

Removed
- hardcoded https://api.aksrt.com/version.json default update endpoint;
  get_endpoint() now reads the 「更新端点」 setting and do_check() no-ops
  when it is empty

Files renamed: inc/helpers/aksrt-*.php -> jio-*.php (54),
assets/admin/src/components/form/Aks*.vue -> Jio*.vue (15).
Build artifacts regenerated: assets/css/build, assets/js/runtime,
assets/admin/build.

Verified: audit-psr4-paths errors=0, audit-admin-contracts errors=0
(warnings=56, unchanged baseline), 813/813 widget-system tests,
44/44 phpunit, php -l clean across all PHP.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-11-01 20:05:04 +08:00

163 lines
5.6 KiB
JavaScript

/**
* Unified cashier: renders the gateway result returned by PaymentOrchestrator::pay().
*
* mode=qrcode on-site QR modal (+ optional mobile deep link)
* mode=redirect same-tab navigation to the gateway page
* mode=form auto-submit the server-built POST form
* mode=jsapi WeChat in-app WeixinJSBridge payment
*
* Polling stays with the caller (JioVipPurchaseCoordinator.poll); this module
* only presents the payment step and reports when the QR modal is dismissed.
*
* @package Jio
*/
(function (window, document) {
"use strict";
var KIND = "pay-cashier";
var sequence = 0;
var listeners = Object.create(null);
function escapeHtml(value) {
return String(value == null ? "" : value).replace(/[&<>"']/g, function (ch) {
return { "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" }[ch];
});
}
function isMobile() {
return /Android|iPhone|iPad|iPod|Mobile/i.test(window.navigator.userAgent || "");
}
function qrDataUrl(text) {
if (typeof window.qrcode !== "function" || !text) return "";
try {
var qr = window.qrcode(0, "M");
qr.addData(String(text));
qr.make();
return qr.createDataURL(6, 12);
} catch (error) {
return "";
}
}
function amountText(data) {
if (data.amount === undefined || data.amount === null || data.amount === "") return "";
var mark = (window._win && window._win.pay_mark) || "¥";
return mark + String(data.amount);
}
/** Inner QR markup; reusable by callers that own their own modal. */
function qrHtml(data) {
var src = qrDataUrl(data.code_url);
var html = '<div class="jio-pay-cashier">';
var amount = amountText(data);
if (amount) html += '<div class="jio-pay-cashier__amount">' + escapeHtml(amount) + "</div>";
html += src
? '<img class="jio-pay-cashier__qr" src="' + src + '" width="200" height="200" alt="支付二维码">'
: '<p class="jio-pay-cashier__error">二维码组件未加载,请刷新页面重试。</p>';
html += '<p class="jio-pay-cashier__tip">' + escapeHtml(data.message || "请使用手机扫码完成支付") + "</p>";
if (data.payment_url && isMobile()) {
html += '<a class="but jb-blue jio-pay-cashier__open" href="' + escapeHtml(data.payment_url) + '">打开支付应用</a>';
}
if (data.order_number) {
html += '<p class="jio-pay-cashier__order muted-2-color em09">订单号:' + escapeHtml(data.order_number) + "</p>";
}
return html + "</div>";
}
function openQr(data, options) {
var runtime = window.JioModalRuntime;
if (!runtime) return false;
var id = String(++sequence);
if (typeof options.onClose === "function") listeners[id] = options.onClose;
runtime.open({
kind: KIND,
cashierId: id,
title: options.title || "扫码支付",
className: "jio-pay-cashier-modal",
telemetry: false,
html: qrHtml(data),
});
return true;
}
function submitForm(html) {
var host = document.createElement("div");
host.hidden = true;
host.innerHTML = String(html || "");
var form = host.querySelector("form");
if (!form) return false;
document.body.appendChild(host);
form.submit();
return true;
}
function invokeJsapi(params, options) {
var run = function () {
window.WeixinJSBridge.invoke("getBrandWCPayRequest", params, function (res) {
var msg = res && res.err_msg ? String(res.err_msg) : "";
if (msg === "get_brand_wcpay_request:ok") return;
if (typeof options.onClose === "function") {
options.onClose(msg === "get_brand_wcpay_request:cancel" ? "cancel" : "error");
}
});
};
if (window.WeixinJSBridge && typeof window.WeixinJSBridge.invoke === "function") {
run();
} else {
document.addEventListener("WeixinJSBridgeReady", run, { once: true });
}
return true;
}
/**
* Present a pending payment. Returns the handled mode, or "" when the
* payload carries no cashier step (synchronous gateways).
*
* options.onClose(reason) fires when the QR modal is dismissed or the
* JSAPI sheet is cancelled; callers stop polling there.
*/
function handle(data, options) {
data = data || {};
options = options || {};
switch (data.mode) {
case "qrcode":
return data.code_url && openQr(data, options) ? "qrcode" : "";
case "redirect":
if (!data.payment_url) return "";
window.location.assign(data.payment_url);
return "redirect";
case "form":
return submitForm(data.form_html) ? "form" : "";
case "jsapi":
return data.jsapi_params ? invokeJsapi(data.jsapi_params, options) && "jsapi" : "";
default:
return "";
}
}
/** Close the cashier QR modal after a terminal poll result. */
function close() {
var runtime = window.JioModalRuntime;
var shell = runtime ? runtime.getActive() : null;
if (shell && shell.classList.contains("jio-pay-cashier-modal")) runtime.close("paid");
}
document.addEventListener("jio:modal:close", function (event) {
var detail = event && event.detail ? event.detail : {};
var opts = detail.session && detail.session.options ? detail.session.options : {};
if (opts.kind !== KIND || !listeners[opts.cashierId]) return;
var fn = listeners[opts.cashierId];
delete listeners[opts.cashierId];
if (detail.reason !== "paid") fn(detail.reason || "close");
});
window.JioPayCashier = {
KIND: KIND,
handle: handle,
close: close,
qrHtml: qrHtml,
isPending: function (data) { return !!(data && data.mode); },
};
})(window, document);