首页>代码>ssm高仿beatport.com在线音乐网网站>/MusicWebProject/src/main/java/com/music/action/applicationPayment/alipayPay/AlipayService.java
package com.music.action.applicationPayment.alipayPay; import com.alipay.api.AlipayApiException; import com.alipay.api.AlipayClient; import com.alipay.api.DefaultAlipayClient; import com.alipay.api.request.*; import java.math.BigDecimal; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Service; /** * Created by Danny on 2017/12/26. */ @Service public class AlipayService implements IAlipayService { /** * 请求支付 * @param orderId 订单编号 * @param amount 金额 * @param orderName 订单名称 * @param description 描述 * @param returnUrl 返回URL * @return */ @Override public String pay(String orderId, String amount, String orderName, String description,String returnUrl,String User_ID,String PriceID) { AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type); //设置请求参数 AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest(); alipayRequest.setReturnUrl(returnUrl); alipayRequest.setNotifyUrl(AlipayConfig.notify_url); //商户订单号,商户网站订单系统中唯一订单号,必填 String out_trade_no = orderId; //付款金额,必填 String total_amount = amount; //订单名称,必填 String subject = orderName; //商品描述,可空 String body = description; alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\"," + "\"total_amount\":\"" + total_amount + "\"," + "\"subject\":\"" + subject + "\"," + "\"body\":\"" + body + "\"," + "\"timeout_express\":\"10m\"," + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}"); //若想给BizContent增加其他可选请求参数,以增加自定义超时时间参数timeout_express来举例说明 //alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\"," // + "\"total_amount\":\""+ total_amount +"\"," // + "\"subject\":\""+ subject +"\"," // + "\"body\":\""+ body +"\"," // + "\"timeout_express\":\"10m\"," // + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}"); //请求参数可查阅【电脑网站支付的API文档-alipay.trade.page.pay-请求参数】章节 //数据库添加订单信息 com.music.entity.Membership_Order Order = new com.music.entity.Membership_Order(); Order.setOrder_Number(orderId);//商户订单编号 Order.setQuantity_of_goods(1);//货物数量 Order.setOrder_Name(orderName);//订单名称 Order.setCommodity_Description(description);//商品详情 Order.setAlipay_Order_Id("");//支付宝订单ID Order.setOrder_Amount(Integer.valueOf(amount));//订单金额 Order.setOrder_Status("等待用户支付");//订单状态 Order.setUser_ID(new BigDecimal(User_ID));//用户ID Order.setPriceID(new BigDecimal(PriceID));//资费ID List<com.music.entity.Membership_Order> list = new ArrayList<>(); list.add(Order); if(new com.music.sqlConnect.dml.add.Membership_OrderAdd().Add_Order(list)){ //正确添加订单信息后请求 //请求 String result = null; try { result = alipayClient.pageExecute(alipayRequest).getBody(); } catch (AlipayApiException e) { } return result; }else{ //无法添加订单信息 return "Failure to add order information"; } } @Override public String query(String orderId, String alipayOrderId) { //获得初始化的AlipayClient AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type); //设置请求参数 AlipayTradeQueryRequest alipayRequest = new AlipayTradeQueryRequest(); //商户订单号,商户网站订单系统中唯一订单号 String out_trade_no = orderId; //支付宝交易号 String trade_no = alipayOrderId; //请二选一设置 alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\"," + "\"trade_no\":\"" + trade_no + "\"}"); //请求 String result = null; try { result = alipayClient.execute(alipayRequest).getBody(); } catch (AlipayApiException e) { } //输出 return result; } @Override public String refund(String orderId, String alipayOrderId, String amount, String reason, String refundReqNum) { AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type); //设置请求参数 AlipayTradeRefundRequest alipayRequest = new AlipayTradeRefundRequest(); //商户订单号,商户网站订单系统中唯一订单号 String out_trade_no = orderId; //支付宝交易号 String trade_no = alipayOrderId; //请二选一设置 //需要退款的金额,该金额不能大于订单金额,必填 String refund_amount = amount; //退款的原因说明 String refund_reason = reason; //标识一次退款请求,同一笔交易多次退款需要保证唯一,如需部分退款,则此参数必传 String out_request_no = refundReqNum; alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\"," + "\"trade_no\":\"" + trade_no + "\"," + "\"refund_amount\":\"" + refund_amount + "\"," + "\"refund_reason\":\"" + refund_reason + "\"," + "\"out_request_no\":\"" + out_request_no + "\"}"); //请求 String result = null; try { result = alipayClient.execute(alipayRequest).getBody(); } catch (AlipayApiException e) { } return result; } @Override public String refundQuery(String orderId, String alipayOrderId, String refundReqNum) { AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type); //设置请求参数 AlipayTradeFastpayRefundQueryRequest alipayRequest = new AlipayTradeFastpayRefundQueryRequest(); //商户订单号,商户网站订单系统中唯一订单号 String out_trade_no = orderId; //支付宝交易号 String trade_no = alipayOrderId; //请二选一设置 //请求退款接口时,传入的退款请求号,如果在退款请求时未传入,则该值为创建交易时的外部交易号,必填 String out_request_no = refundReqNum; alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\"," + "\"trade_no\":\"" + trade_no + "\"," + "\"out_request_no\":\"" + out_request_no + "\"}"); String result = null; //请求 try { result = alipayClient.execute(alipayRequest).getBody(); } catch (AlipayApiException e) { } return result; } @Override public String close(String orderId, String alipayOrderId) { AlipayClient alipayClient = new DefaultAlipayClient(AlipayConfig.gatewayUrl, AlipayConfig.app_id, AlipayConfig.merchant_private_key, "json", AlipayConfig.charset, AlipayConfig.alipay_public_key, AlipayConfig.sign_type); //设置请求参数 AlipayTradeCloseRequest alipayRequest = new AlipayTradeCloseRequest(); //商户订单号,商户网站订单系统中唯一订单号 String out_trade_no = orderId; //支付宝交易号 String trade_no = alipayOrderId; //请二选一设置 alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\"," + "\"trade_no\":\"" + trade_no + "\"}"); String result = null; //请求 try { result = alipayClient.execute(alipayRequest).getBody(); } catch (AlipayApiException e) { } return result; } }
最近下载更多
微信网友_6602457430806528 LV6
2023年9月21日
13680889751 LV1
2023年5月13日
tanziyi1993 LV2
2022年2月17日
项目管理 LV6
2021年12月12日
wanglinddad LV55
2021年12月9日
cc123456RR LV5
2021年10月27日
加油干阳神 LV9
2021年7月15日
15871814851 LV1
2021年7月5日
橘子猫咪汽水 LV1
2021年5月29日
絮落无痕 LV13
2021年5月18日