java personal payment system (springboot)

Posted by chuddyuk on Mon, 12 Aug 2019 11:48:04 +0200

With the widespread development of mobile payment, the current situation of Alipay and WeChat's two largest payment systems has basically been formed in the market. Therefore, the major businesses, websites, platforms and so on have access to Alipay and WeChat as payment methods. However, as a developer, we all know that the WeChat payment interface has never been seen from beginning to end. To personal terminal users, it can only be access through enterprise account, and Alipay also closed personal payment interface in the past few years. This can be distressed our individual households ah, clearly thought of a way to make money, but it is impossible to directly put the benefits into their pockets, the manual operation is too unrealistic, and we can not register a business and then apply for access, that.... What should we do? Is there no way? The answer is yes, that is, Alipay's face-to-face payment.

So how to operate it? Following are detailed steps:

1. Apply for business

Open Alipay APP search "ant gold clothing business platform"

Click Open Payment

Click to sign the contract immediately

Fill in relevant information

Note: Now the rate seems to be 0.6%, please check the official documents.

Then we wait for the audit. It's about a working day.

2. Managing signature key information

After successful signing, login to Ant Golden Clothes Open Platform and click on the top right-hand image to enter secret key management

The private key and public key are configured according to the prompt, and the private key is downloaded from the Alipay RAS key generator according to the prompt.

Note that the 2 configurations used in the code have been circled, the public key is the Alipay public key, the non application public key, and one is for the private key content in the figure above, using the editing tool to open the private key content and copy it.

3. Development and Access

Introducing maven dependencies

<dependency>
    <groupId>com.alipay.sdk</groupId>
    <artifactId>alipay-sdk-java</artifactId>
    <version>3.7.26.ALL</version>
</dependency>

Alipay Controller, fill in your configuration

First generate the order number call to generate two-dimensional code, then query the payment status of the order number, see the official documents for details.

 /**
     * Generating two-dimensional codes
     *
     * @param rechargeRecord
     * @return
     * @throws AlipayApiException
     */
    @PostMapping("/precreate")
    @ResponseBody
    public AjaxResult getPayQrcode(ZyRechargeRecord rechargeRecord) {
        //Get the user ID of the current session here
//        Long userId = ShiroUtils.getUserId();
        Long userId = 101L;
        String orderNo = UUID.randomUUID().toString();
        rechargeRecord.setUserId(userId.intValue());
        rechargeRecord.setOrderNo(orderNo);
        rechargeRecord.setCreateTime(new Date());
        rechargeRecord.setRechargePoint(rechargeRecord.getRechargeMoney());
        //Order warehousing
        rechargeRecordService.save(rechargeRecord);
        AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do",
                appId, privateKey, "json", "GBK", publicKey, "RSA2");
        AlipayTradePrecreateRequest r = new AlipayTradePrecreateRequest();
        r.setBizContent("{" +
                "\"out_trade_no\":\"" + rechargeRecord.getOrderNo() + "\"," +
                "\"total_amount\":" + rechargeRecord.getRechargeMoney() + "," +
                "\"subject\":\"Xiaofeng Resource Sharing Platform-Integral recharge\"" +
                "  }");
        AlipayTradePrecreateResponse response = null;
        try {
            response = alipayClient.execute(r);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if (!response.isSuccess()) {
            return AjaxResult.error("Call Alipay interface to generate two-dimensional code failure");
        }
        Map<String, Object> result = new HashMap<>(16);
        result.put("id", rechargeRecord.getOrderNo());
        result.put("qrCode", response.getQrCode());
        return AjaxResult.success(result);
    }

    /**
     * Query Payment Results
     *
     * @param outTradeNo
     * @return
     * @throws AlipayApiException
     */
    @PostMapping("/query/{outTradeNo}")
    @ResponseBody
    public AjaxResult queryPayState(@PathVariable String outTradeNo) throws AlipayApiException {
        boolean payState = alipayService.getPayState(outTradeNo, appId, privateKey, publicKey);
        return payState ? AjaxResult.success(1) : AjaxResult.error();
    }

Finally, check whether the bill is missing by the way of timed tasks, and update the status of payment in time (job under task package)

So far personal payment has been docked successfully, you can access to your own project, immediately start your payment journey!

The results are as follows:

 

 

 

Complete code download address:

Real Payment Demonstration Address:

Topics: SDK Mobile Maven Java