java实现扫码枪-微信支付

简介: java实现扫码枪-微信支付

实现 Java 扫码枪支付的微信支付功能涉及几个步骤,包括获取微信支付 API 凭证、生成支付二维码、扫描二维码并获取支付结果。以下是一个完整的实现示例,包括详细的注释。

步骤 1:准备工作

确保你已经注册了微信支付账号,并获得了以下信息:

  • 商户号(mch_id)
  • 应用ID(app_id)
  • API 密钥(api_key)

步骤 2:添加依赖

在你的项目中添加必要的依赖。例如,如果你使用的是 Maven 项目,可以在 pom.xml 文件中添加以下依赖:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.13</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.11.3</version>
</dependency>
<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.15</version>
</dependency>

步骤 3:生成微信支付二维码

以下代码示例演示如何生成微信支付二维码:

import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.UUID;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
 
public class WeChatPayQRCode {
 
    private static final String WECHAT_PAY_URL = "https://api.mch.weixin.qq.com/pay/unifiedorder";
    private static final String APP_ID = "your_app_id";
    private static final String MCH_ID = "your_mch_id";
    private static final String API_KEY = "your_api_key";
    private static final String NOTIFY_URL = "http://your_notify_url";
    private static final String TRADE_TYPE = "NATIVE";
 
    public static void main(String[] args) throws Exception {
        String productId = UUID.randomUUID().toString().replaceAll("-", "");
        String qrCodeUrl = generateWeChatQRCode("Test Product", productId, 1);
        System.out.println("QR Code URL: " + qrCodeUrl);
    }
 
    public static String generateWeChatQRCode(String productDescription, String productId, int totalFee) throws Exception {
        SortedMap<String, String> params = new TreeMap<>();
        params.put("appid", APP_ID);
        params.put("mch_id", MCH_ID);
        params.put("nonce_str", UUID.randomUUID().toString().replaceAll("-", ""));
        params.put("body", productDescription);
        params.put("out_trade_no", productId);
        params.put("total_fee", String.valueOf(totalFee));
        params.put("spbill_create_ip", "127.0.0.1");
        params.put("notify_url", NOTIFY_URL);
        params.put("trade_type", TRADE_TYPE);
 
        String sign = generateSignature(params, API_KEY);
        params.put("sign", sign);
 
        String xmlRequest = mapToXml(params);
        String responseXml = sendPost(WECHAT_PAY_URL, xmlRequest);
 
        Map<String, String> responseMap = xmlToMap(responseXml);
        if ("SUCCESS".equals(responseMap.get("return_code")) && "SUCCESS".equals(responseMap.get("result_code"))) {
            return responseMap.get("code_url");
        } else {
            throw new RuntimeException("Failed to generate WeChat QR Code: " + responseMap.get("return_msg"));
        }
    }
 
    private static String generateSignature(SortedMap<String, String> params, String apiKey) throws Exception {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (entry.getValue() != null && !"".equals(entry.getValue()) && !"sign".equals(entry.getKey())) {
                sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
        }
        sb.append("key=").append(apiKey);
        return org.apache.commons.codec.digest.DigestUtils.md5Hex(sb.toString()).toUpperCase();
    }
 
    private static String mapToXml(SortedMap<String, String> params) {
        StringBuilder sb = new StringBuilder();
        sb.append("<xml>");
        for (Map.Entry<String, String> entry : params.entrySet()) {
            sb.append("<").append(entry.getKey()).append(">").append(entry.getValue()).append("</").append(entry.getKey()).append(">");
        }
        sb.append("</xml>");
        return sb.toString();
    }
 
    private static String sendPost(String url, String xml) throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new StringEntity(xml, "UTF-8"));
        CloseableHttpResponse response = httpClient.execute(httpPost);
        try {
            return EntityUtils.toString(response.getEntity(), "UTF-8");
        } finally {
            response.close();
            httpClient.close();
        }
    }
 
    private static Map<String, String> xmlToMap(String xml) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        return mapper.readValue(xml, HashMap.class);
    }
}

步骤 4:处理扫码枪输入

扫码枪通常作为键盘输入设备工作,因此可以直接读取输入内容。

import java.util.Scanner;
 
public class ScannerInput {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("请扫描二维码:");
        while (scanner.hasNextLine()) {
            String input = scanner.nextLine();
            System.out.println("扫码内容: " + input);
            // 在这里调用支付查询接口,检查支付状态
            // checkPaymentStatus(input);
        }
    }
}

步骤 5:查询支付状态

import java.util.SortedMap;
import java.util.TreeMap;
import java.util.UUID;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
 
public class WeChatPayQuery {
 
    private static final String WECHAT_PAY_QUERY_URL = "https://api.mch.weixin.qq.com/pay/orderquery";
    private static final String APP_ID = "your_app_id";
    private static final String MCH_ID = "your_mch_id";
    private static final String API_KEY = "your_api_key";
 
    public static void main(String[] args) throws Exception {
        String orderId = "your_order_id";
        String result = checkPaymentStatus(orderId);
        System.out.println("支付结果: " + result);
    }
 
    public static String checkPaymentStatus(String orderId) throws Exception {
        SortedMap<String, String> params = new TreeMap<>();
        params.put("appid", APP_ID);
        params.put("mch_id", MCH_ID);
        params.put("nonce_str", UUID.randomUUID().toString().replaceAll("-", ""));
        params.put("out_trade_no", orderId);
 
        String sign = generateSignature(params, API_KEY);
        params.put("sign", sign);
 
        String xmlRequest = mapToXml(params);
        String responseXml = sendPost(WECHAT_PAY_QUERY_URL, xmlRequest);
 
        Map<String, String> responseMap = xmlToMap(responseXml);
        if ("SUCCESS".equals(responseMap.get("return_code")) && "SUCCESS".equals(responseMap.get("result_code"))) {
            return responseMap.get("trade_state");
        } else {
            throw new RuntimeException("Failed to query WeChat payment status: " + responseMap.get("return_msg"));
        }
    }
 
    private static String generateSignature(SortedMap<String, String> params, String apiKey) throws Exception {
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (entry.getValue() != null && !"".equals(entry.getValue()) && !"sign".equals(entry.getKey())) {
                sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
        }
        sb.append("key=").append(apiKey);
        return org.apache.commons.codec.digest.DigestUtils.md5Hex(sb.toString()).toUpperCase();
    }
 
    private static String mapToXml(SortedMap<String, String> params) {
        StringBuilder sb = new StringBuilder();
        sb.append("<xml>");
        for (Map.Entry<String, String> entry : params.entrySet()) {
            sb.append("<").append(entry.getKey()).append(">").append(entry.getValue()).append("</").append(entry.getKey()).append(">");
        }
        sb.append("</xml>");
        return sb.toString();
    }
 
    private static String sendPost(String url, String xml) throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpPost httpPost = new HttpPost(url);
        httpPost.setEntity(new StringEntity(xml, "UTF-8"));
        CloseableHttpResponse response = httpClient.execute(httpPost);
        try {
            return EntityUtils.toString(response.getEntity(), "UTF-8");
相关文章
|
5月前
|
小程序 Java 关系型数据库
基于Java微信小程序智能招聘平台设计和实现(源码+LW+调试文档+讲解等)
基于Java微信小程序智能招聘平台设计和实现(源码+LW+调试文档+讲解等)
|
14天前
|
小程序 前端开发 算法
|
5月前
|
小程序 Java 关系型数据库
基于Java微信小程序同城家政服务系统设计和实现(源码+LW+调试文档+讲解等)
基于Java微信小程序同城家政服务系统设计和实现(源码+LW+调试文档+讲解等)
|
20天前
|
Java API 开发者
Java如何实现企业微信审批流程
大家好,我是V哥。本文分享如何在企业微信中实现审批流程,通过调用企业微信的开放API完成。主要内容包括获取Access Token、创建审批模板、发起审批流程和查询审批结果。提供了一个Java示例代码,帮助开发者快速上手。希望对你有帮助,关注V哥爱编程,编码路上同行。
|
5月前
|
小程序 Java 关系型数据库
基于Java微信小程序小说阅读系统设计和实现(源码+LW+调试文档+讲解等)
基于Java微信小程序小说阅读系统设计和实现(源码+LW+调试文档+讲解等)
|
1月前
|
算法 小程序 Java
java制作海报三:获取微信二维码详情,并改变大小,合成到海报(另一张图片)上
这篇文章介绍了如何使用Java获取微信小程序的二维码,并将其调整大小后合成到海报(另一张图片)上。
39 0
|
1月前
|
小程序
java--微信小程序发送模板消息
java--微信小程序发送模板消息
102 0
|
3月前
|
JavaScript 前端开发 Java
|
5月前
|
小程序 安全 Java
基于Java微信小程序民宿短租系统设计和实现(源码+LW+调试文档+讲解等)
基于Java微信小程序民宿短租系统设计和实现(源码+LW+调试文档+讲解等)
基于Java微信小程序民宿短租系统设计和实现(源码+LW+调试文档+讲解等)
|
5月前
|
小程序 Java 关系型数据库
基于Java微信小程序自驾游拼团设计和实现(源码+LW+调试文档+讲解等)
基于Java微信小程序自驾游拼团设计和实现(源码+LW+调试文档+讲解等)