puzhibing
2022-09-29 e28d33c09405e246a2d75fcb1f69a9e8e9d911b8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package com.stylefeng.guns.modular.system.util.Tingg;
 
import com.alibaba.fastjson.JSON;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.util.HttpClientUtil;
import com.stylefeng.guns.modular.system.util.ResultUtil;
import com.stylefeng.guns.modular.system.util.Tingg.model.TokenResponse;
import org.apache.http.protocol.HTTP;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import java.net.HttpURLConnection;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;
import java.util.TimeZone;
 
/**
 * Tingg支付工具类
 */
@Component
public class TinggPayUtil {
 
    @Autowired
    private HttpClientUtil httpClientUtil;
 
    /**
     * 获取token
     * @return
     */
    public TokenResponse getToken(){
        Map<String, Object> params = new HashMap<>();
        params.put("grant_type", "client_credentials");
        params.put("client_id", TinggConfigEnum.CLIENT_ID.getValue());
        params.put("client_secret", TinggConfigEnum.CLIENT_SECRET.getValue());
 
        String s = httpClientUtil.pushHttpRequset("POST", TinggConfigEnum.TOKEN_URL.getValue(), params, null, "json");
        if(ToolUtil.isNotEmpty(s)){
            TokenResponse tokenResponse = JSON.parseObject(s, TokenResponse.class);
            return tokenResponse;
        }
        return null;
    }
 
 
 
    public ResultUtil checkoutRequest(String phone, String email, Double amount){
        Map<String, Object> params = new HashMap<>();
        params.put("merchantTransactionID", "");//商家的唯一交易标识符
        params.put("currencyCode", "");//通过的金额所使用的货币
        params.put("requestAmount", amount);//客户将要支付的总金额
        params.put("countryCode", "");//商家的默认国家代码
        params.put("accountNumber", "");//交易的帐号/参考编号
        params.put("serviceCode", "");//商户的服务代码
        Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        params.put("dueDate", "");//事务到期日期的格式为YYYY-MM-DD HH:mm:ss。这应该是UTC时间
        params.put("requestDescription", "");//事务的叙述
        params.put("customerFirstName", "");//顾客名称
        params.put("customerLastName", "");//客户的姓氏
        params.put("MSISDN", phone);//客户的手机号码
        params.put("customerEmail", email);//客户的邮件
        params.put("paymentWebhookUrl", "");//这是结帐将使用的URL,用相关的支付细节调用商家,以便商家确认支付
 
        Map<String, String> header = new HashMap<>();
        TokenResponse token = getToken();
        if(null == token || ToolUtil.isNotEmpty(token.getError())){
            System.err.println("调用支付异常,获取token凭证失败!【" + token.getError() + "-----" + token.getMessage() + "】");
            return ResultUtil.error("调用支付异常");
        }
        header.put("Authorization", "Bearer " + token.getAccess_token());
        header.put("Content-Type", "application/json");
 
        String s = httpClientUtil.pushHttpRequset("POST", TinggConfigEnum.CHECKOUT_URL.getValue(), params, header, "json");
        if(ToolUtil.isNotEmpty(s)){
 
        }
    }
}