package com.example.test.www.demo.controller;
import com.example.test.www.demo.comm.ErrorCodeEnum;
import com.example.test.www.demo.comm.MD5;
import com.example.test.www.demo.comm.Result;
import com.example.test.www.demo.model.PayermaxModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;
@Slf4j
@RestController
public class Payermax {
private String key = "aaaaa";
private String pay_url_dev = "https://pay-gate-uat.payermax.com/aggregate-pay-gate/api/gateway";
RestTemplate restTemplate;
@ResponseBody
@RequestMapping(value = "/pay/payermax", method = RequestMethod.POST)
public Result<Object> toPay(@RequestBody PayermaxModel payer) throws Exception {
TreeMap<String, String> tree = new TreeMap<>();
tree.put("bizType", payer.getBizType());
tree.put("merchantId", payer.getMerchantId());
tree.put("version", payer.getVersion());
tree.put("orderId", payer.getOrderId());
String _sign = PayermaxModel.setMD5Sign(tree);
log.info("MD5加密后的字符串为:" + _sign);
payer.setSign(_sign);
String co = payer.getCountryCode();
Map<String, String> _pay_detail = new HashMap<>();
switch (co){
case "IN":
_pay_detail.put("paymentType", "000");
case "PH":
_pay_detail.put("paymentType", "EWALLET");
}
_pay_detail.put("email", payer.getPaymentDetail().get("email"));
log.info(String.valueOf(_pay_detail));
payer.setPaymentDetail(_pay_detail);
log.info(String.valueOf(payer));
try {
HashMap r = sendPostRequest(this.pay_url_dev, payer);
return Result.successResult(r);
} catch (Exception e)
{
log.error(e.toString());
return Result.failResult(ErrorCodeEnum.BAD_REQUEST, e.toString());
}
}
public static HashMap sendPostRequest(String url, PayermaxModel params)
{
RestTemplate client = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
HttpMethod method = HttpMethod.POST;
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<PayermaxModel> requestEntity = new HttpEntity<>(params, headers);
ResponseEntity<HashMap> response = client.exchange(url, method, requestEntity, HashMap.class);
log.info("响应:" + String.valueOf(response));
return response.getBody();
}
private String setSign(TreeMap<String, String> tree) throws Exception {
String A = "bizType=" + tree.get("bizType")
+ "&merchantId=" + tree.get("merchantId")
+ "&orderId=" + tree.get("orderId") + "&version=2.1";
String signTemp = A + "&key=" + this.key;
String _md5 = MD5.md5(signTemp,"").toUpperCase();
return _md5;
}
}
|