当前位置:网站首页>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";
}
}
边栏推荐
- pytorch DataLoader实现miniBatch(未完成)
- 1. Somme des deux nombres
- Download the corresponding version of chromedriver
- Final review (Day6)
- [minesweeping of two-dimensional array application] | [simple version] [detailed steps + code]
- NG Textarea-auto-resize
- 1. Sum of two numbers
- 2022.7.2day594
- QT read write excel -- qxlsx insert chart 5
- It is said that the operation and maintenance of shell scripts are paid tens of thousands of yuan a month!!!
猜你喜欢

Redhat7 system root user password cracking

Why is the website slow to open?
![[advanced pointer (2)] | [function pointer, function pointer array, callback function] key analysis + code explanation](/img/9b/a309607c037b0a18ff6b234a866f9f.jpg)
[advanced pointer (2)] | [function pointer, function pointer array, callback function] key analysis + code explanation
![[escape character] [full of dry goods] super detailed explanation + code illustration!](/img/33/ec5a5e11bfd43f53f2767a9a0f0cc9.jpg)
[escape character] [full of dry goods] super detailed explanation + code illustration!
![[teacher Zhao Yuqiang] kubernetes' probe](/img/cc/5509b62756dddc6e5d4facbc6a7c5f.jpg)
[teacher Zhao Yuqiang] kubernetes' probe

Beaucoup de CTO ont été tués aujourd'hui parce qu'il n'a pas fait d'affaires

今天很多 CTO 都是被幹掉的,因為他沒有成就業務

Understand one-way hash function

期末复习(Day5)

Redhat7系统root用户密码破解
随机推荐
Exception when introducing redistemplate: noclassdeffounderror: com/fasterxml/jackson/core/jsonprocessingexception
Communication - how to be a good listener?
Apple submitted the new MAC model to the regulatory database before the spring conference
[advanced pointer (2)] | [function pointer, function pointer array, callback function] key analysis + code explanation
pytorch DataLoader实现miniBatch(未完成)
It is said that the operation and maintenance of shell scripts are paid tens of thousands of yuan a month!!!
Pytorch dataloader implements minibatch (incomplete)
Xaml gradient issue in uwp for some devices
1. Sum of two numbers
Sorry, this user does not exist!
[written examination question analysis] | | get [sizeof and strlen] [pointer and array] graphic explanation + code analysis
Personal outlook | looking forward to the future from Xiaobai's self analysis and future planning
期末复习(DAY6)
88. 合并两个有序数组
"C and pointer" - Chapter 13 function of function pointer 1 - callback function 1
Why is the website slow to open?
Jetson AgX Orin platform porting ar0233 gw5200 max9295 camera driver
Configure DTD of XML file
Final review (Day2)
Final review Day8