package com.szwl.service.impl; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.szwl.constant.AirwallexConstant; import com.szwl.model.bean.PaymentIntent; import com.szwl.model.bean.PaymentIntentRequestBody; import com.szwl.model.bo.R; import com.szwl.model.entity.TOrderAbroad; import com.szwl.model.utils.HttpClientSslUtils; import com.szwl.service.AirwallexService; import com.szwl.service.TOrderAbroadService; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.apache.http.entity.ContentType; import org.apache.http.message.BasicHeader; import org.springframework.stereotype.Service; import org.springframework.web.bind.annotation.RequestBody; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.math.BigDecimal; import java.sql.Array; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** *

* 服务实现类 *

* * @author wuhs * @since 2022-07-12 */ @Service @Slf4j public class AirwallexServiceImpl implements AirwallexService { @Resource TOrderAbroadService tOrderAbroadService; @Override public String getAccessToken() { // HttpResponse response = Unirest.post("https://api-demo.airwallex.com/api/v1/authentication/login") // .header("Content-Type", "application/json") // .header("x-client-id", "W_ORsgAFTiuA9k2KuqZt8A") // .header("x-api-key", "8ac97c856c6d6cae7eb8fd05511f7a165be798d032381cb8026de7b4aa9aaee2e6312a8888a3474d783a40913ab6b55d") // .body("{ // }").asString(); String url = AirwallexConstant.url+"/api/v1/authentication/login"; List headers = new ArrayList<>(); BasicHeader header1 = new BasicHeader("x-client-id",AirwallexConstant.clientid); BasicHeader header2 = new BasicHeader("x-api-key",AirwallexConstant.apiKey); headers.add(header1); headers.add(header2); Map map = new HashMap<>(); String data = JSON.toJSONString(map); String response=null; try { response = HttpClientSslUtils.doPost(url, data,ContentType.APPLICATION_JSON,headers); JSONObject resultJson = JSONObject.parseObject(response); String token = resultJson.getString("token"); if(StringUtils.isNotEmpty(token)){ System.out.println("token="+token); return token; } } catch (Exception e) { e.printStackTrace(); } return response; } @Override public String caPaymentIntent(BigDecimal amount, String currency, String requestId, String merchantOrderId) { // 调用付款意向 api,返回payment intent id和client secret // 请求头 String url = AirwallexConstant.url+"/api/v1/pa/payment_intents/create"; List headers = new ArrayList<>(); BasicHeader header1 = new BasicHeader("Authorization",AirwallexConstant.BEARER + this.getAccessToken()); headers.add(header1); // body参数 Map bodyMap = new HashMap<>(); bodyMap.put("amount", amount); bodyMap.put("currency", currency); bodyMap.put("request_id", requestId); bodyMap.put("merchant_order_id", merchantOrderId); String data = JSON.toJSONString(bodyMap); log.info("请求body参数:" +data); Map responseMap = new HashMap<>(); String resp = null; try { // 发送请求,获取响应结果 resp = HttpClientSslUtils.doPost(url, data, ContentType.APPLICATION_JSON, headers); System.out.println(resp); responseMap = JSON.parseObject(resp); // return resp; } catch (Exception e) { e.printStackTrace(); } String paymentIntentId = (String) responseMap.get("id"); String clientSecret = (String) responseMap.get("client_secret"); String env = AirwallexConstant.ENV; String mode = AirwallexConstant.MODE; // String locale = "it"; String qrUrl = AirwallexConstant.QR_URL; return qrUrl + "?intent_id=" + paymentIntentId + "&client_secret=" + clientSecret + "¤cy=" + currency; } @Override public String createARefund(String requestId) { // 请求头 String url = AirwallexConstant.url + "/api/v1/pa/refunds/create"; List headers = new ArrayList<>(); BasicHeader header1 = new BasicHeader("Authorization", AirwallexConstant.BEARER + this.getAccessToken()); headers.add(header1); // body参数 Map bodyMap = new HashMap<>(); bodyMap.put("request_id", requestId); // bodyMap.put("amount", amount); // 退款金额 // bodyMap.put("reason", reason); // 退款原因 String data = JSON.toJSONString(bodyMap); String resp = null; try { HttpClientSslUtils.doPost(url, data, ContentType.APPLICATION_JSON, headers); } catch (Exception e) { throw new RuntimeException(e); } return resp; } // @Override // public String refundWebhooks(HttpServletRequest request, HttpServletResponse response) { // return null; // } /** * 获取某笔支付订单的信息 * @param intId * @return */ @Override public String retrieveAPaymentIntent(Long intId) { String url = AirwallexConstant.url + "/api/v1/pa/payment_intents/{id}"; // 请求头 List headers = new ArrayList<>(); new BasicHeader("Authorization", AirwallexConstant.BEARER + this.getAccessToken()); // 请求体 // Map bodyMap = new HashMap<>(); // String data = JSON.toJSONString(bodyMap); String resp = null; try { resp = HttpClientSslUtils.doGet(url, ContentType.APPLICATION_JSON, headers); } catch (Exception e) { throw new RuntimeException(e); } return resp; } @Override public R getFormSchema() { // POST /api/v1/beneficiary_form_schemas/generate 根据国家/付款方式查询表单字段,返回前端所需表单信息(对接付款国较多) // https://www.airwallex.com/docs/api#/Payouts/Beneficiaries/_api_v1_beneficiary_form_schemas_generate/post // HttpResponse response = Unirest.post("https://api-demo.airwallex.com/api/v1/beneficiary_form_schemas/generate") // .header("Content-Type", "application/json") // .header("Authorization", "Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ0b20iLCJyb2xlcyI6WyJ1c2VyIl0sImlhdCI6MTQ4ODQxNTI1NywiZXhwIjoxNDg4NDE1MjY3fQ.UHqau03y5kEk5lFbTp7J4a-U6LXsfxIVNEsux85hj-Q") // .header("headers", "[object Object]") // .body("{ // \"account_currency\": \"USD\", // \"bank_country_code\": \"US\", // \"entity_type\": \"PERSONAL\", // \"local_clearing_system\": \"ACH\", // \"payment_method\": \"LOCAL\" // }") // .asString(); return null; } @Override public R getAPISchema() { // POST /api/v1/beneficiary_api_schemas/generate 动态获取指定国家、支付方式,返回创建收款人请求所需字段 // https://www.airwallex.com/docs/api#/Payouts/Beneficiaries/_api_v1_beneficiary_api_schemas_generate/post return null; } @Override public String createBeneficiary (Map requestBody) { // POST /api/v1/beneficiaries/create 创建收款人,返回收款人ID // https://www.airwallex.com/docs/api#/Payouts/Beneficiaries/_api_v1_beneficiaries_create/post Map beneficiary = new HashMap(); Object address = null; ArrayList paymentMethods = new ArrayList<>(); String beneficiaryId = "fsdfsd"; return beneficiaryId; } @Override public String createPayout() { // POST /api/v1/payments/create 创建付款交易,返回创建结果paymentID // https://www.airwallex.com/docs/api#/Payouts/Payments/_api_v1_payments_create/post String paymentId = "111"; return paymentId; } @Override public R getPaymentById(String paymentId) { // GET /api/v1/payments/{payment_id} 查询支付结果,返回支付结果状态 // https://www.airwallex.com/docs/api#/Payouts/Payments/_api_v1_payments__payment_id_/get return null; } // Cancel、Retry }