支付宝的pc端支付全流程的java开发代码及vue30的前端开发代码具体怎么写
由于支付宝的PC端支付全流程包含了前端和后端两部分,因此需要分别编写前端和后端代码。
- 后端代码
支付宝的PC端支付全流程需要使用支付宝提供的SDK进行开发,以下是Java语言下的SDK使用示例。
首先需要引入SDK依赖:
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>3.3.0.ALL</version>
</dependency>
然后需要编写支付宝支付的核心代码:
// 1. 创建AlipayClient实例
AlipayClient alipayClient = new DefaultAlipayClient(
"https://openapi.alipay.com/gateway.do",
"APP_ID",
"APP_PRIVATE_KEY",
"json",
"utf-8",
"ALIPAY_PUBLIC_KEY",
"RSA2"
);
// 2. 构造支付请求参数
AlipayTradePagePayRequest request = new AlipayTradePagePayRequest();
request.setReturnUrl("http://localhost:8080/return_url");
request.setNotifyUrl("http://localhost:8080/notify_url");
request.setBizContent("{\"out_trade_no\":\"" + outTradeNo + "\","
+ "\"total_amount\":\"" + totalAmount + "\","
+ "\"subject\":\"" + subject + "\","
+ "\"body\":\"" + body + "\","
+ "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
// 3. 调用接口进行支付
String form = alipayClient.pageExecute(request).getBody();
以上代码中,需要替换掉APP_ID、APP_PRIVATE_KEY、ALIPAY_PUBLIC_KEY等参数为自己的支付宝应用信息。
- 前端代码
支付宝的PC端支付全流程需要使用支付宝提供的支付页面进行展示和支付,以下是Vue3.0语言下的前端代码示例。
首先需要引入支付宝提供的支付页面:
<template>
<div>
<iframe :src="payUrl" width="100%" height="600"></iframe>
</div>
</template>
<script>
export default {
name: 'AlipayPayment',
props: {
outTradeNo: String,
totalAmount: Number,
subject: String,
body: String
},
computed: {
payUrl () {
const baseUrl = 'https://openapi.alipay.com/gateway.do?'
const appId = 'APP_ID'
const charset = 'utf-8'
const signType = 'RSA2'
const timestamp = this.getTimestamp()
const version = '1.0'
const method = 'alipay.trade.page.pay'
const notifyUrl = 'http://localhost:8080/notify_url'
const returnUrl = 'http://localhost:8080/return_url'
const bizContent = {
out_trade_no: this.outTradeNo,
product_code: 'FAST_INSTANT_TRADE_PAY',
total_amount: this.totalAmount.toFixed(2),
subject: this.subject,
body: this.body
}
const sign = this.getSign({ appId, charset, bizContent, method, notifyUrl, returnUrl, signType, timestamp, version })
const url = `${baseUrl}app_id=${appId}&method=${method}&charset=${charset}&sign_type=${signType}×tamp=${timestamp}&version=${version}¬ify_url=${notifyUrl}&return_url=${returnUrl}&biz_content=${encodeURIComponent(JSON.stringify(bizContent))}&sign=${encodeURIComponent(sign)}`
return url
}
},
methods: {
getTimestamp () {
return new Date().toISOString().replace(/-/g, '').replace(/:/g, '').replace(/\..+/g, '')
},
getSign ({ appId, charset, bizContent, method, notifyUrl, returnUrl, signType, timestamp, version }) {
const privateKey = 'APP_PRIVATE_KEY'
const content = Object.keys(bizContent).sort().map(key => `${key}=${bizContent[key]}`).join('&')
const signStr = `${content}&app_id=${appId}&charset=${charset}&method=${method}¬ify_url=${notifyUrl}&return_url=${returnUrl}&sign_type=${signType}×tamp=${timestamp}&version=${version}`
const sign = new JSEncrypt()
sign.setPrivateKey(privateKey)
return sign.sign(signStr, CryptoJS.SHA256, signType)
}
}
}
</script>
以上代码中,需要替换掉APP_ID、APP_PRIVATE_KEY等参数为自己的支付宝应用信息。同时还需要引入JSEncrypt和CryptoJS库来进行签名算法的计算。在使用前需要先安装这两个库:
npm install jsencrypt crypto-js
在以上代码中,将支付宝支付所需的参数拼接成URL,然后将URL作为iframe的src属性值展示出来,用户点击支付后即可在支付页面完成支付
原文地址: https://www.cveoy.top/t/topic/d2ru 著作权归作者所有。请勿转载和采集!