In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail how Java uses sandbox payment to realize computer code scanning payment. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
I. preparatory work
1. Sign up for the Alipay open platform account and become a developer.
Address: https://open.alipay.com/platform/home.htm
2. Enter the sandbox and configure it.
3. We can see this interface.
4. Parameters to be used later
APPID
Merchant private key (use the public key mode of the system default key, click View to get)
Alipay public key
Alipay gateway
5. Download sandbox Alipay on mobile phone (pay with this Alipay at that time)
6. After downloading the Alipay sandbox version, log in to the account provided by Alipay
Second, effect display
1. A handwritten foreground vue interface
2. Enter the confirm order page and add a description of the product.
3. Jump to the payment interface and use sandbox Alipay to complete the payment
4. Complete the payment
5. Jump back to the interface set by yourself
Third, implementation code 3.1 background code
(I use SpringBoot integrated SSM here, of course, I don't have to use SpringBoot.)
3.1.1 pom.xml Fil
Not all, the key points are: Alipay SDK package, fastjson
4.0.0 com.zking springboot02 0.0.1-SNAPSHOT springboot02 Demo project for Spring Boot 1.8 UTF-8 UTF-8 2.1.18.RELEASE org.springframework.boot spring-boot-starter-jdbc org.springframework.boot spring-boot-starter-web Org.mybatis.spring.boot mybatis-spring-boot-starter 2.1.4 mysql mysql-connector-java 5.1.44 runtime org.projectlombok lombok true Org.springframework.boot spring-boot-starter-test test org.junit.jupiter junit-jupiter RELEASE test com.github.pagehelper pagehelper-spring-boot-starter 1.4.1 org.springframework.boot Spring-boot-starter-aop 2.6.2 com.alipay.sdk alipay-sdk-java 3.1.0 com.alibaba fastjson 1.2.48 org.springframework. Boot spring-boot-starter-data-redis com.alibaba druid 1.2.6 log4j log4j 1.2.17 org.springframework.boot Spring-boot-dependencies ${spring-boot.version} pom import org.apache.maven.plugins maven-compiler-plugin 3.8.1. 8 1.8 UTF-8 org.mybatis.generator mybatis-generator-maven-plugin 1.3.2 true true Src/main/resources/generatorConfig.xml org.springframework.boot spring-boot-maven-plugin 2.1.18.RELEASE com.zking.springboot02.Springboot02Application Repackage repackage
3.1.2 model package
Package com.zking.springboot02.model;import lombok.Data;/** * payment entity object * according to the Alipay interface agreement, the attribute name must be underlined and cannot be modified * * @ author lend me a wonderful pen * / @ Datapublic class AlipayBean {private static final long serialVersionUID = 1L; / * merchant order number, required * / private String out_trade_no / * * order name, required * / private String subject; / * * payment amount, required * according to Alipay interface agreement, you must use an underscore * / private String total_amount; / * * product description, but empty * / private String body / * timeout parameter * / private String timeout_express= "10m"; / * Product number * / 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) {this.total_amount = total_amount;} public String getBody () {return body } public void setBody (String body) {this.body = body;}}
3.1.3 utils package AlipayConfig class
(please configure this class to prevent errors)
AppId:APPID, provided by sandboxed applications
PrivateKey: merchant's private key. Click the public key certificate to view it.
ReturnUrl: the page that is redirected after the payment is completed. For example, I filled in: http://localhost:8088/
Package com.zking.springboot02.utils;import lombok.Data;import org.springframework.context.annotation.Configuration;import org.springframework.context.annotation.PropertySource;import org.springframework.stereotype.Component;/** * configuration file read * * / @ Configuration@Data@Componentpublic class AlipayConfig {/ * App ID, your APPID, the receipt account is your APPID corresponding Alipay account * / private String appId = "" / * merchant private key, your RSA2 private key in PKCS8 format * / private String privateKey = ""; / * Alipay public key, * / private String publicKey = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyqAN9WzWigim0/3fBK97RFZ7Juu31+DfXMVHTHSTP+4WPvr80zTiIQmT9xTFVGBgD8BBX0XELxqLQxsYQm/MgEgccHTnCKPP7Ci979YuwZyjOysdTc6BNO/6RqPZruih7wSYDJNuJUgY/hwuWi+owUDbHL7NvZ8r/TaIJvEzzhJVrTMsIBQBe66LRE7gE2avwEV8Qck9e4yexsDUD7ja1+2T1ltfHAP2u/SBOD+7PkkPgVkINPDHt4bXZ9DIhPhosiw8IidEEniXj/Ku1wtgETll/btJljhhXq98JHBlw94+yx+BQ+9s2S2CjXkxfdZDB9s+jFy80e6UIV76xxfB0QIDAQAB" / * * the asynchronous notification page path of the server requires a full path in http:// format, and cannot add custom parameters such as? id=123 * / private String notifyUrl = "https://www.duan33f.top"; / * page jump synchronous notification page path requires a full path in http:// format. * address returned after payment * / private String returnUrl = ""; / * * signature method * / private String signType = "RSA2"; / * * character encoding format * / private String charset = "utf-8"; / * * Alipay gateway * / private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";" / * * Alipay Gateway * / private String logPath = "C:\";}
Alipay class
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.alipay.api.request.AlipayTradePagePayRequest;import com.zking.springboot02.model.AlipayBean;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import javax.annotation.Resource / * * Alipay payment interface * @ author lends me a wonderful pen * / @ Componentpublic class Alipay {@ Autowired private AlipayConfig alipayConfig; / * payment interface * @ param alipayBean * @ return * @ throws AlipayApiException * / public String pay (AlipayBean alipayBean) throws AlipayApiException {/ / 1, get the initialized AlipayClient String serverUrl = alipayConfig.getGatewayUrl (); String appId = alipayConfig.getAppId () String privateKey = alipayConfig.getPrivateKey (); String format = "json"; String charset = alipayConfig.getCharset (); String alipayPublicKey = alipayConfig.getPublicKey (); String signType = alipayConfig.getSignType (); String returnUrl = alipayConfig.getReturnUrl (); String notifyUrl = alipayConfig.getNotifyUrl (); / / System.out.println (appId); AlipayClient alipayClient = new DefaultAlipayClient (serverUrl, appId, privateKey, format, charset, alipayPublicKey, signType) / 2. Set request parameter AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest (); / / Page Jump synchronous Notification Page path alipayRequest.setReturnUrl (returnUrl); / / Server Asynchronous Notification Page path alipayRequest.setNotifyUrl (notifyUrl); / / Encapsulation Parameter alipayRequest.setBizContent (JSON.toJSONString (alipayBean)) / / 3. Request Alipay to make payment and get the payment result String result = alipayClient.pageExecute (alipayRequest). GetBody (); / / return payment information return result;}}
3.1.4 Service package
PayService interface
Package com.zking.springboot02.service;import com.alipay.api.AlipayApiException;import com.zking.springboot02.model.AlipayBean;/** * payment Service * @ author lent me a wonderful pen * @ date Dec 12, 2018 * / public interface PayService {/ * Alipay Interface * @ param alipayBean * @ return * @ throws AlipayApiException * / String aliPay (AlipayBean alipayBean) throws AlipayApiException;}
PayServiceImpl class
Package com.zking.springboot02.service.impl;import com.alipay.api.AlipayApiException;import com.zking.springboot02.model.AlipayBean;import com.zking.springboot02.service.PayService;import com.zking.springboot02.utils.Alipay;import org.springframework.stereotype.Service;import javax.annotation.Resource;@Servicepublic class PayServiceImpl implements PayService {@ Resource private Alipay alipay; @ Override public String aliPay (AlipayBean alipayBean) throws AlipayApiException {return alipay.pay (alipayBean);}}
3.1.5 contorller package
PayController class
Package com.zking.springboot02.contorller;import com.alipay.api.AlipayApiException;import com.zking.springboot02.model.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") @ CrossOriginpublic class PayController {@ Resource private PayService payService; / * Ali pay * @ param alipayBean * @ return * @ throws AlipayApiException * / @ PostMapping ("/ alipay") public Map alipay (AlipayBean alipayBean) throws AlipayApiException {System.out.println (alipayBean); Map map = new HashMap (); String str = payService.aliPay (alipayBean) System.out.println (str); map.put ("msg", str); map.put ("code", 0); / / return map;}}
3.1.6 application.yml Fil
Server: port: 8080 # Port number servlet: context-path: / S02 # Project name 3.2 foreground code
(the front desk is a Vue project built with scaffolding.)
3.2.1 action.js file under api package under src
/ * the URL format for the address requested by the backend is as follows: * module name _ entity name _ operation * / export default {/ / server 'SERVER':' http://localhost:8080/s02', 'address:' http://localhost:8080/s02/pay/alipay', / / get the full address of the request Use 'getFullPath': k = > {return this.SERVER + this [k] for mockjs testing }}
3.2.2 http.js file under api package under src
/ * the vue project introduces the action module to the global configuration of axios * / import axios from 'axios'import qs from' qs'// and adds it to the class attribute urls of axios: import action from'@ / api/action'axios.urls = action// axios default configuration axios.defaults.timeout = 10000; / / timeout / / axios.defaults.baseURL = 'http://localhost:8080/crm'; / / default address axios.defaults.baseURL = action.SERVER / / data processing / / for POST,PUT,PATCH only, transformRequest` allows you to modify request data axios.defaults.transformRequest = function (data) {data = qs.stringify (data); return data;}; / / request interceptor axios.interceptors.request.use (function (config) {/ / let jwt = sessionStorage.getItem ('jwt') before sending it to the server) / / if (jwt) {/ / config.headers ['jwt'] = jwt; / /} return config;}, function (error) {return Promise.reject (error);}); / / response interceptor axios.interceptors.response.use (function (response) {return response;}, function (error) {return Promise.reject (error);}); export default axios
3.2.3 index.js file under router under src
Import Vue from 'vue'import Router from' vue-router'import Shop from'@ / views/Shop'import buy from'@ / views/buy'Vue.use (Router) export default new Router ({routes: [{path:'/', name: 'Shop', component: Shop}, {path:' / buy', name: 'buy', component: buy}]})
3.2.4 Shop.vue file under views under src
To pay export default {name: "Shop", data: function () {return {tableData: [{out_trade_no: '101 databases, subject:' Java from entry to burial', total_amount: 33}, {out_trade_no: '202 databases, subject:' Mysql removal Guide' Total_amount: 44}, {out_trade_no: '303 programming, subject:' Java programming ideas', total_amount: 89}, {out_trade_no: '404 programming, subject:' Java Design pattern', total_amount: 56}]} }, methods: {toBuy (row) {console.log (row) / / use $router.push to jump this.$router.push ({/ / path is followed by the jump route address path:'/ buy', / / name followed by the jump route name (must be tested in person) Name: 'buy', params: {/ / imgsListsUrl2 is the name defined by yourself, and this.imgsListsUrl is the value to be passed payInfo: row})}}, created: function () {}}
3.2.5 buy.vue file under views under src
Confirm order payment / / import axios from 'axios' export default {name: "buy" Data () {return {payInfo: {out_trade_no:', subject:', total_amount: null, body:'}, mounted () {/ / this.$route.params.imgsListsUrl2 is the passed parameter var time = new Date () This.payInfo = this.$route.params.payInfo this.payInfo.out_trade_no = time.getTime ();}, methods: {submit () {var url = this.axios.urls.alipay;// get alipay this.axios.post (url, this.payInfo) in action.js under api. Then (resp = > {/ / call background method console.log (resp)) Const divForm = document.getElementsByTagName ('div') if (divForm.length) {document.body.removeChild (divForm [0])} const div = document.createElement (' div') div [XSS _ clean] = resp.data.msg / / data is the form form string document.body.appendChild (div) document.forms [0] .setAttribute ('target') returned by the API '_ blank') / / New window Jump document.forms [0] .submit ()}) .catch (resp = > {console.log (resp)) });} .box {width: 800px;} .left {width: 85px; padding-top: 5px; font-size: 15px;} .right {width: 400px;}
3.2.6 main.js file under src
Import Vue from 'vue'import ElementUI from' element-ui' / / add 1import 'element-ui/lib/theme-chalk/index.css' / / add 2 to avoid different packaging styles in the later stage, put it in import App from'. / App' Previous import axios from'@ / api/http' / / vue project global configuration of axios import App from'. / App'import router from'. / router'import VueAxios from 'vue-axios' Vue.use (VueAxios,axios) Vue.use (ElementUI) / / add 3Vue.config.productionTip = false/* eslint-disable no-new * / new Vue ({el:' # app', router, components: {App}, template:''})
OK, the important code has been fully provided.
This is the end of the article on "how Java uses sandbox payment to realize computer code scanning payment". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.