AirwallexServiceImpl.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. package com.szwl.service.impl;
  2. import com.alibaba.fastjson.JSON;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.szwl.constant.AirwallexConstant;
  5. import com.szwl.model.bean.PaymentIntent;
  6. import com.szwl.model.bean.PaymentIntentRequestBody;
  7. import com.szwl.model.bo.R;
  8. import com.szwl.model.entity.TOrderAbroad;
  9. import com.szwl.model.utils.HttpClientSslUtils;
  10. import com.szwl.service.AirwallexService;
  11. import com.szwl.service.TOrderAbroadService;
  12. import lombok.extern.slf4j.Slf4j;
  13. import org.apache.commons.lang.StringUtils;
  14. import org.apache.http.entity.ContentType;
  15. import org.apache.http.message.BasicHeader;
  16. import org.springframework.stereotype.Service;
  17. import org.springframework.web.bind.annotation.RequestBody;
  18. import javax.annotation.Resource;
  19. import javax.servlet.http.HttpServletRequest;
  20. import javax.servlet.http.HttpServletResponse;
  21. import java.math.BigDecimal;
  22. import java.sql.Array;
  23. import java.util.ArrayList;
  24. import java.util.HashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. /**
  28. * <p>
  29. * 服务实现类
  30. * </p>
  31. *
  32. * @author wuhs
  33. * @since 2022-07-12
  34. */
  35. @Service
  36. @Slf4j
  37. public class AirwallexServiceImpl implements AirwallexService {
  38. @Resource
  39. TOrderAbroadService tOrderAbroadService;
  40. @Override
  41. public String getAccessToken() {
  42. // HttpResponse<String> response = Unirest.post("https://api-demo.airwallex.com/api/v1/authentication/login")
  43. // .header("Content-Type", "application/json")
  44. // .header("x-client-id", "W_ORsgAFTiuA9k2KuqZt8A")
  45. // .header("x-api-key", "8ac97c856c6d6cae7eb8fd05511f7a165be798d032381cb8026de7b4aa9aaee2e6312a8888a3474d783a40913ab6b55d")
  46. // .body("{
  47. // }").asString();
  48. String url = AirwallexConstant.url+"/api/v1/authentication/login";
  49. List<BasicHeader> headers = new ArrayList<>();
  50. BasicHeader header1 = new BasicHeader("x-client-id",AirwallexConstant.clientid);
  51. BasicHeader header2 = new BasicHeader("x-api-key",AirwallexConstant.apiKey);
  52. headers.add(header1);
  53. headers.add(header2);
  54. Map<String,String> map = new HashMap<>();
  55. String data = JSON.toJSONString(map);
  56. String response=null;
  57. try {
  58. response = HttpClientSslUtils.doPost(url, data,ContentType.APPLICATION_JSON,headers);
  59. JSONObject resultJson = JSONObject.parseObject(response);
  60. String token = resultJson.getString("token");
  61. if(StringUtils.isNotEmpty(token)){
  62. System.out.println("token="+token);
  63. return token;
  64. }
  65. } catch (Exception e) {
  66. e.printStackTrace();
  67. }
  68. return response;
  69. }
  70. @Override
  71. public String caPaymentIntent(BigDecimal amount, String currency, String requestId, String merchantOrderId) {
  72. // 调用付款意向 api,返回payment intent id和client secret
  73. // 请求头
  74. String url = AirwallexConstant.url+"/api/v1/pa/payment_intents/create";
  75. List<BasicHeader> headers = new ArrayList<>();
  76. BasicHeader header1 = new BasicHeader("Authorization",AirwallexConstant.BEARER + this.getAccessToken());
  77. headers.add(header1);
  78. // body参数
  79. Map<String, Object> bodyMap = new HashMap<>();
  80. bodyMap.put("amount", amount);
  81. bodyMap.put("currency", currency);
  82. bodyMap.put("request_id", requestId);
  83. bodyMap.put("merchant_order_id", merchantOrderId);
  84. String data = JSON.toJSONString(bodyMap);
  85. log.info("请求body参数:" +data);
  86. Map<String, Object> responseMap = new HashMap<>();
  87. String resp = null;
  88. try {
  89. // 发送请求,获取响应结果
  90. resp = HttpClientSslUtils.doPost(url, data, ContentType.APPLICATION_JSON, headers);
  91. System.out.println(resp);
  92. responseMap = JSON.parseObject(resp);
  93. // return resp;
  94. } catch (Exception e) {
  95. e.printStackTrace();
  96. }
  97. String paymentIntentId = (String) responseMap.get("id");
  98. String clientSecret = (String) responseMap.get("client_secret");
  99. String env = AirwallexConstant.ENV;
  100. String mode = AirwallexConstant.MODE;
  101. // String locale = "it";
  102. String qrUrl = AirwallexConstant.QR_URL;
  103. return qrUrl + "?intent_id=" + paymentIntentId + "&client_secret=" + clientSecret + "&currency=" + currency;
  104. }
  105. @Override
  106. public String createARefund(String requestId) {
  107. // 请求头
  108. String url = AirwallexConstant.url + "/api/v1/pa/refunds/create";
  109. List<BasicHeader> headers = new ArrayList<>();
  110. BasicHeader header1 = new BasicHeader("Authorization", AirwallexConstant.BEARER + this.getAccessToken());
  111. headers.add(header1);
  112. // body参数
  113. Map<String, Object> bodyMap = new HashMap<>();
  114. bodyMap.put("request_id", requestId);
  115. // bodyMap.put("amount", amount); // 退款金额
  116. // bodyMap.put("reason", reason); // 退款原因
  117. String data = JSON.toJSONString(bodyMap);
  118. String resp = null;
  119. try {
  120. HttpClientSslUtils.doPost(url, data, ContentType.APPLICATION_JSON, headers);
  121. } catch (Exception e) {
  122. throw new RuntimeException(e);
  123. }
  124. return resp;
  125. }
  126. // @Override
  127. // public String refundWebhooks(HttpServletRequest request, HttpServletResponse response) {
  128. // return null;
  129. // }
  130. /**
  131. * 获取某笔支付订单的信息
  132. * @param intId
  133. * @return
  134. */
  135. @Override
  136. public String retrieveAPaymentIntent(Long intId) {
  137. String url = AirwallexConstant.url + "/api/v1/pa/payment_intents/{id}";
  138. // 请求头
  139. List<BasicHeader> headers = new ArrayList<>();
  140. new BasicHeader("Authorization", AirwallexConstant.BEARER + this.getAccessToken());
  141. // 请求体
  142. // Map<String, Object> bodyMap = new HashMap<>();
  143. // String data = JSON.toJSONString(bodyMap);
  144. String resp = null;
  145. try {
  146. resp = HttpClientSslUtils.doGet(url, ContentType.APPLICATION_JSON, headers);
  147. } catch (Exception e) {
  148. throw new RuntimeException(e);
  149. }
  150. return resp;
  151. }
  152. @Override
  153. public R getFormSchema() {
  154. // POST /api/v1/beneficiary_form_schemas/generate 根据国家/付款方式查询表单字段,返回前端所需表单信息(对接付款国较多)
  155. // https://www.airwallex.com/docs/api#/Payouts/Beneficiaries/_api_v1_beneficiary_form_schemas_generate/post
  156. // HttpResponse<String> response = Unirest.post("https://api-demo.airwallex.com/api/v1/beneficiary_form_schemas/generate")
  157. // .header("Content-Type", "application/json")
  158. // .header("Authorization", "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0b20iLCJyb2xlcyI6WyJ1c2VyIl0sImlhdCI6MTQ4ODQxNTI1NywiZXhwIjoxNDg4NDE1MjY3fQ.UHqau03y5kEk5lFbTp7J4a-U6LXsfxIVNEsux85hj-Q")
  159. // .header("headers", "[object Object]")
  160. // .body("{
  161. // \"account_currency\": \"USD\",
  162. // \"bank_country_code\": \"US\",
  163. // \"entity_type\": \"PERSONAL\",
  164. // \"local_clearing_system\": \"ACH\",
  165. // \"payment_method\": \"LOCAL\"
  166. // }")
  167. // .asString();
  168. return null;
  169. }
  170. @Override
  171. public R getAPISchema() {
  172. // POST /api/v1/beneficiary_api_schemas/generate 动态获取指定国家、支付方式,返回创建收款人请求所需字段
  173. // https://www.airwallex.com/docs/api#/Payouts/Beneficiaries/_api_v1_beneficiary_api_schemas_generate/post
  174. return null;
  175. }
  176. @Override
  177. public String createBeneficiary (Map<String, Object> requestBody) {
  178. // POST /api/v1/beneficiaries/create 创建收款人,返回收款人ID
  179. // https://www.airwallex.com/docs/api#/Payouts/Beneficiaries/_api_v1_beneficiaries_create/post
  180. Map beneficiary = new HashMap();
  181. Object address = null;
  182. ArrayList paymentMethods = new ArrayList<>();
  183. String beneficiaryId = "fsdfsd";
  184. return beneficiaryId;
  185. }
  186. @Override
  187. public String createPayout() {
  188. // POST /api/v1/payments/create 创建付款交易,返回创建结果paymentID
  189. // https://www.airwallex.com/docs/api#/Payouts/Payments/_api_v1_payments_create/post
  190. String paymentId = "111";
  191. return paymentId;
  192. }
  193. @Override
  194. public R getPaymentById(String paymentId) {
  195. // GET /api/v1/payments/{payment_id} 查询支付结果,返回支付结果状态
  196. // https://www.airwallex.com/docs/api#/Payouts/Payments/_api_v1_payments__payment_id_/get
  197. return null;
  198. }
  199. // Cancel、Retry
  200. }