| | |
| | | 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 com.stylefeng.guns.modular.system.util.Tingg.model.CheckoutRequest; |
| | | import io.cellulant.model.Payload; |
| | | import io.cellulant.service.CheckoutEncryption; |
| | | |
| | | import java.net.HttpURLConnection; |
| | | import java.util.Calendar; |
| | | import java.util.HashMap; |
| | | import java.util.Map; |
| | | import java.util.TimeZone; |
| | | import java.text.SimpleDateFormat; |
| | | import java.util.*; |
| | | |
| | | /** |
| | | * Tingg支付工具类 |
| | | */ |
| | | @Component |
| | | public class TinggPayUtil { |
| | | |
| | | @Autowired |
| | | private HttpClientUtil httpClientUtil; |
| | | private static String accessKey = "KxjPP444jEE7K88E7juej4PKqqzKq7qKjKj84q744q9zj4Ej4zK47uj4KKj4"; |
| | | |
| | | private static String ivKey = "qsffKsCOJJdhSBCQ"; |
| | | |
| | | private static String secretKey = "9jjz4Ex74P8ue4qK"; |
| | | |
| | | |
| | | /** |
| | | * 获取token |
| | | * 获取支付数据 |
| | | * @param checkoutRequest |
| | | * @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; |
| | | public static ResultUtil checkoutRequest(CheckoutRequest checkoutRequest){ |
| | | try { |
| | | CheckoutEncryption checkoutEncrption = new CheckoutEncryption(ivKey, secretKey); |
| | | Payload payload = getPayload(checkoutRequest); |
| | | String param = checkoutEncrption.encrypt(payload); |
| | | System.out.println("Encrpted payload=" + param); |
| | | String url = "https://online.uat.tingg.africa/testing/express/checkout?encrypted_payload=" + param + "&access_key=" + accessKey; |
| | | return ResultUtil.success(url); |
| | | }catch (Exception e){ |
| | | e.printStackTrace(); |
| | | return ResultUtil.runErr(); |
| | | } |
| | | 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"); |
| | | public static Payload getPayload(CheckoutRequest checkoutRequest) { |
| | | SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); |
| | | String countryCode="GHA";//收取付款的国家的3位ISO代码 |
| | | String currencyCode = "GHS";//3位ISO代码的货币,商家正在开发票。 |
| | | String serviceCode="IGOGHANA";//服务代码 |
| | | String dueDate = sdf.format(new Date(System.currentTimeMillis() + 1800000));//到期时间 |
| | | String languageCode="EN"; |
| | | String paymentOptionCode = "";//支付选项码 |
| | | |
| | | String s = httpClientUtil.pushHttpRequset("POST", TinggConfigEnum.CHECKOUT_URL.getValue(), params, header, "json"); |
| | | if(ToolUtil.isNotEmpty(s)){ |
| | | Payload payload = new Payload(checkoutRequest.getMerchantTransactionId(), checkoutRequest.getCustomerFirstName(), checkoutRequest.getCustomerLastName(), checkoutRequest.getMsisdn(), checkoutRequest.getCustomerEmail(), |
| | | checkoutRequest.getRequestAmount(), currencyCode, checkoutRequest.getAccountNumber(), serviceCode, dueDate, checkoutRequest.getRequestDescription(), |
| | | countryCode, languageCode, paymentOptionCode, checkoutRequest.getSuccessRedirectUrl(), checkoutRequest.getFailRedirectUrl(), checkoutRequest.getPendingRedirectUrl(), checkoutRequest.getCallbackUrl()); |
| | | return payload; |
| | | } |
| | | |
| | | } |
| | | |
| | | public static void main(String[] ages){ |
| | | CheckoutRequest checkoutRequest = new CheckoutRequest(); |
| | | checkoutRequest.setMsisdn(233240000000L); |
| | | checkoutRequest.setCustomerEmail("393733352@qq.com"); |
| | | checkoutRequest.setAccountNumber("4111111111111111"); |
| | | checkoutRequest.setCustomerFirstName("zhibing"); |
| | | checkoutRequest.setCustomerLastName("pu"); |
| | | checkoutRequest.setRequestAmount(1.00D); |
| | | checkoutRequest.setMerchantTransactionId("123456T"); |
| | | checkoutRequest.setRequestDescription("payment test"); |
| | | checkoutRequest.setCallbackUrl("https://10pz685243.zicp.fun"); |
| | | checkoutRequest.setPendingRedirectUrl("https://10pz685243.zicp.fun"); |
| | | checkoutRequest.setSuccessRedirectUrl("https://10pz685243.zicp.fun"); |
| | | checkoutRequest.setFailRedirectUrl("https://www.baidu.com"); |
| | | ResultUtil resultUtil = TinggPayUtil.checkoutRequest(checkoutRequest); |
| | | System.err.println(JSON.toJSONString(resultUtil)); |
| | | } |
| | | } |