| | |
| | | String reqUrl = "https://api.watchglb.com/pay/web"; |
| | | // 支付2 |
| | | String uipReqRul = "https://v2.fastechservicepro.com/payin/unifiedorder"; |
| | | // 支付3 |
| | | String threeUrl = "https://www.copays.net/open-api/create-pay-order"; |
| | | |
| | | User user = userService.getCurrentRefreshUser(request); |
| | | if(ObjectUtils.isEmpty(user)){ |
| | |
| | | return ServerResponse.createByErrorCodeMsg(401,googleTranslateUtil.translate("请登录",lang )); |
| | | } |
| | | |
| | | if(type == 2){//支付1 |
| | | if(type == 0){//支付1 |
| | | return getPaymentOne(tradeAmount, reqUrl,user,request); |
| | | }else if(type == 1){//支付2 |
| | | }else if(type == 2){//支付2 |
| | | return getPaymentTwo(tradeAmount, uipReqRul, user,request); |
| | | }else if(type == 0){ |
| | | } else if(type == 3){//支付2 |
| | | return getPaymentThree(tradeAmount, threeUrl, user,request); |
| | | }else if(type == 1){ |
| | | return getPaymentZero(tradeAmount, uipReqRul, user,request); |
| | | } |
| | | return ServerResponse.createByErrorMsg("获取支付方式失败",request); |
| | |
| | | |
| | | private ServerResponse getPaymentZero(String tradeAmount, String uipReqRul, User user, HttpServletRequest request) throws Exception { |
| | | //int |
| | | String url = "https://gateway.king-pays.com/gateway/payment/init";//正式地址 |
| | | String url = "https://gateway.kings-pays.com/gateway/payment/init";//正式地址 |
| | | String merchantKey = "qqaC1DH/LeR9iPvm";//商户key 需替换 |
| | | String aesKey = "ge6vK40fHNZPFJ4p";//商户aesKey 需替换 |
| | | String aesIv = "6gJoHTEE1i2O3ovE";//商户aesIv 需替换 |
| | |
| | | cipher.init(Cipher.ENCRYPT_MODE, k, ivParameterSpec); |
| | | return cipher.doFinal(data); |
| | | } |
| | | |
| | | private ServerResponse getPaymentThree(String tradeAmount, String uipReqRul, User user, HttpServletRequest request) throws Exception { |
| | | |
| | | // 配置参数(建议放到配置文件中) |
| | | String appId = "your_app_id_here"; // 从商户后台获取 |
| | | String key = "your_key_here"; // 商户密钥 |
| | | String payChannel = "INDIA_NATIVE"; // 支付通道,根据实际情况选择 |
| | | |
| | | // 生成商户订单号 |
| | | String merchantOrderId = generatePayOrderId(); |
| | | |
| | | // 处理金额,保留两位小数 |
| | | BigDecimal amount = new BigDecimal(tradeAmount); |
| | | amount = amount.setScale(2, BigDecimal.ROUND_HALF_UP); |
| | | |
| | | // 构建请求参数 |
| | | Map<String, String> params = new HashMap<>(); |
| | | params.put("app_id", appId); |
| | | params.put("merchant_order_id", merchantOrderId); |
| | | params.put("amount", amount.toString()); |
| | | params.put("pay_channel", payChannel); |
| | | params.put("notify_url", "https://api.durocaspitall.com/user/rechargeCallbackTwo.do"); // 异步通知地址 |
| | | params.put("page_return_url", "https://yourdomain.com/pay/return"); // 前端返回地址 |
| | | |
| | | // 可选参数 |
| | | if (user != null) { |
| | | String customerName = UserNameUtil.formatCustomerName(user.getNickName()); |
| | | if (StringUtils.isNotEmpty(customerName)) { |
| | | params.put("customer_name", customerName); |
| | | } |
| | | if (StringUtils.isNotEmpty(user.getPhone())) { |
| | | params.put("customer_phone", user.getPhone()); |
| | | } |
| | | params.put("customer_email", "test@gmail.com"); // 根据实际情况获取用户邮箱 |
| | | } |
| | | |
| | | // 生成签名(注意:签名生成时不需要包含sign参数本身) |
| | | String sign = PaymentSignUtil.generateSign(params, key); |
| | | params.put("sign", sign); |
| | | |
| | | log.info("支付请求参数:{}", params); |
| | | log.info("生成的签名:{}", sign); |
| | | |
| | | // 发送请求 |
| | | String result = HttpClientUtil.doPost(uipReqRul, params, "utf-8"); |
| | | log.info("支付返回参数:{}", result); |
| | | |
| | | // 解析响应 |
| | | ObjectMapper objectMapper = new ObjectMapper(); |
| | | PaymentApiResponse response = objectMapper.readValue(result, PaymentApiResponse.class); |
| | | |
| | | // 检查响应 |
| | | if (!Integer.valueOf(200).equals(response.getCode())) { |
| | | log.error("支付下单失败,返回码:{},消息:{},请求参数:{}", |
| | | response.getCode(), response.getMessage(), params); |
| | | return ServerResponse.createByErrorMsg("获取支付链接失败:" + response.getMessage(), request); |
| | | } |
| | | |
| | | // 可选:验证返回签名 |
| | | PaymentApiResponse.PaymentData responseData = response.getData(); |
| | | Map<String, String> responseSignParams = new HashMap<>(); |
| | | responseSignParams.put("merchant_id", responseData.getMerchantId()); |
| | | responseSignParams.put("app_id", responseData.getAppId()); |
| | | responseSignParams.put("system_order_id", responseData.getSystemOrderId()); |
| | | responseSignParams.put("merchant_order_id", responseData.getMerchantOrderId()); |
| | | responseSignParams.put("pay_url", responseData.getPayUrl()); |
| | | |
| | | boolean signValid = PaymentSignUtil.verifySign(responseSignParams, key, responseData.getSign()); |
| | | if (!signValid) { |
| | | log.warn("返回签名验证失败,可能存在安全风险"); |
| | | } |
| | | |
| | | // 保存支付记录 |
| | | PaymentRecharge paymentRecharge = new PaymentRecharge(); |
| | | paymentRecharge.setUserId(user.getId()); |
| | | paymentRecharge.setOrderNo(merchantOrderId); // 商户订单号 |
| | | paymentRecharge.setMchOrderNo(responseData.getSystemOrderId()); // 平台订单号 |
| | | paymentRecharge.setAmount(amount); |
| | | paymentRecharge.setStatus(1); // 1:已获取 |
| | | paymentRecharge.setPaymentTime(new Date()); |
| | | paymentRecharge.setCreatedAt(new Date()); |
| | | paymentRecharge.setNotifyUrl(params.get("notify_url")); |
| | | paymentRecharge.setPayInfo(result); |
| | | paymentRechargeService.save(paymentRecharge); |
| | | |
| | | // 保存用户充值记录 |
| | | UserRecharge userRecharge = new UserRecharge(); |
| | | userRecharge.setUserId(user.getId()); |
| | | userRecharge.setNickName(user.getRealName()); |
| | | userRecharge.setAgentId(user.getAgentId()); |
| | | userRecharge.setOrderSn(merchantOrderId); |
| | | userRecharge.setPayChannel(payChannel); // 使用实际的支付通道 |
| | | userRecharge.setPayAmt(amount); |
| | | userRecharge.setAddTime(new Date()); |
| | | userRecharge.setPayTime(new Date()); |
| | | userRecharge.setOrderStatus(0); // 审核中 |
| | | userRecharge.setPayId(3); // 支付通道ID,根据实际情况设置 |
| | | userRechargeMapper.insert(userRecharge); |
| | | |
| | | // 返回支付URL |
| | | return ServerResponse.createBySuccess(responseData.getPayUrl()); |
| | | } |
| | | |
| | | //支付2 |
| | | private ServerResponse getPaymentTwo(String tradeAmount, String uipReqRul, User user, HttpServletRequest request) throws UnsupportedEncodingException, NoSuchAlgorithmException, JsonProcessingException { |
| | | // 示例参数 |