Implementing Alipay and WeChat Scheme-Based App Payments in Third-Party H5 Environments

Alipay Scheme Configuration

The Alipay client can be invoked via the alipay:// protocol. The deep link structure mandates a fixed application identifier and an encoded payment string.

Link Structure

alipay://platformapi/startapp?appId=20000067&url={encodeURIComponent_payload}

The appId parameter remains constant at 20000067. The url parameter accepts the URL-encoded result from the backend alipay.trade.app.pay API response.

Invocation and Fallback Implementation

const rawPayload = backendResponse.orderString;
const encodedPayload = encodeURIComponent(rawPayload);
const targetUri = `alipay://platformapi/startapp?appId=20000067&url=${encodedPayload}`;

window.location.href = targetUri;

const fallbackTimer = setTimeout(() => {
  if (!document.hidden) {
    window.location.href = 'https://m.alipay.com/gateway/do?...'; // WAP endpoint
  }
}, 1500);

document.addEventListener('visibilitychange', () => {
  if (document.hidden) clearTimeout(fallbackTimer);
});

Native redirections require direct user interaction (e.g., click events) to bypass browser popup blockers. On iOS, Universal Links offer a more robust alternative requiring Associated Domains configuration.

WeChat Scheme Configuration

WeChat enforces strict validation on Scheme invocations. The weixin://wap/pay protocol is exclusively functional within applications registered on the WeChat Open Platform, matching specific bundle identifiers and signatures.

Link Structure

weixin://wap/pay?prepayid={prepay_id}&package=Sign%3DWXPay&noncestr={nonce}&timestamp={ts}&sign={signature}

All parameters are generated server-side during the unified order creation phase.

Invocation Logic

function triggerWxPayment(prepId, nonce, ts, signature) {
  const wxUri = `weixin://wap/pay?prepayid=${prepId}&package=Sign%3DWXPay&noncestr=${nonce}&timestamp=${ts}&sign=${signature}`;
  window.location.href = wxUri;

  setTimeout(() => {
    if (!document.hidden) {
      console.error('WeChat app not detected.');
      // Transition to QR code or H5 payment flow
    }
  }, 2000);
}

Unregistered host applications will face immediate interception by the WeChat client. Furthermore, iOS 9+ mandates declaring weixin within the host app's LSApplicationQueriesSchemes array in Info.plist to permit URI querying.

Compatibility and Fallback Strategies

Due to the volatility of Scheme redirections across different browsers and OS versions, implementing a robust fallback mechanism is critical.

  • Always bind Scheme triggers to explicit user gestures.
  • Monitor the visibilitychange event or utilize timeouts to detect failed app transitions.
  • For WeChat, if native bundle ID validation blocks the Scheme, shift immediately to WeChat H5 payment or render a payment QR code.
  • For Alipay, route failures to the standard mobile WAP payment gateway.
Payment ChannelProtocol FormatPrimary ConstraintAlternative Flow
Alipayalipay://platformapi/startapp...Requires user-triggered eventAlipay WAP gateway
WeChatweixin://wap/pay...Restricted to whitelisted app signaturesWeChat H5 payment or QR code

Tags: alipay WeChat scheme h5-payment mobile-payment

Posted on Sun, 24 May 2026 18:50:46 +0000 by Jas