Java利⽤沙箱⽀付实现电脑扫码⽀付教程⽬录
⼀、准备⼯作
⼆、效果展⽰
三、实现代码
3.1 后台代码
3.2 前台代码
⼀、准备⼯作
1、注册⽀付宝开放平台账号,成为开发者。
2、进⼊沙箱,进⾏配置。
3.我们可以看到这个界⾯
4.后⾯需要使⽤的参数
APPID
商户私钥(使⽤系统默认密钥的公钥模式,点击查看获取)
⽀付宝公钥
⽀付宝⽹关
5、⼿机上下载沙箱⽀付宝(到时候⽀付⽤这个⽀付宝⽀付)
6、下载好⽀付宝沙箱版后,登录⽀付宝提供的账号
⼆、效果展⽰
1、随⼿写的⼀个前台vue界⾯
2、进⼊确定订单界⾯,可以添加商品描述
3、跳转⽀付界⾯,利⽤沙箱⽀付宝完成⽀付
4、完成⽀付
5、跳回⾃⼰设置的界⾯
三、实现代码
3.1 后台代码
(我这⾥利⽤的是SpringBoot集成的SSM,当然不使⽤SpringBoot也可以)
3.1.l⽂件
不⽤全部的,重点是:⽀付宝sdk包、fastjson
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zking</groupId>
<artifactId>springboot02</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot02</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
&porting.outputEncoding>UTF-8</porting.outputEncoding>
<spring-boot.version>2.1.18.RELEASE</spring-boot.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId&batis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.4</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.44</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
<version>2.6.2</version>
</dependency>
<!-- ⽀付宝sdk包 -->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>3.1.0</version>
</dependency>
<!-- fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.48</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- mvnrepository/artifact/com.alibaba/druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.6</version>
</dependency>
<!-- mvnrepository/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>${spring-boot.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- mvn mybatis-generator:generate -->
<plugin>
<groupId&ator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.2</version>
<configuration>
<verbose>true</verbose>
<overwrite>true</overwrite>
<!--配置⽂件的位置-->
<configurationFile>src/main/l</configurationFile> </configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.1.18.RELEASE</version>
<configuration>
<mainClass>com.zking.springboot02.Springboot02Application</mainClass> </configuration>
<executions>
<execution>
<id>repackage</id>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
3.1.2 model包
package com.del;
import lombok.Data;
/**
* ⽀付实体对象
* 根据⽀付宝接⼝协议,其中的属性名,必须使⽤下划线,不能修改
*
* @author 借我丹青妙笔
*/
@Data
public class AlipayBean {
private static final long serialVersionUID = 1L;
/**
* 商户订单号,必填
*
*/
private String out_trade_no;
/**
* 订单名称,必填
*/
private String subject;
/**
* 付款⾦额,必填
* 根据⽀付宝接⼝协议,必须使⽤下划线
*/
private String total_amount;
/**
* 商品描述,可空
*/
private String body;
/**
* 超时时间参数
*/
private String timeout_express= "10m";
/**
* 产品编号
*/
private String product_code= "FAST_INSTANT_TRADE_PAY";
public String getOut_trade_no() {
return out_trade_no;
}
public void setOut_trade_no(String out_trade_no) {
this.out_trade_no = out_trade_no;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getTotal_amount() {
return total_amount;
}
public void setTotal_amount(String total_amount) {
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
}
3.1.3 utils包AlipayConfig类
(请配置好该类,防⽌报错)
1. appId:APPID,沙箱应⽤提供的
2. privateKey: 商户私钥,点击公钥证书查看
3. returnUrl : ⽀付完成后跳转的页⾯,例如我填的是:localhost:8088/
package com.zking.springboot02.utils;
import lombok.Data;
import t.annotation.Configuration;
import t.annotation.PropertySource;
import org.springframework.stereotype.Component;
/**
* 配置⽂件读取
*
*/
@Configuration
@Data
@Component
public class AlipayConfig {
/**
* 应⽤ID,您的APPID,收款账号既是您的APPID对应⽀付宝账号
*/
private String appId = "";
/**
* 商户私钥,您的PKCS8格式RSA2私钥
*/
private String privateKey = "";
/**
* ⽀付宝公钥,
*/
private String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyqAN9WzWigi
m0/3fBK97RFZ7Juu31+DfXMVHTHSTP+4WPvr80zTiIQmT9xTFVGBgD8BBX0XELxqLQxsYQm/MgEgccHTnCKPP7Ci979YuwZyjOysdTc6BNO/6RqPZruih /**
* 服务器异步通知页⾯路径需格式的完整路径,不能加?id=123这类⾃定义参数
*/
private String notifyUrl = "p";
/**
* 页⾯跳转同步通知页⾯路径需格式的完整路径.
* ⽀付完成后返回的地址
*/
private String returnUrl = "";
/**
* 签名⽅式
*/
private String signType = "RSA2";
/**
* 字符编码格式
*/
private String charset = "utf-8";
/**
* ⽀付宝⽹关
*/
private String gatewayUrl = "openapi.alipaydev/gateway.do";
/**
* ⽀付宝⽹关
*/
private String logPath = "C:\\";
}
Alipay类
package com.zking.springboot02.utils;
import com.alibaba.fastjson.JSON;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.quest.AlipayTradePagePayRequest;
import com.del.AlipayBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* ⽀付宝⽀付接⼝
* @author 借我丹青妙笔
*/
@Component
public class Alipay {
@Autowired
private AlipayConfig alipayConfig;
/**
* ⽀付接⼝
* @param alipayBean
* @return
* @throws AlipayApiException
*/
public String pay(AlipayBean alipayBean) throws AlipayApiException {
// 1、获得初始化的AlipayClient
String serverUrl = GatewayUrl();
String appId = AppId();
String privateKey = PrivateKey();
String format = "json";
String charset = Charset();
String alipayPublicKey = PublicKey();
String signType = SignType();
String returnUrl = ReturnUrl();
String notifyUrl = NotifyUrl();
//System.out.println(appId);
AlipayClient alipayClient = new DefaultAlipayClient(serverUrl, appId, privateKey, format, charset, alipayPublicKey, signType); // 2、设置请求参数
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
// 页⾯跳转同步通知页⾯路径
alipayRequest.setReturnUrl(returnUrl);
// 服务器异步通知页⾯路径
alipayRequest.setNotifyUrl(notifyUrl);
// 封装参数
alipayRequest.JSONString(alipayBean));
// 3、请求⽀付宝进⾏付款,并获取⽀付结果
String result = alipayClient.pageExecute(alipayRequest).getBody();
// 返回付款信息
return result;
}
}
3.1.4 Service包
PayService接⼝
package com.zking.springboot02.service;
import com.alipay.api.AlipayApiException;
import com.del.AlipayBean;
/**
* ⽀付服务
* @author 借我丹青妙笔
* @date Dec 12, 2018
*/
public interface PayService {
/
**
* ⽀付宝⽀付接⼝
* @param alipayBean
* @return
* @throws AlipayApiException
*/
String aliPay(AlipayBean alipayBean) throws AlipayApiException;
}
PayServiceImpl类
package com.zking.springboot02.service.impl;
import com.alipay.api.AlipayApiException;
import com.del.AlipayBean;
import com.zking.springboot02.service.PayService;
import com.zking.springboot02.utils.Alipay;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class PayServiceImpl implements PayService {
@Resource
private Alipay alipay;
@Override
public String aliPay(AlipayBean alipayBean) throws AlipayApiException {
return alipay.pay(alipayBean);
}
}
3.1.5 contorller包
payController类
package com.ller;
import com.alipay.api.AlipayApiException;
import com.del.AlipayBean;
import com.zking.springboot02.service.PayService;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
@RestController
@RequestMapping("/pay")
@CrossOrigin
public class PayController {
@Resource
private PayService payService;
/
**
* 阿⾥⽀付
* @param alipayBean
* @return
maven下载教程* @throws AlipayApiException
*/
@PostMapping("/alipay")
public Map alipay(AlipayBean alipayBean) throws AlipayApiException {
System.out.println(alipayBean);
Map<String,Object> map = new HashMap<String,Object>();
String str = payService.aliPay(alipayBean);
System.out.println(str);
map.put("msg",str);
map.put("code",0);
//
return map;
}
}
3.1.l⽂件
server:
port: 8080 #端⼝号
servlet:
context-path: /s02 #项⽬名
3.2 前台代码
(前台是利⽤脚⼿架搭建的Vue项⽬)
3.2.1 src下api包下action.js⽂件
/**
* 对后台请求的地址的封装,URL格式如下:
* 模块名_实体名_操作
*/
export default {
//服务器
'SERVER': 'localhost:8080/s02',
'alipay' : 'localhost:8080/s02/pay/alipay',
//获得请求的完整地址,⽤于mockjs测试时使⽤
'getFullPath': k => {
return this.SERVER + this[k];
}
}
3.2.2 src下api包下http.js⽂件
/**
* vue项⽬对axios的全局配置
*/
import axios from 'axios'
import qs from 'qs'
//引⼊action模块,并添加⾄axios的类属性urls上
import action from '@/api/action'
axios.urls = action
// axios默认配置
axios.defaults.timeout = 10000; // 超时时间
// axios.defaults.baseURL = 'localhost:8080/crm'; // 默认地址
axios.defaults.baseURL = action.SERVER;
//整理数据
// 只适⽤于 POST,PUT,PATCH,transformRequest` 允许在向服务器发送前,修改请求数据ansformRequest = function(data) {
data = qs.stringify(data);
return data;
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论