How to implement Java payment function?
The simplest payment SDK in history
First let’s take a look at the API:
package com.joe.pay; import com.joe.pay.pojo.*; /** * 支付服务接口 * * @author joe * @version 2018.06.29 11:32 */ public interface PayService { /** * 调用第三方支付 * * @param request 支付参数 * @return 支付结果 */ SysResponse<PayResponse> pay(PayRequest request); /** * 申请退款 * * @param request 退款请求 * @return 退款响应 */ SysResponse<RefundResponse> refund(RefundRequest request); }
As you can see, the PayService interface provides payment methods and refund methods. Let’s look at the parameter definitions of the payment methods:
package com.joe.pay.pojo; import lombok.Data; /** * 支付参数,除了openid外所有参数必填 * * @author joe * @version 2018.07.02 10:36 */ @Data public class PayRequest implements Request{ /** * 商户网站唯一订单号 */ private String outTradeNo; /** * 对一笔交易的描述信息 */ private String body; /** * 商品的标题/交易标题/订单标题/订单关键字等。 * <p> * 对于微信支付来说需传入应用市场上的APP名字-实际商品名称,天天爱消除-游戏充值。 */ private String subject; /** * 订单生成时间,格式为yyyy-MM-dd HH:mm:ss */ private String createTime; /** * 支付时长,单位为秒,如果设置300就是五分钟,超过五分钟订单自动取消 */ private int expire = 300; /** * 订单金额,单位为分 */ private int totalAmount; /** * 发起请求的用户的实际IP */ private String ip; /** * 支付类型:APP、WEB(微信对应公众号,支付宝对应网页支付) */ private String tradeType = "APP"; /** * 用户openid,只有在微信公众号支付时需要 */ private String openid; /** * 附加数据,会与结果一并返回 */ private String attach; }
OK, I believe that the above parameters can be understood clearly and intuitively even by students who have never made a payment. They are some basic order information. Let’s look at the definition of refund parameters:
package com.joe.pay.pojo; import lombok.Data; import org.hibernate.validator.constraints.NotEmpty; import javax.validation.constraints.Size; /** * 退款请求 * * @author joe * @version 2018.07.05 17:12 */ @Data public class RefundRequest { /** * 第三方账单ID,与outTradeNo不能同时为空 */ private String orderId; /** * 商户系统订单ID,与orderId不能同时为空 */ private String outTradeNo; /** * 商户退款单号 * <p> * 退款支持单笔交易分多次退款,多次退款需要提交原支付订单的商户订单号和设置不同的退款单号。申请退款总金 * <p> * 额不能超过订单金额。 一笔退款失败后重新提交,请不要更换退款单号,请使用原商户退款单号。 */ @NotEmpty(message = "商户退款单号不能为空") private String outRefundNo; /** * 订单总金额,单位为分 */ @Size(min = 1) private int totalFee; /** * 退款金额,不能大于订单总金额 * <p> * 注:多次退款总金额不能大于订单总金额 */ @Size(min = 1) private int refundFee; /** * 退款原因 * <p> * 可以为空 */ private String refundDesc; }
The refund parameters are even simpler and require quite little information. With this in place, we can look at the payment test code:
package com.joe.pay; import com.joe.pay.pojo.*; import com.joe.pay.pojo.prop.PayProp; import com.joe.utils.common.DateUtil; import com.joe.utils.common.Tools; import org.junit.Assert; import org.junit.Before; import org.junit.Test; /** * 支付测试 * * @author joe * @version 2018.07.02 14:38 */ public class PayServiceTest { /** * 微信配置 */ private PayProp wxProp; private PayProp aliProp; /** * 微信服务 */ private PayService wxPayService; private PayService aliPayService; @Before public void init() { //配置微信支付,需要将下列参数替换为自己的参数 wxProp = PayProp.builder() .appid("123123123") .notifyUrl("http://www.baidu.com") .useWxPay() .certInput(null) .key("123123123") .mchId("123123123123") .password("123123123123") .build(); wxPayService = PayServiceFactory.getInstance(wxProp); String privateKey = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDEuQvaAad8+PAUVLeh9tqNsWNDCiAXEal4krM0E1" + "4oHiUvzOT8Xf5BYZeMmiA+G+Z4iT2dP6bNRBIth7W5COh51v8TDf4dYxqV3Jw7jyQF2K/p74izFXm96yDXz3cMotRdDW3JdcC" + "ME2tgrlSjJxN26qPCrHh+OL9Y/yqJGhsAHKlaIYQ4S8F9B3rIPMAv6MbTuHFrFXkwuJqesupTwUqzmPgriy/2Au9ZApnpVCex" + "sKIkjMwpmOe3FxaQ9U6271NA9jmzbg7ge5BlfblQL3lIkEQdoqPPKrfLbO3djN1ORn1vhKrIAUhIQAqWKDVdz9dGxrIZckGOe" + "8iuUGOs8D4VAgMBAAECggEBAIzyuJcekE5uXjVy7Y9SOw1Ch5hE/PEKao5FLbim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ejZVIJhK5q60KLV5n/HMxuHTGRZxpbfcH4/44d3Yz7ieccmCiPtqxFYUwP" + "W0JukNWDL2tOzTSZ5ABAwqorV3bM67mLUSt0O5dL4YtSBJ2J3F6joO1fTZEcUCybO/A5J4wTZDOV1AR"; //配置支付宝支付,需要将下列参数替换为自己的参数 aliProp = PayProp.builder() .appid("123123123") .notifyUrl("http://www.baidu.com") .useAliPay() .publicKey("123123") .privateKey(privateKey) .build(); aliPayService = PayServiceFactory.getInstance(aliProp); } /** * 测试微信支付 */ @Test public void doWxPay() { doPay(wxPayService); } /** * 测试支付宝支付 */ @Test public void doAliPay() { doPay(aliPayService); } /** * 测试微信退款 * <p> * 注意:该测试当前通不过,因为没有提供正确的证书,若想要测试通过则需要在配置文件中提供正确的微信证书和单号 */ @Test public void doWxRefund() { doRefund(wxPayService); } /** * 测试支付宝退款 * <p> * 注意:该测试当前通不过,若想要测试通过则需要提供正确的单号 */ @Test public void doAliRefund() { doRefund(aliPayService); } /** * 校验响应 * * @param response 响应 */ private void check(SysResponse<? extends BizResponse> response) { Assert.assertTrue(response.isSuccess()); Assert.assertTrue(response.getData().isSuccess()); } /** * 发起支付请求 * * @param service service */ private void doPay(PayService service) { SysResponse<PayResponse> response = service.pay(buildPay()); check(response); } /** * 发起退款请求 * * @param service service */ private void doRefund(PayService service) { SysResponse<RefundResponse> response = service.refund(buildRefund()); System.out.println("结果是:" + response); } /** * 构建一个支付订单 * * @return 支付订单 */ private PayRequest buildPay() { PayRequest payRequest = new PayRequest(); payRequest.setOutTradeNo(Tools.createUUID()); payRequest.setBody("天天爱消除-游戏充值"); payRequest.setSubject("天天爱消除-游戏充值"); payRequest.setCreateTime(DateUtil.getFormatDate(DateUtil.BASE)); payRequest.setTotalAmount(100 * 10); payRequest.setIp("106.120.141.226"); return payRequest; } /** * 构建一个退款订单 * * @return 退款订单 */ private RefundRequest buildRefund() { RefundRequest request = new RefundRequest(); request.setOutTradeNo("123456"); request.setOutRefundNo("123456"); request.setTotalFee(100 * 10); request.setRefundFee(100); return request; } }
The above is the detailed content of How to implement Java payment function?. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In this article, we have kept the most asked Java Spring Interview Questions with their detailed answers. So that you can crack the interview.

Java 8 introduces the Stream API, providing a powerful and expressive way to process data collections. However, a common question when using Stream is: How to break or return from a forEach operation? Traditional loops allow for early interruption or return, but Stream's forEach method does not directly support this method. This article will explain the reasons and explore alternative methods for implementing premature termination in Stream processing systems. Further reading: Java Stream API improvements Understand Stream forEach The forEach method is a terminal operation that performs one operation on each element in the Stream. Its design intention is

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

Capsules are three-dimensional geometric figures, composed of a cylinder and a hemisphere at both ends. The volume of the capsule can be calculated by adding the volume of the cylinder and the volume of the hemisphere at both ends. This tutorial will discuss how to calculate the volume of a given capsule in Java using different methods. Capsule volume formula The formula for capsule volume is as follows: Capsule volume = Cylindrical volume Volume Two hemisphere volume in, r: The radius of the hemisphere. h: The height of the cylinder (excluding the hemisphere). Example 1 enter Radius = 5 units Height = 10 units Output Volume = 1570.8 cubic units explain Calculate volume using formula: Volume = π × r2 × h (4

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python each have their own advantages and are suitable for different scenarios. 1.PHP is suitable for web development and provides built-in web servers and rich function libraries. 2. Python is suitable for data science and machine learning, with concise syntax and a powerful standard library. When choosing, it should be decided based on project requirements.

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.
