java抖音支付

后端代码

package com.alsark.paydemo.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

@RestController
public class PayController {

    //sign签名
    public final static List<String> REQUEST_NOT_NEED_SIGN_PARAMS = Arrays.asList("app_id", "thirdparty_id", "sign", "other_settle_params");
    private static final String SALT = "JEwfiklDXIfPw4nl0lT15cpmJlreMPIMQOJbTOpT";

    public static String requestSign(Map<String, Object> paramsMap) {
        List<String> paramsArr = new ArrayList<>();
        for (Map.Entry<String, Object> entry : paramsMap.entrySet()) {
            String key = entry.getKey();
            if (REQUEST_NOT_NEED_SIGN_PARAMS.contains(key)) {
                continue;
            }
            String value = entry.getValue().toString();

            value = value.trim();
            if (value.startsWith("\"") && value.endsWith("\"") && value.length() > 1) {
                value = value.substring(1, value.length() - 1);
            }
            value = value.trim();
            if (value.equals("") || value.equals("null")) {
                continue;
            }
            paramsArr.add(value);
        }
        paramsArr.add(SALT);
        Collections.sort(paramsArr);
        StringBuilder signStr = new StringBuilder();
        String sep = "";
        for (String s : paramsArr) {
            signStr.append(sep).append(s);
            sep = "&";
        }
        return md5FromStr(signStr.toString());
    }

    private static String md5FromStr(String inStr) {
        MessageDigest md5;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return "";
        }

        byte[] byteArray = inStr.getBytes(StandardCharsets.UTF_8);
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuilder hexValue = new StringBuilder();
        for (byte md5Byte : md5Bytes) {
            int val = ((int) md5Byte) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }

    //预下单接口
    @GetMapping("/pay")
    public String pay() {

        long currentTimestamp = System.currentTimeMillis();
        Map<String, Object> testCase = new HashMap<>() {
            {
                put("app_id", "ttb905cfb8263a12dc01");
                put("out_order_no", "noncestr" + currentTimestamp);
                put("total_amount", 1);
                put("subject", "抖音商品");
                put("body", "抖音商品");
                put("valid_time", 300);
                put("notify_url", "https://app.mujuapi.cn/api/pay/notify");
            }
        };
        testCase.put("sign", requestSign(testCase));

        HttpResponse<String> response = null;
        try {
            String url = "https://developer.toutiao.com/api/apps/ecpay/v1/create_order";
            HttpClient httpClient = HttpClient.newHttpClient();
            ObjectMapper objectMapper = new ObjectMapper();
            String jsonPayload = objectMapper.writeValueAsString(testCase);

            HttpRequest request = HttpRequest.newBuilder()
                    .uri(URI.create(url))
                    .header("Content-Type", "application/json")
                    .POST(HttpRequest.BodyPublishers.ofString(jsonPayload, StandardCharsets.UTF_8))
                    .build();

            response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());

        } catch (Exception e) {
            e.printStackTrace();
        }


        if (response != null) {
            return response.body();
        } else {
            return null;
        }

    }


}

 使用

src/main/java/com/alsark/paydemo/controller/PayController.java

示例为springboot3 jdk17

替换我画横线的地方即可完成测试

开源地址

 gitee java_抖音_支付: java抖音小程序支付icon-default.png?t=O83Ahttps://gitee.com/alsark/java-tiktok-payment.git

github https://github.com/alsarkc/java-tiktok-payment.giticon-default.png?t=O83Ahttps://github.com/alsarkc/java-tiktok-payment.git 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值