Java 实现微信和支付宝支付功能

一、前期准备

1、申请好微信商户号appid,拿到商户id和商户秘钥,退款的话需要商户证书
2、申请好支付宝商户号appid,商户公钥和秘钥(需要用支付宝工具自己生成),支付宝退款不需要证书

二、数据库表设计

1、微信支付配置表

CREATE TABLE `py_wx_config` (
 `wx_config_id` varchar(18) NOT NULL COMMENT '微信支付配置ID',
 `appid` varchar(128) DEFAULT NULL COMMENT '微信公众号ID',
 `mch_id` varchar(128) DEFAULT NULL COMMENT '商户号ID',
 `mch_key_secret` varchar(2200) DEFAULT NULL COMMENT '商户安全密钥',
 `crt_path` varchar(200) DEFAULT NULL COMMENT '商户证书存放路径',
 `app_secret` varchar(2200) DEFAULT NULL COMMENT '公众号安全密钥',
 `create_id` varchar(18) DEFAULT NULL COMMENT '创建人',
 `create_time` datetime DEFAULT NULL COMMENT '创建时间',
 `upd_id` varchar(18) DEFAULT NULL COMMENT '修改人',
 `upd_time` datetime DEFAULT NULL COMMENT '修改时间',
 `delete_flag` tinyint(1) NOT NULL DEFAULT '0' COMMENT '删除标志',
 `app_code` varchar(18) NOT NULL COMMENT '商家编码',
 `bm_code` varchar(20) DEFAULT NULL COMMENT '部门编码',
 PRIMARY KEY (`wx_config_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT COMMENT='微信支付配置';

2、微信小程序支付配置表

CREATE TABLE `py_wx_min_config` (
 `wx_min_config_id` varchar(18) NOT NULL COMMENT '微信支付配置ID',
 `appid` varchar(128) DEFAULT NULL COMMENT '微信公众号ID',
 `mch_id` varchar(128) DEFAULT NULL COMMENT '商户号ID',
 `mch_key_secret` varchar(2200) DEFAULT NULL COMMENT '商户安全密钥',
 `crt_path` varchar(200) DEFAULT NULL COMMENT '商户证书存放路径',
 `app_secret` varchar(2200) DEFAULT NULL COMMENT '公众号安全密钥',
 `create_id` varchar(18) DEFAULT NULL COMMENT '创建人',
 `create_time` datetime DEFAULT NULL COMMENT '创建时间',
 `upd_id` varchar(18) DEFAULT NULL COMMENT '修改人',
 `upd_time` datetime DEFAULT NULL COMMENT '修改时间',
 `delete_flag` tinyint(1) NOT NULL DEFAULT '0' COMMENT '删除标志',
 `app_code` varchar(18) NOT NULL COMMENT '商家编码',
 `sys_software_code` varchar(18) DEFAULT NULL COMMENT '系统编码',
 PRIMARY KEY (`wx_min_config_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT COMMENT='微信支付配置';

3、支付宝支付配置表

CREATE TABLE `py_alipay_config` (
 `alipay_config_id` varchar(18) NOT NULL COMMENT '支付宝支付配置ID',
 `ali_private_key` varchar(2200) DEFAULT NULL COMMENT '支付宝密钥',
 `ali_public_key` varchar(2200) DEFAULT NULL COMMENT '支付宝公钥',
 `ali_appid` varchar(128) DEFAULT NULL COMMENT '支付宝appid',
 `create_id` varchar(18) DEFAULT NULL COMMENT '创建人',
 `create_time` datetime DEFAULT NULL COMMENT '创建时间',
 `upd_id` varchar(18) DEFAULT NULL COMMENT '修改人',
 `upd_time` datetime DEFAULT NULL COMMENT '修改时间',
 `delete_flag` tinyint(1) NOT NULL DEFAULT '0' COMMENT '删除标志',
 `app_code` varchar(18) NOT NULL COMMENT '商家编码',
 PRIMARY KEY (`alipay_config_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT COMMENT='支付宝支付配置';

4、支付流水记录表

CREATE TABLE `py_pay_log` (
 `pay_log_id` varchar(18) NOT NULL COMMENT '支付流水ID',
 `pay_no` varchar(20) DEFAULT NULL COMMENT '支付订单号',
 `third_id` varchar(64) DEFAULT NULL COMMENT '微信支付宝订单ID',
 `money_type` varchar(20) DEFAULT NULL COMMENT '支付类型 0-微信 1-支付宝 2-会员卡 3-银联',
 `mq_topic` varchar(100) DEFAULT NULL COMMENT 'MQ主题',
 `mq_status` tinyint(3) DEFAULT NULL COMMENT 'MQ发送状态 0-待发送 1-已发送 2-发送失败',
 `mq_remark` varchar(100) DEFAULT NULL COMMENT 'MQ发送备注',
 `request` text COMMENT '发送数据',
 `response` text COMMENT '返回数据',
 `fee` decimal(14,2) DEFAULT NULL COMMENT '订单金额',
 `create_id` varchar(18) DEFAULT NULL COMMENT '创建人',
 `create_time` datetime DEFAULT NULL COMMENT '创建时间',
 `upd_id` varchar(18) DEFAULT NULL COMMENT '修改人',
 `upd_time` datetime DEFAULT NULL COMMENT '修改时间',
 `delete_flag` tinyint(1) NOT NULL DEFAULT '0' COMMENT '删除标志',
 `app_code` varchar(18) NOT NULL COMMENT '商家编码',
 `pay_config_json_str` longtext COMMENT '支付配置',
 PRIMARY KEY (`pay_log_id`) USING BTREE
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='支付流水';

5、支付失败记录表

CREATE TABLE `pay_fail_log` (
 `pay_id` varchar(18) NOT NULL,
 `json_date` varchar(500) DEFAULT NULL,
 `create_id` varchar(18) DEFAULT NULL,
 `create_time` datetime DEFAULT NULL,
 `update_id` varchar(18) DEFAULT NULL,
 `update_time` datetime DEFAULT NULL,
 `app_code` varchar(18) NOT NULL,
 `delete_flag` tinyint(1) DEFAULT '0' COMMENT '是否删除:0-否;1-是',
 PRIMARY KEY (`pay_id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT;

三、依赖引入

 <?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

 <parent>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-parent</artifactId>
  <version>2.1.3.RELEASE</version>
  <relativePath/> <!-- lookup parent from repository -->
 </parent>

 <modelVersion>4.0.0</modelVersion>
 <groupId>com.pay</groupId>
 <artifactId>qc-payment</artifactId>
 <version>2.3</version>
 <name>qc-payment</name>
 <!-- FIXME change it to the project's website -->
 <url>http://www.example.com</url>

 <properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <maven.compiler.source>1.7</maven.compiler.source>
  <maven.compiler.target>1.7</maven.compiler.target>
  <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
 </properties>

 <dependencies>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-web</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
  </dependency>
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
  </dependency>

  <!--z支付宝-->
  <dependency>
   <groupId>com.alipay.sdk</groupId>
   <artifactId>alipay-sdk-java</artifactId>
   <version>3.3.4.ALL</version>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-test</artifactId>
   <scope>test</scope>
  </dependency>

  <!--导入重试机制的坐标-->
  <dependency>
   <groupId>org.springframework.retry</groupId>
   <artifactId>spring-retry</artifactId>
  </dependency>

  <!--引入Hystrix依赖-->
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
  </dependency>

  <!--导入jwt相关依赖-->
  <dependency>
   <groupId>com.auth0</groupId>
   <artifactId>java-jwt</artifactId>
   <version>3.4.1</version>
  </dependency>

  <!-- StringUtil 类-->
  <dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-lang3</artifactId>
   <version>3.3.2</version>
  </dependency>

  <!-- ali 连接池 -->
  <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>druid</artifactId>
   <version>1.1.12</version>
  </dependency>

  <!-- mybatis -->
  <dependency>
   <groupId>org.mybatis.spring.boot</groupId>
   <artifactId>mybatis-spring-boot-starter</artifactId>
   <version>2.0.0</version>
  </dependency>

  <!-- mysql -->
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
  </dependency>
  <dependency>
   <groupId>javax.persistence</groupId>
   <artifactId>persistence-api</artifactId>
   <version>1.0.2</version>
  </dependency>
  <!-- https://mvnrepository.com/artifact/tk.mybatis/mapper -->
  <dependency>
   <groupId>tk.mybatis</groupId>
   <artifactId>mapper</artifactId>
   <version>4.0.3</version>
  </dependency>

  <!-- import lombok 可以省去getter setter toString等方法 -->
  <dependency>
   <groupId>org.projectlombok</groupId>
   <artifactId>lombok</artifactId>
   <scope>provided</scope>
  </dependency>

  <!-- commons-io -->
  <dependency>
   <groupId>commons-io</groupId>
   <artifactId>commons-io</artifactId>
   <version>2.4</version>
  </dependency>

  <!-- 引入easyqinyu-tools项目 -->
  <dependency>
   <groupId>com.pay</groupId>
   <artifactId>easyqinyu-tools</artifactId>
   <version>0.0.1-SNAPSHOT</version>
  </dependency>

  <!-- pagehelper分页插件 -->
  <dependency>
   <groupId>com.github.pagehelper</groupId>
   <artifactId>pagehelper</artifactId>
   <version>5.1.2</version>
  </dependency>

  <dependency>
   <groupId>com.github.pagehelper</groupId>
   <artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
   <version>1.2.3</version>
  </dependency>
  <dependency>
   <groupId>com.github.pagehelper</groupId>
   <artifactId>pagehelper-spring-boot-starter</artifactId>
   <version>1.2.3</version>
  </dependency>
  <dependency>
   <groupId>com.qcsoft</groupId>
   <artifactId>qc-swagger-ui</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <scope>compile</scope>
  </dependency>
  <dependency>
   <groupId>com.google.zxing</groupId>
   <artifactId>core</artifactId>
   <version>3.3.0</version>
  </dependency>

  <dependency>
   <groupId>com.alibaba</groupId>
   <artifactId>fastjson</artifactId>
   <version>RELEASE</version>
   <scope>compile</scope>
  </dependency>

  <dependency>
   <groupId>org.apache.httpcomponents</groupId>
   <artifactId>httpclient</artifactId>
   <version>${httpclient.version}</version>
  </dependency>

  <dependency>
   <groupId>com.google.code.gson</groupId>
   <artifactId>gson</artifactId>
   <version>2.8.2</version>
  </dependency>

  <!--阿里云信息发送-->
  <dependency>
   <groupId>com.aliyun</groupId>
   <artifactId>aliyun-java-sdk-core</artifactId>
   <version>3.3.1</version>
  </dependency>
  <dependency>
   <groupId>com.github.1991wangliang</groupId>
   <artifactId>aliyun-java-sdk-dysmsapi</artifactId>
   <version>1.0.0</version>
  </dependency>

  <dependency>
   <groupId>org.springframework.kafka</groupId>
   <artifactId>spring-kafka</artifactId>
  </dependency>

  <!-- xml解析-->
  <dependency>
   <groupId>com.thoughtworks.xstream</groupId>
   <artifactId>xstream</artifactId>
   <version>1.4.10</version>
  </dependency>

  <!-- dom4j -->
  <dependency>
   <groupId>org.dom4j</groupId>
   <artifactId>dom4j</artifactId>
   <version>2.1.1</version>
  </dependency>

   <dependency>
   <groupId>com.github.binarywang</groupId>
   <artifactId>weixin-java-common</artifactId>
   <version>3.3.4.B</version>
  </dependency>

  <dependency>
   <groupId>org.jodd</groupId>
   <artifactId>jodd-http</artifactId>
   <scope>compile</scope>
  </dependency>

  <dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-lang3</artifactId>
  </dependency>

  <!-- xml解析-->
  <dependency>
   <groupId>commons-beanutils</groupId>
   <artifactId>commons-beanutils</artifactId>
   <version>1.9.3</version>
  </dependency>

  <dependency>
   <groupId>jdom</groupId>
   <artifactId>jdom</artifactId>
   <version>1.0</version>
  </dependency>

  <dependency>
   <groupId>org.bouncycastle</groupId>
   <artifactId>bcpkix-jdk15on</artifactId>
   <version>1.59</version>
  </dependency>
  <dependency>
   <groupId>org.jodd</groupId>
   <artifactId>jodd-http</artifactId>
   <version>3.7.1</version>
  </dependency>
  <!--二维码工具-->
  <dependency>
   <groupId>com.google.zxing</groupId>
   <artifactId>core</artifactId>
   <version>3.2.1</version>
  </dependency>

  <dependency>
   <groupId>com.qcsoft</groupId>
   <artifactId>qc-commonbean</artifactId>
   <version>0.0.1-SNAPSHOT</version>
  </dependency>

  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-openfeign</artifactId>
  </dependency>
  <!-- xxl-job-core -->
  <dependency>
   <groupId>com.xuxueli</groupId>
   <artifactId>xxl-job-core</artifactId>
   <version>2.0.1</version>
  </dependency>
  <!--线路跟踪-->
  <dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-netflix-hystrix-dashboard</artifactId>
  </dependency>

  <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-actuator</artifactId>
  </dependency>
 </dependencies>

 <dependencyManagement>
  <dependencies>
   <dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-dependencies</artifactId>
    <version>${spring-cloud.version}</version>
    <type>pom</type>
    <scope>import</scope>
   </dependency>
  </dependencies>
 </dependencyManagement>
 <build>
  <finalName>qc-payment</finalName>
  <plugins>
   <plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
   </plugin>
   <plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-compiler-plugin</artifactId>
    <configuration>
     <source>8</source>
     <target>8</target>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>

四、程序实现

1、Controller

package com.qcsoft.payment.controller;

import com.alibaba.fastjson.JSON;
import com.qcsoft.commonbean.bean.common.SwaggerConstant;
import com.qcsoft.commonbean.bean.payment.common.PrePayResult;
import com.qcsoft.commonbean.bean.payment.wx.bean.PreOrderParam;
import com.qcsoft.commonbean.bean.payment.wx.exception.WxPayException;
import com.qcsoft.commonbean.bean.payment.wx.util.QrcodeUtils;
import com.qcsoft.easyqinyutools.message.ReturnMsg;
import com.qcsoft.payment.controller.commom.BaseController;
import com.qcsoft.payment.service.PayService;
import com.qcsoft.swaggerui.anno.ApiTag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;

/**
 * @Description: 支付控制中心
 * @Author:
 * @Date:
 */
@RestController
@RequestMapping("/v2")
public class PayController extends BaseController {
 private static final Logger logger = LoggerFactory.getLogger(PayController.class);

 @Autowired
 private PayService payService;

 //private final AtomicInteger tick = new AtomicInteger();

 /**
  * 统一预支付接口(统一下单)
  *
  * @param orderParam {dh:订单号,payType:支付方式(WXPAY、ALIPAY、MEMBER_CARD、ACCOUNT),appCode:商家编码,
  *     tradeType:支付方式类型(NATIVE、JSAPI、APP、MWEB),openId:支付方式为JSAPI必传,reqId:请求的真实IP地址
  *     ,redirectUrl:支付成功返回地址,appCode:商家编码,outTradeNo:商务订单号,timeExpire,订单到期时间
  *     ,totalAmount:订单金额,body:商品简单描述,subject:订单标题,quitUrl:中途退出}
  * @param token  token
  * @return (微信公众号 / 小程序返回一串json字符串 : { appId :, timeStamp :, nonceStr : , info_package :, paySign :, signType : }, 前端使用微信sdk调用微信支付)
  * (微信扫码支付返回一个连接,前端需要把连接生成一个二维码使用,连接如:weixin://123.123.com/***)
  * (微信H5--返回一个连接,前端需要把连接生成一个连接前端直接跳转该连接使用,使用如:location.href='weixin://123.123.com/***')
  * (支付宝扫码支付--返回一个连接,前端直接跳转到该连接使用,使用如:location.href='weixin://123.123.com/***')
  * (支付宝扫码H5--返回一个页面form表单,前端需要把这个form表单渲染到h5页面上,使用如:$("#view).html(alipayForm)')
  * (支付宝app支付--返回一个参数包,如sdk-version=**&service=**&&service=**&&body=**&&out_trade_no=**&,前端使用支付宝sdk调用app支付')
  * {
  * wxNativeUrl:微信扫码地址
  * wxAppPackage:app支付sdk参数包
  * wxJsapiPackage:公众号支付包
  * wxH5Url:h5支付url
  * wxScanUrl:wxScanUrl
  * alipayScanUrl:支付宝扫码支付url
  * alipayH5Form:支付宝h5支付表单
  * alipayAppParamStr:支付宝app支付参数支付串
  * }
  * @throws WxPayException
  */
 @RequestMapping(value = "/prePayOrder")
 @ApiTag(SwaggerConstant.PAYMENT_001)
 public ReturnMsg prePayOrder(@RequestBody PreOrderParam orderParam) {
  String metadata = JSON.toJSONString(orderParam);
  logger.info("PayController.prePayOrder----\n正在请求支付....,参数为>>>>{}", metadata);
  ReturnMsg ret = new ReturnMsg();
  //预下单
  PrePayResult prePayResult = this.payService.payPreOrder(orderParam);
  ret.setData(prePayResult);
  return ret.success("下单成功,订单有效期为" + orderParam.getTimeExpire());
 }

 /**
  * 生成支付二维码
  *
  * @param url 二维码地址
  * @throws IOException
  */
 @RequestMapping(value = "/createQrCode/url")
 @ApiTag(SwaggerConstant.PAYMENT_001)
 public void createQrCode(@PathVariable String url, HttpServletRequest request, HttpServletResponse response) throws IOException {
  String queryString = request.getQueryString();
  BufferedImage bufferedImage = QrcodeUtils.encode(url+queryString, 500, 500, null, null);
  BufferedImage grayImage = QrcodeUtils.grayImage(bufferedImage);
  ImageIO.write(grayImage, "png", response.getOutputStream());
 }

}

2、PayService

package com.qcsoft.payment.service.impl;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayClient;
import com.qcsoft.commonbean.bean.common.KafkaConstant;
import com.qcsoft.commonbean.bean.payment.ChinaumsConfig;
import com.qcsoft.commonbean.bean.payment.PayLog;
import com.qcsoft.commonbean.bean.payment.common.PayConstant;
import com.qcsoft.commonbean.bean.payment.common.PrePayResult;
import com.qcsoft.commonbean.bean.payment.common.WxPrePayResultPackage;
import com.qcsoft.commonbean.bean.payment.wx.bean.PreOrderParam;
import com.qcsoft.commonbean.bean.payment.wx.bean.request.WxPayUnifiedOrderRequest;
import com.qcsoft.commonbean.bean.payment.wx.bean.result.WxPayUnifiedOrderResult;
import com.qcsoft.commonbean.bean.payment.wx.config.WxPayConfig;
import com.qcsoft.easyqinyutools.utils.JsonUtil;
import com.qcsoft.payment.commns.exception.BaseException;
import com.qcsoft.payment.commns.exception.QYError;
import com.qcsoft.payment.commns.utils.UserUtil;
import com.qcsoft.payment.commns.utils.weixin.PackageUtil;
import com.qcsoft.payment.service.*;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.SortedMap;
import java.util.TreeMap;

@Service
public class PayServiceImpl implements PayService {
 public Logger logger = LoggerFactory.getLogger(getClass());
 @Autowired
 private AliPayService aliPayService;
 @Autowired
 private WxPayService wxPayService;
 @Autowired
 private ChinaumsPayService chinaumsPayService;

 @Autowired
 private UserUtil userUtil;
 @Autowired
 private PayLogService payLogService;

 @Override
 public PrePayResult payPreOrder(PreOrderParam orderParam) throws BaseException {
  logger.info("PayServiceImpl.payPreOrder---->>>>\n准备下单.....,当前用户订单号[{}]", orderParam.getDh());
  String temp = "";
  PrePayResult payResult = new PrePayResult();
  //校验支付方式
  String payType = orderParam.getPayType();
  if (!StringUtils.isNotBlank(payType))
   throw new BaseException(QYError.msg("请选择支付方式!"));
  if (PayConstant.WXPAY.toString().equals(payType)) {
   //查询商家配置信息
   WxPayConfig config;
   if (orderParam.getTradeType().equals(PayConstant.WECHATAPP.toString())) {
    config = wxPayService.getWxMinPayConfigByAppCode(orderParam.getAppCode(), orderParam.getSysSoftwareCode());
   } else {
    config = wxPayService.getWxPayConfigByAppCode(orderParam.getAppCode(), orderParam.getBmCode());
   }
   //设置预支付请求参数
   if (orderParam.getTradeType().equals(PayConstant.WECHATAPP.toString())) {
    orderParam.setTradeType(PayConstant.JSAPI.toString());
   }
   WxPayUnifiedOrderRequest requestParam = wxPayService.packRequestParam(orderParam);
   //开始请求预支付接口
   WxPayUnifiedOrderResult result = wxPayService.wxPrePay(config, requestParam);
   //根据支付类型设置值
   WxPrePayResultPackage resultPackage = null;
   if (orderParam.getTradeType().equals(PayConstant.APP.toString()) || orderParam.getTradeType().equals(PayConstant.JSAPI.toString()) || orderParam.getTradeType().equals(PayConstant.WECHATAPP.toString())) {
    resultPackage = new WxPrePayResultPackage();
    String timeMillis = PackageUtil.getTimeStamp();
    String nonceString = PackageUtil.getNonceStr();
    resultPackage.setAppId(result.getAppid());
    resultPackage.setNonceStr(nonceString);
    resultPackage.setTimeStamp(timeMillis);
    resultPackage.setPrePayId(result.getPrepayId());
    resultPackage.setInfoPackage("prepay_id=" + result.getPrepayId());
    resultPackage.setSignType(requestParam.getSignType());
    if (orderParam.getTradeType().equals(PayConstant.APP.toString())) {
     SortedMap<String, String> returnData = PackageUtil.getReturnData(resultPackage, config.getMchId());
     String paysign = PackageUtil.createSign(returnData, config.getMchKey());
     resultPackage.setPaySign(paysign);
     resultPackage.setInfoPackage(returnData.get("package"));
     payResult.setWxAppPackage(resultPackage);
    } else {
     SortedMap<String, String> paypackageParams = new TreeMap<String, String>();
     paypackageParams.put("appId", result.getAppid()); // appid
     paypackageParams.put("timeStamp", timeMillis); //
     paypackageParams.put("nonceStr", nonceString); //
     paypackageParams.put("package", resultPackage.getInfoPackage()); //
     paypackageParams.put("signType", resultPackage.getSignType());
     String paysign = PackageUtil.createSign(paypackageParams, config.getMchKey());
     resultPackage.setPaySign(paysign);
     payResult.setWxJsapiPackage(resultPackage);
    }
   } else if (orderParam.getTradeType().equals(PayConstant.MWEB.toString())) {
    payResult.setWxH5Url(result.getMwebUrl() + " &redirect_url=" + orderParam.getRedirectUrl());
   } else if (orderParam.getTradeType().equals(PayConstant.NATIVE.toString())) {
    payResult.setWxNativeUrl(result.getCodeURL());
   }
   //添加支付日志
   insertPayLog(orderParam, KafkaConstant.PAYMENT_NOTIFY_001.getTopic(), "0", resultPackage, JSON.toJSONString(config));
   return payResult;
  } else if (PayConstant.ALIPAY.toString().equals(payType)) {
   AlipayClient client = aliPayService.getAlipayClientByAppCode(orderParam.getAppCode());
   if (orderParam.getTradeType().equals(PayConstant.APP.toString())) {
    temp = aliPayService.aliAppPay(client, orderParam);
    payResult.setAlipayAppParamStr(temp);
   } else if (orderParam.getTradeType().equals(PayConstant.MWEB.toString())) {
    temp = aliPayService.aliH5Pay(client, orderParam);
    payResult.setAlipayH5Form(temp);
   } else if (orderParam.getTradeType().equals(PayConstant.NATIVE.toString())) {
    temp = aliPayService.aliScanPay(client, orderParam);
    payResult.setAlipayScanUrl(temp);
   }
   //添加支付日志
   insertPayLog(orderParam, KafkaConstant.PAYMENT_NOTIFY_001.getTopic(), "1", orderParam, JSON.toJSONString(aliPayService.getConfigByAppCode(orderParam.getAppCode())));
   return payResult;
  } else if (PayConstant.WECHATAPP.toString().equals(payType)) {

  } else if (PayConstant.CHINAUMS.toString().equals(payType)) { //银联pos支付
   //查询商家配置信息
   ChinaumsConfig config;
   config = chinaumsPayService.getPayConfigByAppCode(orderParam.getAppCode(), orderParam.getBmCode());
   if (orderParam.getTradeType().equals(PayConstant.CODE_SCAN.toString())) {
    chinaumsPayService.codeScanPay(config, orderParam);
   }
   //添加支付日志
   insertPayLog(orderParam, KafkaConstant.PAYMENT_NOTIFY_001.getTopic(), "3", orderParam, JSON.toJSONString(config));
   return payResult;
  }
  logger.warn("用户未选择正确的支付方式");
  throw new BaseException(QYError.msg("请选择正确的支付方式!"));

 }

 public void insertPayLog(PreOrderParam orderParam, String topic, String moneyType, Object obj, String payConfigJsonStr) {
  PayLog payLog = new PayLog();
  payLog.setAppCode(orderParam.getAppCode());
  payLog.setPayLogId(orderParam.getDh());
  payLog.setMoneyType(moneyType);
  payLog.setMqTopic(topic);
  payLog.setMqStatus(0);
  payLog.setPayNo(orderParam.getDh());
  payLog.setRequest(JsonUtil.toJSon(obj));
  payLog.setFee(orderParam.getTotalAmount());
  payLog.setPayConfigJsonStr(payConfigJsonStr);
  payLogService.saveOrUpdate(payLog);
 }
}

3、AliPayService

package com.qcsoft.payment.service;

import com.alipay.api.AlipayClient;
import com.alipay.api.request.AlipayTradeRefundRequest;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.alipay.api.response.AlipayTradeRefundResponse;
import com.qcsoft.commonbean.bean.payment.AlipayConfig;
import com.qcsoft.commonbean.bean.payment.alipay.AliPayRefundParam;
import com.qcsoft.commonbean.bean.payment.common.PayNotify;
import com.qcsoft.commonbean.bean.payment.wx.bean.PreOrderParam;
import com.qcsoft.easyqinyutools.message.ReturnMsg;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

public interface AliPayService {
 /**
  *
  * @param alipayClient
  * @param orderParam
  * @param
  * @return
  */
 String aliAppPay(AlipayClient alipayClient,PreOrderParam orderParam);

 String aliScanPay(AlipayClient alipayClient, PreOrderParam orderParam);

 String aliH5Pay(AlipayClient alipayClient, PreOrderParam orderParam);

 void aliPayNotify(PayNotify params);

 boolean rsaCheckV1(HttpServletRequest request,String appCode);

 AlipayTradeQueryResponse aliPayOrderQuery(AlipayClient alipayClient, String out_trade_no);

 AlipayClient getAlipayClientByAppCode(String appCode);

 AlipayConfig getConfigByAppCode(String appCode);

 /**
  * 支付宝退款接口
  * @param aliPayRefundParam
  * @return
  */
 ReturnMsg aliPayRefund(AliPayRefundParam aliPayRefundParam);

}

AliPayServiceImpl

package com.qcsoft.payment.service.impl;

import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradeAppPayModel;
import com.alipay.api.domain.AlipayTradePrecreateModel;
import com.alipay.api.domain.AlipayTradeRefundModel;
import com.alipay.api.domain.AlipayTradeWapPayModel;
import com.alipay.api.internal.util.AlipaySignature;
import com.alipay.api.request.*;
import com.alipay.api.response.*;
import com.qcsoft.commonbean.bean.payment.AlipayConfig;
import com.qcsoft.commonbean.bean.payment.alipay.AliPayRefundParam;
import com.qcsoft.commonbean.bean.payment.common.PayConstant;
import com.qcsoft.commonbean.bean.payment.common.PayNotify;
import com.qcsoft.commonbean.bean.payment.wx.bean.PreOrderParam;
import com.qcsoft.easyqinyutools.message.ReturnMsg;
import com.qcsoft.payment.commns.exception.BaseException;
import com.qcsoft.payment.commns.exception.QYError;
import com.qcsoft.payment.commns.utils.DateUtils;
import com.qcsoft.payment.commns.utils.RedisUtil;
import com.qcsoft.payment.service.AliPayService;
import com.qcsoft.payment.service.AlipayConfigService;
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 javax.servlet.http.HttpServletRequest;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 支付宝相关业务实现类
 */
@Service
public class AliPayServiceImpl implements AliPayService {
 private static final Logger logger = LoggerFactory.getLogger(AliPayServiceImpl.class);
 @Autowired
 private AlipayConfigService alipayConfigService;
 @Autowired
 private RedisUtil redisUtil;
 @Value("${payNotifyUrl}")
 private String notifyUrl;

 /**
  * APP支付
  * 参数加签
  */
 @Override
 public String aliAppPay(AlipayClient alipayClient, PreOrderParam orderParam) {
  logger.info("AliPayServiceImpl.aliAppPay--->>>\n支付宝预支付开始,\n支付类型为:APP支付,\n订单号为:{}", orderParam.getDh());
  Date date = new Date();
  AlipayTradeAppPayResponse response = null;
  // 发起App支付请求
  AlipayTradeAppPayModel model = new AlipayTradeAppPayModel();
  // 订单描述
  model.setBody(orderParam.getBody());
  // 订单标题
  model.setSubject(orderParam.getSubject());
  // 商户订单号 就是商户后台生成的订单号
  model.setOutTradeNo(orderParam.getOutTradeNo());
  // 该笔订单允许的最晚付款时间,逾期将关闭交易。取值范围:1m~15d。m-分钟,h-小时,d-天,1c-当天 (屁股后面的字母一定要带,不然报错)
  model.setTimeoutExpress(DateUtils.toString(date, orderParam.getTimeExpire()));
  // 订单总金额 ,默认单位为元,精确到小数点后两位,取值范围[0.01,100000000]
  model.setTotalAmount(orderParam.getTotalAmount().toString());
  // 销售产品码
  model.setProductCode(PayConstant.ALIPAY_QUICK_WAP_WAY.getKey());
  AlipayTradeAppPayRequest request = new AlipayTradeAppPayRequest();
  request.setBizModel(model);
  // request.setNotifyUrl("商户外网可以访问的异步地址,不写就是不回调");
  request.setNotifyUrl(notifyUrl + PayConstant.ALIPAY_05.getKey() + "/" + orderParam.getAppCode() + "/" + orderParam.getDh());
  //支付成功返回地址
  request.setReturnUrl(orderParam.getRedirectUrl());
  // 通过api的方法请求阿里接口获得反馈
  logger.info("AliPayServiceImpl.aliAppPay----->\n支付宝预支付接口请求信息:{}", JSON.toJSONString(request));
  try {
   response = alipayClient.sdkExecute(request);
  } catch (AlipayApiException e) {
   logger.info("AliPayServiceImpl.aliAppPay----->\n支付宝预下单失败,\n本次下单的订单号为:{},\n相关报错信息为:{}", orderParam.getDh(), response, e);
   throw new BaseException(QYError.msg("调用支付宝预下单接口失败![" + e.getMessage() + "]"));
  }

  logger.info("AliPayServiceImpl.aliAppPay----->\n支付宝预支付接口返回信息:{}", JSON.toJSONString(response));
  if (response.isSuccess()) {
   logger.info("AliPayServiceImpl.aliAppPay----->\n支付宝预下单成功,\n本次下单的订单号为:{},商户订单号为:{}", orderParam.getDh(), orderParam.getOutTradeNo());
  } else {
   logger.error("AliPayServiceImpl.aliAppPay----->\n调用支付宝预下单接口失败!\n具体信息为:\n{}", response.getBody());
   throw new BaseException(QYError.msg("调用支付宝预下单接口失败![" + response.getMsg() + "]"));
  }
  return response.getBody();
 }

 /**
  * 扫码支付
  * 参数加签
  */
 @Override
 public String aliScanPay(AlipayClient alipayClient, PreOrderParam orderParam) {
  logger.info("AliPayServiceImpl.aliScanPay--->>>\n支付宝预支付开始,\n支付类型为:扫码支付,\n订单号:{}", orderParam.getDh());
  /* AlipayTradePagePayResponse response = null;*/
  AlipayTradePrecreateResponse response = null;
  Date date = new Date();
  /**
   * 设置请求model参数(body、subject、outTradeNo、totalAmount、timeoutExpress;必传)
   */
  AlipayTradePrecreateModel model = new AlipayTradePrecreateModel();
  // 订单描述
  model.setBody(orderParam.getBody());
  // 订单标题
  model.setSubject(orderParam.getBody());
  // 商户订单号 就是商户后台生成的订单号
  model.setOutTradeNo(orderParam.getOutTradeNo());
  // 该笔订单允许的最晚付款时间,逾期将关闭交易。取值范围:1m~15d。m-分钟,h-小时,d-天,1c-当天 (屁股后面的字母一定要带,不然报错)
  model.setTimeoutExpress(orderParam.getTimeExpire());
  //model.setSellerId(DateUtils.toString(date, "yyyyMMddHHmmssSSS"));
  // 订单总金额 ,默认单位为元,精确到小数点后两位,取值范围[0.01,100000000]
  model.setTotalAmount(orderParam.getTotalAmount().toString());
  //model.setProductCode(PayConstant.ALIPAY_FAST_INSTANT_TRADE_PAY.getKey());
  // 选填
  /**
   * 创建支付宝扫码支付请求接口类,设置相关请求处理信息,准备请求下单
   */
  AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
  request.setBizModel(model);
  // request.setNotifyUrl("商户外网可以访问的异步地址,不写就是不回调");
  request.setNotifyUrl(notifyUrl + PayConstant.ALIPAY_05.getKey() + "/" + orderParam.getAppCode() + "/" + orderParam.getDh());

  //支付成功返回地址
  request.setReturnUrl(orderParam.getRedirectUrl());
  // 通过api的方法请求阿里接口获得反馈 alipayClient.pageExecute(request)
  try {
   logger.info("支付宝扫码付请求报文:{}",JSON.toJSONString(request));
   response = alipayClient.execute(request)/**/;
  } catch (AlipayApiException e) {
   logger.info("AliPayServiceImpl.aliScanPay----->\n支付宝预下单失败,\n本次下单的订单号:{},\n相关报错信息为:{}", orderParam.getDh(), response, e);
   throw new BaseException(QYError.msg("调用支付宝预下单接口失败![" + e.getMessage() + "]"));
  }catch (Exception e){
   e.printStackTrace();
   throw new BaseException(QYError.msg("调用支付宝预下单接口失败![" + e.getMessage() + "]"));
  }

  //打印返回信息
  logger.info("AliPayServiceImpl.aliScanPay----->\n支付宝预支付接口返回信息:{}", JSON.toJSONString(response));
  //判断预下单返回状态
  if (response.isSuccess()) {
   logger.info("AliPayServiceImpl.aliScanPay----->\n支付宝预下单成功,\n本次下单的订单号:{},商务订单号为", orderParam.getDh(), orderParam.getOutTradeNo());
  } else {
   logger.error("AliPayServiceImpl.aliScanPay----->\n调用支付宝预下单接口失败!\n具体信息:\n{}", response.getBody());
   throw new BaseException(QYError.msg("调用支付宝预下单接口失败![" + response.getMsg() + "]"));
  }

  return response.getQrCode();
 }

 /**
  * H5支付
  * 参数加签
  */
 @Override
 public String aliH5Pay(AlipayClient alipayClient, PreOrderParam orderParam) {
  logger.info("AliPayServiceImpl.aliH5Pay--->>>\n支付宝预支付开始,\n支付类型为:H5支付,\n订单号:{}", orderParam.getDh());
  String form = "";
  AlipayTradeWapPayResponse response = null;
  /**
   * 封装请求支付信息
   */
  AlipayTradeWapPayModel payModel = new AlipayTradeWapPayModel();
  payModel.setOutTradeNo(orderParam.getOutTradeNo());
  payModel.setSubject(orderParam.getBody());
  payModel.setTotalAmount(orderParam.getTotalAmount().toString());
  payModel.setBody(orderParam.getBody());
  payModel.setTimeoutExpress(orderParam.getTimeExpire());
  payModel.setProductCode(PayConstant.ALIPAY_QUICK_WAP_WAY.getKey());
  payModel.setQuitUrl(URLEncoder.encode(orderParam.getQuitUrl()));
  /**
   * 创建支付宝H5支付请求接口类,设置相关请求处理信息,准备请求下单
   */
  AlipayTradeWapPayRequest alipay_request = new AlipayTradeWapPayRequest();
  alipay_request.setBizModel(payModel);
  // 设置异步通知地址
  logger.info("通知地址{}", notifyUrl + PayConstant.ALIPAY_05.getKey() + "/" + orderParam.getAppCode() + "/" + orderParam.getDh());
  alipay_request.setNotifyUrl(notifyUrl + PayConstant.ALIPAY_05.getKey() + "/" + orderParam.getAppCode() + "/" + orderParam.getDh());
  // 设置同步地址
  alipay_request.setReturnUrl(URLEncoder.encode(orderParam.getRedirectUrl()));
  // 调用SDK生成表单
  try {
   response = alipayClient.pageExecute(alipay_request);
   if (response.isSuccess()) {
    form = response.getBody();
    logger.info("AliPayServiceImpl.aliH5Pay----->>>>\n支付宝预支付接口返回信息:{}", JSON.toJSONString(response));
   } else {
    logger.info("AliPayServiceImpl.aliH5Pay----->>>>\n支付宝预支付接口返回信息:{}", JSON.toJSONString(response));
    throw new BaseException(QYError.msg("支付宝"));
   }

  } catch (AlipayApiException e) {
   logger.info("AliPayServiceImpl.aliH5Pay----->>>>\n支付宝预支付接口返回信息:{}", JSON.toJSONString(response));
   throw new BaseException(QYError.msg("调用支付宝预下单接口失败![" + e.getMessage() + "]"));
  }
  logger.info("AliPayServiceImpl.aliH5Pay----->>>>\n支付宝预支付接口返回信息:{}", JSON.toJSONString(response));
  /**
   * 打印日志,并返回链接
   */
  return form;
 }

 /**
  * 支付宝通知
  *
  * @param params
  */
 @Override
 public void aliPayNotify(PayNotify params) {
  logger.info("AliPayServiceImpl.appAliPayNotify-\n阿里服务器消费手机回调参数获取和参数判断-------》》》");
  /**
   * 打印信息
   */
  //校验支付金额
  /*ReturnMsg returnMsg = restTemplate.postForEntity(params.getNoOrderAmountUrl(), params, ReturnMsg.class).getBody();
  if (!returnMsg.isSuccess()) {
   logger.info("WxPayController.wxPayNotify-->\n校验订单金额失败,请核实商务订单号:{}", params);
   throw new BaseException(QYError.msg("校验订单金额失败!商务订单号[" + params.getOutTradeNo() + "]"));
  }*/
  /**
   * 判断支付状态
   */
  if (params.getTradeStatus().equals("TRADE_SUCCESS")) {
   logger.info("AliPayServiceImpl.appAliPayNotify----\n支付宝支付成功!商务订单号[{}]----->>", params.getOutTradeNo(), JSON.toJSONString(params));
  } else {
   logger.error("AliPayServiceImpl.appAliPayNotify---\n支付宝支付失败!商务订单号[{}]---->>", params.getOutTradeNo());
   throw new BaseException(QYError.msg("支付宝支付失败!商务订单号[" + params.getOutTradeNo() + "]"));
  }
 }

 @Override
 public boolean rsaCheckV1(HttpServletRequest request, String appCode) {
  //获取支付宝GET过来反馈信息
  Map<String, String> params = new HashMap<String, String>();
  // 签名验证(对支付宝返回的数据验证,确定是支付宝返回的)
  boolean result = false;
  try {
   // 从支付宝回调的request域中取值
   Map<String, String[]> requestParams = request.getParameterMap();
   for (Iterator<String> iter = requestParams.keySet().iterator(); iter.hasNext(); ) {
    String name = iter.next();
    String[] values = (String[]) requestParams.get(name);
    String valueStr = "";
    for (int i = 0; i < values.length; i++) {
     valueStr = (i == values.length - 1) ? valueStr + values[i] : valueStr + values[i] + ",";
    }
    params.put(name, valueStr);
   }
   logger.info("AliPayServiceImpl.appAliPayNotify--\n支付宝通知数据包----->>>>>>>>>>>>>>>{}", JSON.toJSONString(params));
   // 商家编码

   AlipayConfig alipayConfig = getConfigByAppCode(appCode);
   result = AlipaySignature.rsaCheckV1(params, alipayConfig.getAliPublicKey(),
     PayConstant.ALIPAY_CHARSET.getKey(), PayConstant.ALIPAY_SIGN_TYPE_RSA2.getKey());
  } catch (Exception e) {
   logger.error("AliPayServiceImpl.appAliPayNotify---\n校验签名失败!商务订单号[{}]---->>", JSON.toJSONString(request), e);
   throw new BaseException(QYError.msg("支付宝支付校验签名失败!"));
  }
  return result;
 }

 /**
  * 根据商务订单号查询查询支付信息
  *
  * @param alipayClient
  * @param out_trade_no
  * @return
  */
 @Override
 public AlipayTradeQueryResponse aliPayOrderQuery(AlipayClient alipayClient, String out_trade_no) {
  logger.info("AliPayServiceImpl.aliPayOrderQuery----\n根据商务订单号查询支付宝订单信息,\n商务订单号:[{}]----》》》", out_trade_no);
  AlipayTradeQueryResponse response = null;
  AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
  //设置查询参数
  Map<String, String> bizModel = new HashMap<>();
  bizModel.put("out_trade_no", out_trade_no);
  request.setBizContent(JSON.toJSONString(bizModel));
  //开始查询
  try {
   response = alipayClient.execute(request);
  } catch (AlipayApiException e) {
   logger.error("AliPayServiceImpl.aliPayOrderQuery--->>\n支付宝订单查询失败,\n商务订单号:[{}]", out_trade_no);
   throw new BaseException(QYError.msg("订单号[" + out_trade_no + "],订单支付失败,状态码为[" + response.getTradeStatus() + "]"));
  }

  logger.info("AliPayServiceImpl.aliPayOrderQuery---->>\n查询支付宝订单信息,\n商务订单号:[{}],\n返回信息:{}", out_trade_no, JSON.toJSONString(response));
  //查询状态处理
  if (response.isSuccess()) {
   logger.info("AliPayServiceImpl.aliPayOrderQuery--->>\n支付宝订单查询成功,\n商务订单号:[{}]", out_trade_no);
  } else {
   logger.error("AliPayServiceImpl.aliPayOrderQuery--->>\n支付宝订单查询失败,\n商务订单号:[{}]", out_trade_no);
   throw new BaseException(QYError.msg("订单号[" + out_trade_no + "],订单支付失败,状态码为[" + response.getTradeStatus() + "]"));
  }

  return response;
 }

 /**
  * 根据商家编码设置alipay配置信息
  *
  * @param appCode 商家编码
  * @return
  */
 @Override
 public AlipayClient getAlipayClientByAppCode(String appCode) {
  logger.info("AliPayServiceImpl.getAlipayClientByAppCode--->>\n获取支付宝配置信息:[{}]", appCode);
  AlipayConfig alipayConfig = null;
  /**
   * 获取商家支付宝配置信息(优先级:1-redis,2-mysql,3-用户系统)
   */
  try {
   alipayConfig = getConfigByAppCode(appCode);
   if (alipayConfig == null) {
    logger.info("AliPayServiceImpl.getAlipayClientByAppCode--->>\n获取支付宝支付配置信息失败:[{}]", appCode);
    logger.info("AliPayServiceImpl.getAlipayClientByAppCode--->>\n获取支付宝支付配置信息失败:[{}{}]", appCode,alipayConfig);
    throw new BaseException(QYError.msg("获取支付宝支付配置信息失败!"));
   }
  } catch (Exception e) {
   logger.error("AliPayServiceImpl.getAlipayClientByAppCode--->>\n获取支付宝配置信息失败:[{}]", e);
   throw new BaseException(QYError.msg("获取支付宝配置信息失败!"));
  }
  AlipayClient alipayClient = new DefaultAlipayClient(
    PayConstant.ALIPAY_01.getKey(),
    alipayConfig.getAliAppid(),
    alipayConfig.getAliPrivateKey(),
    PayConstant.ALIPAY_FORMAT.getKey(),
    PayConstant.ALIPAY_CHARSET.getKey(),
    alipayConfig.getAliPublicKey(),
    PayConstant.ALIPAY_SIGN_TYPE_RSA2.getKey());
  return alipayClient;
 }

 //获取商家支付宝的配置信息
 public AlipayConfig getConfigByAppCode(String appCode) {
  AlipayConfig alipayConfig = alipayConfigService.selectByAppCode(appCode);
  return alipayConfig;
 }

 /**
  * 支付宝退款接口
  * @param aliPayRefundParam
  * @return
  */
 public ReturnMsg aliPayRefund(AliPayRefundParam aliPayRefundParam){
  ReturnMsg returnMsg=new ReturnMsg();

  String appCode=aliPayRefundParam.getAppCode();
  AlipayClient alipayClient = getAlipayClientByAppCode(appCode);

  AlipayTradeRefundResponse response = null;
  AlipayTradeRefundModel model = new AlipayTradeRefundModel();
  //订单支付时传入的商户订单号,不能和 trade_no同时为空。
  model.setOutTradeNo(aliPayRefundParam.getOutTradeNo());
  //支付宝交易号,和商户订单号不能同时为空
  //model.setTradeNo(aliPayRefundParam.getTradeNo());
  //标识一次退款请求,同一笔交易多次退款需要保证唯一,如需部分退款,则此参数必传。
  model.setOutRequestNo(aliPayRefundParam.getOutRequestNo());
  //	需要退款的金额,该金额不能大于订单金额,单位为元,支持两位小数
  model.setRefundAmount(aliPayRefundParam.getRefundAmount());
  //退款的原因说明
  model.setRefundReason(aliPayRefundParam.getRefundReason());

  /**
   * 创建支付宝扫码支付请求接口类,设置相关请求处理信息,准备请求下单
   */
  AlipayTradeRefundRequest request = new AlipayTradeRefundRequest();
  request.setBizModel(model);
  //商户外网可以访问的异步地址,不写就是不回调
  request.setNotifyUrl(notifyUrl + PayConstant.ALIPAY_05.getKey() + "/" + aliPayRefundParam.getAppCode() + "/" + aliPayRefundParam.getDh());
  //支付成功返回地址
  request.setReturnUrl(aliPayRefundParam.getRedirectUrl());

  // 通过api的方法请求阿里接口获得反馈 alipayClient.pageExecute(request)
  try {
   logger.info("支付宝退款请求完整报文:{}",JSON.toJSONString(request));
   response = alipayClient.execute(request)/**/;
  } catch (AlipayApiException e) {
   logger.info("支付宝退款失败,\n本次下单的订单号:{},\n相关报错信息为:{}", aliPayRefundParam.getDh(), response, e);
   throw new BaseException(QYError.msg("调用支付宝预退款失败![" + e.getMessage() + "]"));
  }catch (Exception e){
   e.printStackTrace();
   throw new BaseException(QYError.msg("支付宝预退款失败![" + e.getMessage() + "]"));
  }

  //打印返回信息
  logger.info("支付宝退款失败:{}", JSON.toJSONString(response));
  //判断预下单返回状态
  if (response.isSuccess()&&"Y".equals(response.getFundChange())) {
   logger.info("AliPayServiceImpl.aliScanPay----->\n支付宝退款成功,\n本次下单的订单号:{},商务订单号为,{}", aliPayRefundParam.getDh(), aliPayRefundParam.getOutTradeNo());
   return returnMsg.setData("退款成功");

  } else {
   logger.error("AliPayServiceImpl.aliScanPay----->\n调用支付宝退款接口失败!\n具体信息:\n{}", response.getBody());
   throw new BaseException(QYError.msg("调用支付宝退款接口失败![" + response.getBody() + "]"));
  }
 }

}

4、WxPayService

package com.qcsoft.payment.service;

import com.qcsoft.commonbean.bean.payment.WxMinConfig;
import com.qcsoft.commonbean.bean.payment.wx.bean.PreOrderParam;
import com.qcsoft.commonbean.bean.payment.wx.bean.WxRefund;
import com.qcsoft.commonbean.bean.payment.wx.bean.entpay.EntPayParam;
import com.qcsoft.commonbean.bean.payment.wx.bean.entpay.EntPayResult;
import com.qcsoft.commonbean.bean.payment.wx.bean.request.WxPayUnifiedOrderRequest;
import com.qcsoft.commonbean.bean.payment.wx.bean.result.BaseWxPayResult;
import com.qcsoft.commonbean.bean.payment.wx.bean.result.WxPayOrderQueryResult;
import com.qcsoft.commonbean.bean.payment.wx.bean.result.WxPayUnifiedOrderResult;
import com.qcsoft.commonbean.bean.payment.wx.config.WxPayConfig;
import com.qcsoft.commonbean.bean.payment.wx.exception.WxPayException;

/**
 * 微信预支付
 * @param appId 应用ID(微信公众号)
 * @param mchId 商务号ID
 * @param mchKey 商务号密钥
 * @param notifyUrl 异步通知地址
 * @param signType 签名类型
 * @param body 描述
 * @param totalFee 金额
 * @param spbillCreateIp app和h5支付需要用户的真实请求地址
 * @param tradeType
 * @param outTradeNo
 * @return
 */
public interface WxPayService {
 /**
  *
  * @param config {appId:应用ID,mchId:商务号ID,mchKey:商务号密钥,notifyUrl:异步通知地址,signType:签名类型}
  * @param request {body:订单描述,totalFee:金额,
  *    spbillCreateIp:app和h5支付需要用户的真实请求地址Native支付填调用微信支付API的机器IP,
  *    tradeType:SAPI--公众号支付、NATIVE--原生扫码支付、APP--app支付,统一下单接口trade_type的传参可参考这里,
  *    outTradeNo:商户订单号,productId:商品Id(扫码支付必传,其他选填),openid:公众号openId}
  * @return
  */
 WxPayUnifiedOrderResult wxPrePay(WxPayConfig config, WxPayUnifiedOrderRequest request);

 /**
  * 根据appCode获取商家微信支付配置
  * @param appCode
  * @return
  */
 WxPayConfig getWxPayConfigByAppCode(String appCode,String bmCode);

 WxPayConfig getWxMinPayConfigByAppCode(String appCode,String sysSoftwareCode);

 /**
  * 封装预下单参数实体
  * @param orderParam 预支付接口参数实体
  * @param order 点单
  * @return
  */
 WxPayUnifiedOrderRequest packRequestParam(PreOrderParam orderParam);

 /**
  * 查询订单
  * @param appCode 商家编码
  * @param dh 订单号
  * @param sysSoftwareCode
  */
 WxPayOrderQueryResult wxPayOrderQuery(String appCode, String bmCode, String dh, Integer type, String sysSoftwareCode);

 /**
  * 企业支付
  * @param params
  * @return:
  * @Author:
  * @date:
  */
 BaseWxPayResult entPay(EntPayParam params) throws WxPayException;
}

WxPayServiceImpl

package com.qcsoft.payment.service.impl;

import com.alibaba.fastjson.JSON;
import com.qcsoft.commonbean.bean.payment.PayLog;
import com.qcsoft.commonbean.bean.payment.WxConfig;
import com.qcsoft.commonbean.bean.payment.WxMinConfig;
import com.qcsoft.commonbean.bean.payment.common.PayConstant;
import com.qcsoft.commonbean.bean.payment.common.PayNotify;
import com.qcsoft.commonbean.bean.payment.wx.bean.PreOrderParam;
import com.qcsoft.commonbean.bean.payment.wx.bean.entpay.*;
import com.qcsoft.commonbean.bean.payment.wx.bean.request.WxPayOrderQueryRequest;
import com.qcsoft.commonbean.bean.payment.wx.bean.request.WxPayUnifiedOrderRequest;
import com.qcsoft.commonbean.bean.payment.wx.bean.result.BaseWxPayResult;
import com.qcsoft.commonbean.bean.payment.wx.bean.result.WxPayOrderQueryResult;
import com.qcsoft.commonbean.bean.payment.wx.bean.result.WxPayUnifiedOrderResult;
import com.qcsoft.commonbean.bean.payment.wx.config.WxPayConfig;
import com.qcsoft.commonbean.bean.payment.wx.exception.WxPayException;
import com.qcsoft.commonbean.bean.payment.wx.handler.EntPayHandler;
import com.qcsoft.commonbean.bean.payment.wx.handler.WxPayHandler;
import com.qcsoft.commonbean.bean.payment.wx.handler.impl.EntPayHandlerImpl;
import com.qcsoft.commonbean.bean.payment.wx.handler.impl.WxPayHandlerApacheHttpImpl;
import com.qcsoft.payment.commns.exception.BaseException;
import com.qcsoft.payment.commns.exception.QYError;
import com.qcsoft.payment.commns.utils.DateUtils;
import com.qcsoft.payment.commns.utils.RedisUtil;
import com.qcsoft.payment.service.PayLogService;
import com.qcsoft.payment.service.WxConfigService;
import com.qcsoft.payment.service.WxMinConfigService;
import com.qcsoft.payment.service.WxPayService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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.util.Date;

@Service
@Slf4j
public class WxPayServiceImpl implements WxPayService {
 private static final Logger logger = LoggerFactory.getLogger(WxPayServiceImpl.class);

 @Value("${payNotifyUrl}")
 private String notifyUrl;
 @Autowired
 private RedisUtil redisUtil;
 @Autowired
 private WxConfigService wxConfigService;
 @Autowired
 private WxMinConfigService wxMinConfigService;
 @Autowired
 private PayLogService payLogService;

 /**
  * @param config {appId:应用ID,mchId:商务号ID,mchKey:商务号密钥,notifyUrl:异步通知地址,signType:签名类型}
  * @param request {body:订单描述,totalFee:金额,
  *    spbillCreateIp:app和h5支付需要用户的真实请求地址Native支付填调用微信支付API的机器IP,
  *    tradeType:SAPI--公众号支付、NATIVE--原生扫码支付、APP--app支付,统一下单接口trade_type的传参可参考这里,
  *    outTradeNo:商户订单号,productId:商品Id(扫码支付必传,其他选填),openid:公众号openId} 还有很多参数选填
  * @return
  * @throws WxPayException
  */
 @Override
 public WxPayUnifiedOrderResult wxPrePay(WxPayConfig config, WxPayUnifiedOrderRequest request) {
  logger.info("WxPayServiceImpl.wxPrePay--->>\n微信预支付,\n商务订单号:[{}]", request.getOutTradeNo());
  WxPayHandler wx = new WxPayHandlerApacheHttpImpl();
  WxPayUnifiedOrderResult orderResult = null;
  wx.setConfig(config);
  log.info("WxPayUnifiedOrderResult ={}", config.getNotifyUrl());
  try {
   orderResult = wx.unifiedOrder(request);
  } catch (WxPayException e) {
   logger.error("WxPayServiceImpl.wxPrePay--->>\n微信预支付失败,\n商务订单号:[{}]", request.getOutTradeNo(), orderResult);
   logger.error("异常信息{}", e.getMessage());
   throw new BaseException(QYError.msg("订单号[" + request.getOutTradeNo() + "]预支付失败!"));
  }
  return orderResult;
 }

 /**
  * 根据商家编码获取微信商户配置信息
  *
  * @param appCode
  * @return
  */
 @Override
 public WxPayConfig getWxPayConfigByAppCode(String appCode, String bmCode) {
  logger.info("WxPayServiceImpl.getWxPayConfigByAppCode--->>\n获取微信支付配置信息:appCode[{}],bmCode[{}]", appCode, bmCode);
  WxConfig config = null;
  WxPayConfig wxPayConfig = new WxPayConfig();
  /**
   * 获取商家支付宝配置信息(优先级:1-redis,2-mysql,3-用户系统)
   */
  try {
   //2、mysql
   if (config == null) {
    if (StringUtils.isNotBlank(bmCode)) {
     config = wxConfigService.selectByBmCode(bmCode, appCode);
     //如果部门取不到,则去商家的配置
     if (config == null)
      config = wxConfigService.selectByAppCode(appCode);
    } else {
     config = wxConfigService.selectByAppCode(appCode);
    }
   }
   if (config == null) {
    logger.warn("WxPayServiceImpl.getWxPayConfigByAppCode--->>\n获取微信支付配置信息失败:[{}]", appCode);
    throw new BaseException(QYError.msg("获取微信支付配置信息失败!"));
   }
  } catch (Exception e) {
   logger.error("WxPayServiceImpl.getWxPayConfigByAppCode--->>\n获取微信支付配置信息失败:[{}]", e);
   throw new BaseException(QYError.msg("获取微信支付配置信息失败!"));
  }
  //根据appCode查询商家微信配置信息
  wxPayConfig.setAppId(config.getAppid());
  wxPayConfig.setMchId(config.getMchId());
  wxPayConfig.setMchKey(config.getMchKeySecret());
  wxPayConfig.setSignType(PayConstant.WXPAY_SIGNTYPE_MD5.getKey());
  wxPayConfig.setKeyUrl(config.getCrtPath());
  wxPayConfig.setKeyPath(config.getCrtPath());
  log.info("获取微信公众号配置为 [{}],转为微信支付实体为 [{}]",config,wxPayConfig);
  return wxPayConfig;
 }

 @Override
 public WxPayConfig getWxMinPayConfigByAppCode(String appCode, String sysSoftwareCode) {
  logger.error("WxPayServiceImpl.getWxMinPayConfigByAppCode--->>\n获取微信小程序支付配置信息:[{}]", appCode);
  if (StringUtils.isBlank(sysSoftwareCode)) {
   sysSoftwareCode = "store-service";
  }
  WxMinConfig config = null;
  WxPayConfig wxPayConfig = new WxPayConfig();
  /**
   * 获取商家支付宝配置信息(优先级:1-redis,2-mysql,3-用户系统)
   */
  try {
   if (config == null) {
    //config = wxMinConfigService.selectByAppCode(appCode);
    config = wxMinConfigService.selectBySysSoftwareCode(appCode,sysSoftwareCode);
   }
   if (config == null) {
    logger.warn("WxPayServiceImpl.getWxMinPayConfigByAppCode--->>\n获取微信小程序支付配置信息失败:[{}]", appCode);
    throw new BaseException(QYError.msg("获取微信支付配置信息失败!"));
   }
  } catch (Exception e) {
   logger.error("WxPayServiceImpl.getWxMinPayConfigByAppCode--->>\n获取微信小程序支付配置信息失败:[{}]", e);
   throw new BaseException(QYError.msg("获取微信小程序支付配置信息失败!"));
  }
  //根据appCode查询商家微信配置信息
  wxPayConfig.setAppId(config.getAppid());
  wxPayConfig.setMchId(config.getMchId());
  wxPayConfig.setMchKey(config.getMchKeySecret());
  wxPayConfig.setSignType(PayConstant.WXPAY_SIGNTYPE_MD5.getKey());
  wxPayConfig.setKeyUrl(config.getCrtPath());
  wxPayConfig.setKeyPath(config.getCrtPath());
  log.info("获取微信小程序退款配置为 [{}],转为微信支付实体为 [{}]",config,wxPayConfig);
  return wxPayConfig;
 }

 /**
  * 封装预下单参数实体
  *
  * @param orderParam 预支付接口参数实体
  * @param order  点单
  * @return
  */
 @Override
 public WxPayUnifiedOrderRequest packRequestParam(PreOrderParam orderParam) {
  Date date = new Date();
  BigDecimal paramAmount = new BigDecimal(orderParam.getTotalAmount().toString());
  BigDecimal multiplyNum = new BigDecimal("100");
  WxPayUnifiedOrderRequest request = WxPayUnifiedOrderRequest.newBuilder()
    .body(orderParam.getBody())
    .totalFee(paramAmount.multiply(multiplyNum).intValue())
    .spbillCreateIp(orderParam.getReqId())
    .notifyUrl(notifyUrl + PayConstant.WXPAY_09.getKey())
    .tradeType(orderParam.getTradeType())
    .outTradeNo(orderParam.getOutTradeNo())
    .build();
  log.info("WxPayUnifiedOrderRequest = {}", PayConstant.WXPAY_09.getKey());

  request.setProductId(orderParam.getDh());
  request.setDetail(orderParam.getSubject());
  PayNotify payNotify = new PayNotify();
  payNotify.setAppCode(orderParam.getAppCode());
  payNotify.setCode(orderParam.getBmCode());
  payNotify.setDh(orderParam.getDh());
  request.setAttach(JSON.toJSONString(payNotify));
  if (orderParam.getTradeType().equals(PayConstant.JSAPI.toString())) {
   request.setOpenid(orderParam.getOpenId());
  }
  //开始时间
  request.setTimeStart(DateUtils.getLastMinute(date, 0));
  //有效期
  request.setTimeExpire(orderParam.getTimeExpire());
  return request;
 }

 /**
  * 订单查询
  *
  * @param appCode 商家编码
  * @param dh  订单号
  * @param sysSoftwareCode
  * @return
  */
 @Override
 public WxPayOrderQueryResult wxPayOrderQuery(String appCode, String bmCode, String dh, Integer type, String sysSoftwareCode) {
  logger.info("WxPayServiceImpl.wxPayOrderQuery--->>\n微信订单查询,\n本次查询的商家编码为:[{}],商务订单号:[{}]", appCode, dh);
  WxPayHandler handler = new WxPayHandlerApacheHttpImpl();
  WxPayOrderQueryRequest request = new WxPayOrderQueryRequest();
  WxPayOrderQueryResult queryResult = null;
  WxPayConfig config = null;

  try {
   PayLog payLog = payLogService.selectByDh(dh);
   if (payLog==null) {
    //商家的微信支付
    if (type == null || type == 1) {
     bmCode = null;
     config = this.getWxPayConfigByAppCode(appCode, bmCode);
     //部门的微信支付支付
    } else if (type == 2) {
     config = this.getWxPayConfigByAppCode(appCode, bmCode);
     //商家小程序支付
    } else if (type == 3) {
     config = this.getWxMinPayConfigByAppCode(appCode,sysSoftwareCode);
    }
   }else{
    config = JSON.parseObject(payLog.getPayConfigJsonStr(), WxPayConfig.class);
   }
   request.setOutTradeNo(dh);
   handler.setConfig(config);
   queryResult = handler.queryOrder(request);
   logger.info("WxPayServiceImpl.wxPayOrderQuery--->>\n微信订单查询成功,\n返回包信息:[{}]", JSON.toJSONString(queryResult));
  } catch (WxPayException e) {
   logger.error("WxPayServiceImpl.wxPayOrderQuery--->>\n微信订单查询失败,\n商务订单号:[{}]", dh);
   throw new BaseException(QYError.msg("微信订单查询失败,商务订单号:[{}]" + dh + ""));
  }
  return queryResult;
 }
 /**
  * 企业支付
  * @param params
  * @return: com.qcsoft.commonbean.bean.payment.wx.bean.result.BaseWxPayResult
  * @Author:
  * @date:
  */
 @Override
 public BaseWxPayResult entPay(EntPayParam params) throws WxPayException {
  WxPayHandler wxPayHandler = new WxPayHandlerApacheHttpImpl();
  WxPayConfig config = null;
  EntPayResult result = null;
  EntPayBankResult bankResult = null;
  if (params.getTradeType() != null && params.getTradeType() == 3) {
   config = this.getWxMinPayConfigByAppCode(params.getAppCode(),params.getSysSoftwareCode());
  } else {
   config = this.getWxPayConfigByAppCode(params.getAppCode(), params.getBmCode());
  }
  config.setUseKey(true);
  wxPayHandler.setConfig(config);
  if (params.getTradeType()==1) {
   EntPayRequest request = packEntPayRequest(params,config);
   EntPayHandler entPayHandler = new EntPayHandlerImpl(wxPayHandler);

   result = entPayHandler.entPay(request);
   return result;
  }else{
   EntPayBankRequest request = packEntPayBankRequest(params);
   EntPayHandler entPayHandler = new EntPayHandlerImpl(wxPayHandler);
   bankResult = entPayHandler.payBank(request);
   return bankResult;
  }
 }
 /**
  * 封装微信企业支付请求参数
  * @param params
  * @return: com.qcsoft.commonbean.bean.payment.wx.bean.entpay.EntPayRequest
  * @Author:
  * @date:
  */
 public EntPayRequest packEntPayRequest(EntPayParam params,WxPayConfig config) {
  EntPayRequest request = new EntPayRequest();
  request.setAmount(params.getAmount().multiply(new BigDecimal("10")).intValue());
  request.setCheckName("NO_CHECK");
  request.setOpenid(params.getOpenid());
  request.setSpbillCreateIp(params.getSpbillCreateIp());
  request.setPartnerTradeNo(params.getPartnerTradeNo());
  request.setMchAppid(config.getAppId());
  request.setMchId(config.getMchId());
  request.setDescription(params.getDesc());
  return request;
 }

 /**
  * 封装银行卡企业支付参数
  * @param params
  * @return: com.qcsoft.commonbean.bean.payment.wx.bean.entpay.EntPayBankRequest
  * @Author:
  * @date:
  */
 public EntPayBankRequest packEntPayBankRequest(EntPayParam params) {
  EntPayBankRequest request = new EntPayBankRequest();
  request.setAmount(params.getAmount().multiply(new BigDecimal("10")).intValue());
  request.setBankCode(params.getBankCode());
  request.setEncTrueName(params.getEncTrueName());
  request.setPartnerTradeNo(params.getPartnerTradeNo());
  request.setDescription(params.getDesc());
  return request;
 }
}

5、这些类是和数据库交互的增删查该类

 @Autowired
 private WxConfigService wxConfigService;
 @Autowired
 private WxMinConfigService wxMinConfigService;
 @Autowired
 private PayLogService payLogService;
 @Autowired
 private AlipayConfigService alipayConfigService;
 ```

到此这篇关于Java 实现微信和支付宝支付功能的文章就介绍到这了,更多相关Java 微信和支付宝支付内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • java实现微信支付结果通知

    支付完成后,微信会把相关支付结果和用户信息发送给商户,商户需要接收处理,并返回应答. 对后台通知交互时,如果微信收到商户的应答不是成功或超时,微信认为通知失败,微信会通过一定的策略定期重新发起通知,尽可能提高通知的成功率,但微信不保证通知最终能成功. (通知频率为15/15/30/180/1800/1800/1800/1800/3600,单位:秒) 注意:同样的通知可能会多次发送给商户系统.商户系统必须能够正确处理重复的通知. 推荐的做法是,当收到通知进行处理时,首先检查对应业务数据的状态,判断

  • java 商户PC端接入支付宝支付的实现方法

    用户在商户网站上完成支付一.商户申请流程和业务场景即时到账:网上交易时,买家的交易资金直接打入卖家支付宝账户,快速回笼交易资金.申请条件: 1) 申请前必须拥有经过实名认证的支付宝账户: 2) 企业或个体工商户可申请: 3) 需提供真实有效的营业执照,且支付宝账户名称需与营业执照主体一致: 4) 网站能正常访问且页面显示完整,网站需要明确经营内容且有完整的商品信息: 5) 网站必须通过ICP备案.如为个体工商户,网站备案主体需要与支付宝账户主体名称一致: 6) 如为个体工商户,则团购不开放,且古

  • java后台实现支付宝支付接口和支付宝订单查询接口(前端为APP)

    最近项目APP需要接入微信.支付宝支付功能,在分配开发任务时,听说微信支付接口比支付宝支付接口要难实现,由于我开发经验不是那么丰富(现工作经验1年半)且未接触过支付接口开发,组里刚好又有支付接口的老司机,所以很自然把简单的支付宝接口开发任务交给了我,看来开发组的组长还是很好人的嘛.....,废话就不多说了,我们开始吧! 实现支付宝接口详细过程 1.去支付宝官网申请公司企业账号并开通一个应用,在应用里签约APP支付功能 具体的申请截图步骤,在这里我就不详细说了,因为这不是文章的重点,可参考支付宝官

  • 微信支付H5调用支付详解(java版)

    最近项目需要微信支付,然后看了下微信公众号支付,,虽然不难,但是细节还是需要注意的,用了大半天时间写了个demo,并且完整的测试了一下支付流程,下面分享一下微信公众号支付的经验. 一.配置公众号微信支付  需要我们配置微信公众号支付地址和测试白名单. 比如:支付JS页面的地址为 http://www.xxx.com/shop/pay/ 那此处配置www.xxx.com/shop/pay/ 二.开发流程 借用微信公众号支付api(地址 http://pay.weixin.qq.com/wiki/d

  • Java微信支付之公众号支付、扫码支付实例

    微信支付现在已经变得越来越流行了,随之也出现了很多以可以快速接入微信支付为噱头的产品,不过方便之余也使得我们做东西慢慢依赖第三方,丧失了独立思考的能力,这次打算分享下我之前开发过的微信支付. 一 .H5公众号支付 要点:正确获取openId以及统一下单接口,正确处理支付结果通知,正确配置支付授权目录 H5的支付方式是使用较为广泛的方式,这种支付方式主要用于微信内自定义菜单的网页,依赖手机上安装的微信客户端,高版本的微信才支持微信支付,下面按我的流程注意说明 1  编写用于支付的页面,由于是测试用

  • 详解java集成支付宝支付接口(JSP+支付宝20160912)

    吐槽一下: 支付宝的接口和微信的DEMO和文档真心太难看懂了,乱七八糟,都不知道去哪里找自己要的东西,最近几天我们公司需要做类似的开发,我作为先锋,率先解决Java集成支付宝支付和微信支付接口工作. 我们的工作环境:JSP网站+支付接口,目前工作的支付宝接口为20160912,微信为V3版本,如遇到版本升级,请联系相关机构的客户服务人员升级. 本文介绍JSP+支付宝接口,本文非原创. 新手注意: 1.本文使用的接口地址和参数为沙箱的地址,无论你在使用沙箱或者正式地址,务必核实使用接口地址和参数,

  • Java 实现微信和支付宝支付功能

    一.前期准备 1.申请好微信商户号appid,拿到商户id和商户秘钥,退款的话需要商户证书 2.申请好支付宝商户号appid,商户公钥和秘钥(需要用支付宝工具自己生成),支付宝退款不需要证书 二.数据库表设计 1.微信支付配置表 CREATE TABLE `py_wx_config` ( `wx_config_id` varchar(18) NOT NULL COMMENT '微信支付配置ID', `appid` varchar(128) DEFAULT NULL COMMENT '微信公众号I

  • Java spring boot 实现支付宝支付功能的示例代码

    一.准备工作: 1.登陆支付宝开发者中心,申请一个开发者账号. 地址:https://openhome.alipay.com/ 2.进入研发服务: 3.点击链接进入工具下载页面: 4.点击下载对应版本的RSA公钥生成器: 5.生成公钥密钥(记录你的应用私钥): 6.在支付宝配置公钥(点击保存): 二.搭建demo 1.引入jia包: <dependency> <groupId>com.alipay.sdk</groupId> <artifactId>alip

  • java实现微信扫码支付功能

    本文实例为大家分享了java实现微信扫码支付的具体代码,供大家参考,具体内容如下 1.maven项目的pom.xml中添加如下jar包: <dependency> <groupId>com.github.wxpay</groupId> <artifactId>wxpay-sdk</artifactId> <version>0.0.3</version> </dependency> 2.编写WeWxConfig类

  • java服务器端微信、支付宝支付和退款功能

    工作需要,写了服务器端的支付和退款功能,包含微信和支付宝,网上也有很多demo可以借鉴,我把我的代码放出来,写的比较简单,有问题的欢迎指正,大家一起学习. 微信支付需要调用微信的统一下单接口,而支付宝不用. 我写的时候微信和支付宝都单独写了一个工具类,来调用支付,给前端返回需要的数据. ps:支付是可以不需要服务器端的,不过为了安全一点点,所以前端需要调起支付的字段都直接从服务器端返回,前端拿到字段直接调起支付就可以了. Map<String,String> map = new HashMap

  • Java将微信和支付宝支付的个二维码合二为一的方法

    因公司需要将支付宝和微信的二维码合成一个,不管用户用支付宝扫还是微信扫都能打开对应的支付页面,在网上找了一些文章,很感谢各位大神的经验,我也记录下我是如何将两个二维码合二为一的~. 原理:支付宝或微信生成的二维码中本质都内嵌了一个url,在扫码时实际是定向去访问二维码中内嵌的url,这样我就可以将这个url指定到我的一个控制器,在控制器中判断是微信还是支付宝软件扫的,然后去唤醒各自的支付即可. 1.首先生成二维码: folderName:存储二维码图片的文件夹名 imageName:二维码图片名

  • SpringMVC+Spring+Mybatis实现支付宝支付功能的示例代码

    本博客详细介绍了如何使用ssm框架实现支付宝支付功能.本文章分为两大部分,分别是「支付宝测试环境代码测试」和「将支付宝支付整合到ssm框架」,详细的代码和图文解释,自己实践的时候一定仔细阅读相关文档. 教程源代码:https://github.com/OUYANGSIHAI/sihai-maven-ssm-alipay 一.支付宝测试环境代码测试: 1.下载电脑网站的官方demo以及查看参考相关文档: 地址:https://docs.open.alipay.com/270/106291/ 2.下

  • Vue的H5页面唤起支付宝支付功能

    目前项目中比较常用的第三方支付无非就是支付宝支付和微信支付.下面介绍一下Vue中H5页面如何使用支付宝支付.其实很简单的,只不过是调自己后台的一个接口而已(后台根据支付宝文档,写好支付接口). 触发支付宝支付调用后台接口,后台会返回支付宝提供的form表单,我们只要在vue里面创建新节点,将返回的form表单append进去,并提交就可以唤起支付宝支付.另在此说一下这个 returnUrl , 它是支付后支付宝回调的页面.具体可以根据自身业务,后台写死或者由前端控制. methods () {

  • django中使用事务及接入支付宝支付功能

    之前一直想记录一下在项目中使用到的事务以及支付宝支付功能,自己一直犯懒没有完,趁今天有点兴致,在这记录一下. 商城项目必备的就是支付订单的功能,所以就会涉及到订单的保存以及支付接口的引入.先来看看订单的保存,在数据库模型涉及之初,将订单分成了两个表,一个为订单表,记录订单的基本信息,如订单号,用户信息,运费之类,一个为订单商品表,记录该订单中的商品信息.在保存订单时,肯定会涉及到两个表的新建和保存,其实还有一张表也需要进行一些修改,那就是商品表,当一个订单保存成功,意味着本次交易成功,商品售出,

  • 微信公众平台开发教程③ PHP实现微信公众号支付功能图文详解

    本文实例讲述了PHP实现微信公众号支付功能.分享给大家供大家参考,具体如下: 直言无讳,我就是一个初涉微信开发的小白,写这篇博客的原因:一是为了给自己做下备忘记录,以便以后能回忆这条程序猿的坎坷路:二是希望能帮助到同是自学开发的小白们:三是对那些不屑一顾于我等尘埃的大牛们的控诉,小白的道路坎坷,你们凭什么总要一副高高在上的样子?我等敬而不畏... 背景介绍: 随着智能手机的普及,移动支付下的微信.支付宝所提供的便利需求不言而喻,好吧,至少我周围连个小摊贩的早餐都可以微信支付,而且人家手机还比我高

  • PHP实现的支付宝支付功能示例

    本文实例讲述了PHP实现的支付宝支付功能.分享给大家供大家参考,具体如下: 在给app做支付宝支付接口的时候收集内容整理如下: 接口: import('alipay.AopClient', EXTEND_PATH); import('alipay.request.AlipayTradeAppPayRequest', EXTEND_PATH); $aop = new \AopClient(); $aop->gatewayUrl = "https://openapi.alipay.com/ga

随机推荐