Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Implementation:A616567126 GPT WEB JAVA WxPayUtil

From Leeroopedia
Knowledge Sources
Domains Payments, Security, WeChat_Pay
Last Updated 2026-10-13 00:00 GMT

Overview

Concrete tool for generating WeChat Pay v3 API signatures (RSA-SHA256) and constructing Native payment request JSON, provided as a static utility class.

Description

The WxPayUtil class provides static methods for WeChat Pay v3 integration:

  • getToken(method, url, body) — Builds the WECHATPAY2-SHA256-RSA2048 authorization header with RSA-SHA256 signature
  • buildNativePayJson(total, outTradeNo, description) — Constructs the JSON body for Native payment order creation
  • sign(message) — Signs bytes with the merchant's RSA private key
  • getPrivateKey() — Loads the PKCS8 private key from PayConfig (cached in Redis)
  • buildMessage(method, url, timestamp, nonceStr, body) — Constructs the canonical string to sign

Usage

Import this utility when you need to:

  • Sign WeChat Pay v3 API requests
  • Build Native payment order JSON bodies
  • Access the WeChat Pay merchant private key

Code Reference

Source Location

  • Repository: GPT-WEB-JAVA
  • File: src/main/java/com/intelligent/bot/utils/sys/WxPayUtil.java

Signature

public class WxPayUtil {
    public static String getToken(String method, HttpUrl url, String body) throws Exception;
    public static PrivateKey getPrivateKey() throws IOException;
    public static String sign(byte[] message) throws Exception;
    public static String buildMessage(String method, HttpUrl url, long timestamp, String nonceStr, String body);
    public static String buildNativePayJson(BigDecimal total_amount, String out_trade_no, String description);
}

Import

import com.intelligent.bot.utils.sys.WxPayUtil;

I/O Contract

Inputs

Name Type Required Description
method String Yes HTTP method (POST, GET)
url HttpUrl Yes Request URL
body String Yes Request body
total_amount BigDecimal Yes Payment amount (converted to cents)
out_trade_no String Yes Merchant order number
description String Yes Product description

Outputs

Name Type Description
getToken returns String Full Authorization header value
buildNativePayJson returns String JSON body for Native pay API

Usage Examples

WeChat Pay Native Order

String bodyParam = WxPayUtil.buildNativePayJson(
    new BigDecimal("5"), order.getId().toString(), "获取卡密#5");
HttpUrl httpurl = HttpUrl.parse(CommonConst.WX_NATIVE_URL);
String sign = WxPayUtil.getToken("POST", httpurl, bodyParam);
String body = HttpUtil.createPost(CommonConst.WX_NATIVE_URL)
    .header(Header.AUTHORIZATION, sign)
    .header(Header.CONTENT_TYPE, "application/json")
    .body(bodyParam)
    .execute()
    .body();
// body contains "code_url" for QR code generation

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment