微信APP支付

本文介绍了在商城项目中实现微信APP支付的流程,包括官方文档链接、调试支付的挑战、后端主要任务(生成加密支付数据、编写回调接口、查询支付结果)以及统一下单和回调方法的示例。在APP支付中,后端工作相对简化,只需处理3个关键接口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前言:最近公司接了个商城项目,被分到了做微信支付,支付宝支付和stripe国际支付,在此记录一下。

正文:

    微信支付接入方式分很多种如下:

 

比如h5,大部分业务几乎都在后台,而app支付一半在前端一半在后端,我这里讲的是app支付

官方说明文档地址:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_1

想要调试支付,必须得注册成商户,需要有营业执照什么的,不像微信公众号会有测试公众号,这也是我们比较难测试的地方

时序图:

从上面看出来,后端的工作只有三个:

1.生成加密的支付数据交给前端(统一下单,链接:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1

2.编写后台回调(支付结果通知,链接:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_7&index=3

3.查询支付结果(查询订单,链接:https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_2&index=4

所以在app支付中我们后端只需要给前端准备数据,和接受回调就行了,还有一个查询订单支付结果,一共只3个接口需要编写,相对于h5少了很多东西。

在这里我展示统一下单,和回调方法:

第一步:

准备商户数据

 

package com.otcbi.pay.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.otcbi.coin.common.exception.ApiException;
import com.otcbi.coin.common.exception.ApiResponMessage;
import com.otcbi.common.dto.OperateResult;
import com.otcbi.pay.dto.ShopOrder;
import com.otcbi.pay.entity.WechatOrder;
import com.otcbi.pay.service.IWechatOrderService;
import com.otcbi.pay.service.WeiXinPayService;
import com.otcbi.pay.utils.MD5Util;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.*;

@Service
public class WeiXinPayServiceImpl implements WeiXinPayService {
    protected Logger logger = LoggerFactory.getLogger(WeiXinPayServiceImpl.class);

    // 正在处理的订单号
    public static Set<String> processOrder = new HashSet<String>();

    @Value("${wechat.appid}")
    private String appidConfig;

    //商户id
    @Val
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值