当前位置:网站首页>Alibaba cloud Alipay sandbox payment
Alibaba cloud Alipay sandbox payment
2022-07-03 05:51:00 【SSID-cc-r】
Alibaba cloud Alipay sandbox payment
The project file is maven project
First step pom.xml introduce jar package
<!-- Sandbox payment dependency -->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>4.20.0.ALL</version>
</dependency>
Create a AlipayConfig class
package com.alipay.config;
import java.io.FileWriter;
import java.io.IOException;
/* *
* Class name :AlipayConfig
* function : Basic configuration class
* detailed : Set up account information and return path
* modification date :2017-04-05
* explain :
* The following code is just a sample code for the convenience of merchant testing , Merchants can according to their own website needs , Prepared according to technical documents , It's not necessary to use this code .
* This code is for studying and studying Alipay interface only. , Just a reference .
*/
public class AlipayConfig {
//↓↓↓↓↓↓↓↓↓↓ Please configure your basic information here ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
// application ID, Your APPID, The collection account is your APPID Corresponding Alipay account number
public static String app_id = "";
// Merchants private key , Your PKCS8 Format RSA2 Private key
public static String merchant_private_key = "";
// Alipay public key , Check the address :https://openhome.alipay.com/platform/keyManage.htm Corresponding APPID Alipay public key .
public static String alipay_public_key = "";
// Server asynchronous notification page path Need to be http:// Full path to format , Cannot add ?id=123 Such custom parameters , The Internet must be accessible
// The address to jump after successful payment Change to the address where you modify the order status .
// public static String return_url = "http:// This machine ip Address :8081/order/updateOrderStatus";
public static String notify_url = "http:// Access address of the project public network /alipay.trade.page.pay-JAVA-UTF-8/notify_url.jsp";
// Page Jump synchronization notification page path Need to be http:// Full path to format , Cannot add ?id=123 Such custom parameters , The Internet must be accessible
public static String return_url = "http:// Access address of the project public network /alipay.trade.page.pay-JAVA-UTF-8/return_url.jsp";
// Signature method
public static String sign_type = "RSA2";
// Character encoding format
public static String charset = "utf-8";
// Alipay gateway
public static String gatewayUrl = "https://openapi.alipay.com/gateway.do";
// Alipay gateway
public static String log_path = "C:\\";
//↑↑↑↑↑↑↑↑↑↑ Please configure your basic information here ↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
/**
* Write the log , Convenient test ( See website requirements , It can also be changed to store records in the database )
* @param sWord To write the text content in the log
*/
public static void logResult(String sWord) {
FileWriter writer = null;
try {
writer = new FileWriter(log_path + "alipay_log_" + System.currentTimeMillis()+".txt");
writer.write(sWord);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Submit button of shopping cart page , When you click Submit , The page jumps to ShopcarController class , Sandbox payment page go , Pay and arrive OrderController Class .
submit();
function submit() {
// Get all the product information and submit it to the background
$.ajax({
url: "/shopcar/shopCarBalance",
data: {
bookid: bookid,
ids: ids,
totalcount: total_count,
totalprice: total_price,
cname: addName,
phone: addPhone,
address: addAddrerss,
express: express
},
dataType: "json",
type: "post",
success: function (addOrderObj) {
if (addOrderObj.code == 1) {
location.href="/shopcar/pay?orderno="+addOrderObj.data.ordernumber+"&money="+addOrderObj.data.totalprice;
} else {
layer.msg(" Failed to submit order !", {icon: 5})
}
}
})
}
}
ShopcarController class , Sandbox payment page
/**
* Confirmation of order
*/
@RestController
@RequestMapping("/shopcar")
public class ShopcarController {
@Autowired
private ShopcarMapper shopcarMapper;
@Autowired
private AddressMapper addressMapper;
@Autowired
private BookMapper bookMapper;
@Autowired
private OrderMapper orderMapper;
@Autowired
private Order_detailMapper orderDetailMapper;
@Autowired
private BooktypeMapper bookTypeMapper;
/**
* Add it to the order when paying
* @param order
* @param session
* @return
// */
// shopCarBalance
@RequestMapping("shopCarBalance")
public CommonResult shopCarBalance(Order order, HttpSession session, Integer bookid, String ids,String count, String price){
User user = (User) session.getAttribute("user");
order.setPname(user.getName()); // The name of the next person
order.setUid(user.getUid()); // The number of the next person
long orderNo = new Date().getTime();
order.setOrderNumber(orderNo+""+user.getUid()); // The order number
order.setStatus(5); // The order status 5 Did not pay
order.setPdate(new Date()); // Order time
System.out.println(order);
orderMapper.insert(order);
System.out.println("~~~~~~~~~~~~bookid==========="+bookid);
if (bookid !=null ){
// Add a Book
Book book = bookMapper.selectByPrimaryKey(bookid); // According to the book id Search for book information
Order_detail orderDetail = new Order_detail();
orderDetail.setBookid(book.getBookid());
orderDetail.setBookname(book.getBookname());
orderDetail.setAuthor(book.getBookauthor());
orderDetail.setPublisher(book.getBookpublisher());
Integer count1 = Integer.valueOf(count);
orderDetail.setCount(count1);
orderDetail.setPrice(price);
Booktype booktype = bookTypeMapper.selectByPrimaryKey(book.getTypeid());
orderDetail.setBooktype(booktype.getBookType());
orderDetail.setUrl(book.getUrl());
orderDetail.setPid(order.getPid());
orderDetail.setStatus(0);
orderDetailMapper.insert(orderDetail);
}
// Add multiple
System.out.println("~~~~~~~~~ids~~~~~"+ids);
if (ids!=null && ids!=""){
String[] split = ids.split(",");// Multiple bookid
for (String s:split){
System.out.println(s);
}
System.out.println(split+"===========");
for (String s:split){
Integer ss = Integer.valueOf(s);
Shopcar shopcar = shopcarMapper.selectByPrimaryKey(ss);
Book book = bookMapper.selectByPrimaryKey(shopcar.getBookid());
System.out.println(book);
Order_detail orderDetail = new Order_detail();
orderDetail.setBookid(book.getBookid());
orderDetail.setBookname(book.getBookname());
orderDetail.setAuthor(book.getBookauthor());
orderDetail.setPublisher(book.getBookpublisher());
orderDetail.setCount(shopcar.getCount());
orderDetail.setPrice(shopcar.getPrice());
Booktype booktype = bookTypeMapper.selectByPrimaryKey(book.getTypeid());
orderDetail.setBooktype(booktype.getBookType());
orderDetail.setUrl(book.getUrl());
orderDetail.setPid(order.getPid());
orderDetail.setStatus(0);
orderDetailMapper.insert(orderDetail);
}
}
return new CommonResult(1," Order added successfully ",order);
}
/* The sandbox */
@RequestMapping("pay")
public String pay(String orderno, String money, HttpServletRequest request){
try {
// Get initialized AlipayClient
AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type);
// Set request parameters
AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
alipayRequest.setReturnUrl(AlipayConfig.return_url);
alipayRequest.setNotifyUrl(AlipayConfig.notify_url);
// Merchant order number , The unique order number in the order system of the merchant website , Required
String out_trade_no = new String(orderno.getBytes("ISO-8859-1"), "UTF-8");
// The payment amount , Required
String total_amount = new String(money.getBytes("ISO-8859-1"), "UTF-8");
// Name of the order , Required
String subject = new String("qy143 Book management ".getBytes("ISO-8859-1"), "UTF-8");
// Commodity Description , Can be empty
alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
+ "\"total_amount\":\"" + total_amount + "\","
+ "\"subject\":\"" + subject + "\","
+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
// If you want to BizContent Add other optional request parameters , To add custom timeout parameters timeout_express To illustrate
//alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
// + "\"total_amount\":\""+ total_amount +"\","
// + "\"subject\":\""+ subject +"\","
// + "\"body\":\""+ body +"\","
// + "\"timeout_express\":\"10m\","
// + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
// Request parameters are available 【 Paid by computer website API file -alipay.trade.page.pay- Request parameters 】 chapter
// request
String result = alipayClient.pageExecute(alipayRequest).getBody();
return result;
}catch (Exception e){
e.printStackTrace();
}
return null;
// Output
}
}
OrderController The user payment successfully jumps to this page
@Controller
@RequestMapping("order")
public class OrderController {
@Autowired
ShopcarMapper shopcarMapper;
@Autowired
private OrderMapper orderMapper;
@Autowired
Order_detailMapper order_detailMapper;
@RequestMapping("updateOrderStatus")
public String updateOrderStatus(String out_trade_no){
System.out.println(out_trade_no);
Order order=new Order();
order.setOrderNumber(out_trade_no);
order.setStatus(0);
orderMapper.updateOrderStatus(order);
return "redirect:/gereng.jsp";
}
}
边栏推荐
- Bernoulli distribution, binomial distribution and Poisson distribution, and the relationship between maximum likelihood (incomplete)
- [untitled]
- Complete set of C language file operation functions (super detailed)
- C 语言文件操作函数大全 (超详细)
- AtCoder Beginner Contest 258(A-D)
- [advanced pointer (1)] | detailed explanation of character pointer, pointer array, array pointer
- pytorch DataLoader实现miniBatch(未完成)
- Maximum likelihood estimation, divergence, cross entropy
- Solve the problem of automatic disconnection of SecureCRT timeout connection
- Source insight operation manual installation trial
猜你喜欢
【一起上水硕系列】Day 10
[teacher Zhao Yuqiang] Cassandra foundation of NoSQL database
PHP notes are super detailed!!!
mapbox尝鲜值之云图动画
一起上水硕系列】Day 9
多线程与高并发(7)——从ReentrantLock到AQS源码(两万字大章,一篇理解AQS)
[escape character] [full of dry goods] super detailed explanation + code illustration!
Apt update and apt upgrade commands - what is the difference?
[teacher Zhao Yuqiang] the most detailed introduction to PostgreSQL architecture in history
今天很多 CTO 都是被幹掉的,因為他沒有成就業務
随机推荐
The programmer shell with a monthly salary of more than 10000 becomes a grammar skill for secondary school. Do you often use it!!!
Final review (Day7)
1. Somme des deux nombres
kubernetes资源对象介绍及常用命令(五)-(ConfigMap)
Today, many CTOs were killed because they didn't achieve business
Jetson AgX Orin platform porting ar0233 gw5200 max9295 camera driver
MySQL startup error: several solutions to the server quit without updating PID file
Life is a process of continuous learning
mapbox尝鲜值之云图动画
AtCoder Beginner Contest 258(A-D)
2022.6.30DAY591
牛客网 JS 分隔符
最大似然估计,散度,交叉熵
Personal outlook | looking forward to the future from Xiaobai's self analysis and future planning
Error 1045 (28000) occurs when Linux logs in MySQL: access denied for user 'root' @ 'localhost' (using password: yes)
[teacher Zhao Yuqiang] MySQL high availability architecture: MHA
Capacity expansion mechanism of map
Using the ethtool command by example
Why should there be a firewall? This time xiaowai has something to say!!!
Export the altaro event log to a text file