日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

支付宝接口的调用

發布時間:2024/10/6 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 支付宝接口的调用 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • 支付流程圖

  • 對接支付寶的準備工作

  • 一、申請條件
    1.企業或個體工商戶可申請;
    2. 提供真實有效的營業執照,且支付寶賬戶名稱需與營業執照主體一致;
    3. 網站能正常訪問且頁面信息有完整商品內容;
    4. 網站必須通過ICP備案,個體戶備案需與賬戶主體一致。
    (團購類網站不支持個體工商戶簽約)

  • alipay.properties
  • #支付寶的接口地址 alipay_url=https://openapi.alipay.com/gateway.do #企業與支付寶合作伙伴Id app_id=2018020102122556 #企業自己的私鑰 app_private_key=MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCdQeknhM2rhiGAH6V0ljxn3rAWIdzduTEQuteTfwjnZtvMhQPuuN1b/88D5yMuaZhZNFeUdWb+SmtP9DAzAWWgnT13T0YhJcxP6txm7JBRrjadCRt+LOFxPiPQk5t9fH7yXjw9i4uMDsNJeTncrVZ/AZYrk0ESC9anJR8XeuBc3HE8T4fqlKKl35jlumIWrPbPNQhKGXaGcOnpiaXO9qYYUSP/tnrjNYXHOso0yBs4YTl+LLX2TJ12p3n/oX6HnL4zQgtN5k4QasHP7CIig1ngcVQGfWsMm4djI9KXNXvGLQPfMQEmyb71mM5OCdl1MtAc6OaIAymhSv2hOLNIuyodAgMBAAECggEAe05/P5mGm4QlKI2n8u8KlneqovAS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uYyXBdcpPuZ/ys2C25pNrkACuhIKNgnMc0floJoYEfJzetw/3cIimWu4NJzVQOaojaGA58oo2+fub43Xn25Jq4rvSVe3oLdb5xWkw5Q= #支付寶公鑰 alipay_public_key=MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAhkZi6W0wn/prX+NIIF9ATb5Z8ReKK4hFYtBrweDfGHD1mNW7YIZY4G5hE7S2Sry8eFXlFgSlBWlJ4fVnDaK9MkVThpwE2H65ooVlK/wLuyPqovIVpMt/utva5Ayuzv7eQOWK45FdLDNDlK8QLoBko6SS+YbnWnf7a+mrf4NAS4UFClpfe8Byqe8XIraO2Cg4Ko5Y5schX39rOAH8GlLdgqQRYVQ2dCnkIQ+L+I4Cy9Mvw3rIkTwt3MBU+AqREXY4r5Bn6cmmX/9MAJbFqrofGiUAqG+qbjTcZAzgNPfuiD0zXgt/YYjMQMzck75BOmwnYOam2ajODUSQn8Xybsa7wQIDAQAB# 同步回調地址 重定向地址本地瀏覽器 return_payment_url=http://payment.gmall.com:8087/alipay/callback/return# 異步通知地址 公網接口(webService) notify_payment_url=http://60.205.215.91/alipay/callback/notifyreturn_order_url=http://order.gmall.com:8086/orderList
  • 類AlipayConfig整合到項目
  • @Configuration @PropertySource("classpath:alipay.properties") public class AlipayConfig {@Value("${alipay_url}")private String alipay_url;@Value("${app_private_key}")private String app_private_key;@Value("${app_id}")private String app_id;public final static String format="json";public final static String charset="utf-8";public final static String sign_type="RSA2";public static String return_payment_url;public static String notify_payment_url;public static String return_order_url;public static String alipay_public_key;@Value("${alipay_public_key}")public void setAlipay_public_key(String alipay_public_key) {AlipayConfig.alipay_public_key = alipay_public_key;}@Value("${return_payment_url}")public void setReturn_url(String return_payment_url) {AlipayConfig.return_payment_url = return_payment_url;}@Value("${notify_payment_url}")public void setNotify_url(String notify_payment_url) {AlipayConfig.notify_payment_url = notify_payment_url;}@Value("${return_order_url}")public void setReturn_order_url(String return_order_url) {AlipayConfig.return_order_url = return_order_url;}@Beanpublic AlipayClient alipayClient(){AlipayClient alipayClient=new DefaultAlipayClient(alipay_url,app_id,app_private_key,format,charset, alipay_public_key,sign_type );return alipayClient;}
  • PaymentMapper.java
  • public interface PaymentMapper extends Mapper<PaymentInfo>{ }
  • PaymentServiceImpl.java
  • @Service public class PaymentServiceImpl implements PaymentService{@AutowiredPaymentMapper paymentMapper;@AutowiredActiveMQUtil activeMQUtil;@AutowiredAlipayClient alipayClient;@Overridepublic void savePaymentInfo(PaymentInfo paymentInfo) {paymentMapper.insert(paymentInfo);}@Overridepublic void updatePayment(PaymentInfo paymentInfo) {//冪等性檢查PaymentInfo paymentInfoParam = new PaymentInfo();paymentInfoParam.setOrderSn(paymentInfo.getOrderSn());PaymentInfo paymentInfoResult=paymentMapper.selectOne(paymentInfoParam);if(StringUtils.isNotBlank(paymentInfoResult.getPaymentStatus())&&paymentInfoResult.getPaymentStatus().equals("已支付")){return;}else{String orderSn = paymentInfo.getOrderSn();Example example=new Example(PaymentInfo.class);example.createCriteria().andEqualTo("orderSn",orderSn);ConnectionFactory connectionFactory = activeMQUtil.getConnectionFactory();Session session=null;Connection connection=null;try {connection = connectionFactory.createConnection();session = connection.createSession(true, Session.SESSION_TRANSACTED);} catch (JMSException e) {e.printStackTrace();}try{paymentMapper.updateByExampleSelective(paymentInfo,example);//支付成功后,引起系統服務===》訂單服務===》庫存服務===》物流//調用mq發送支付成功的消息Queue payment_success_queue = session.createQueue("PAYMENT_SUCCESS_QUEUE");MessageProducer producer = session.createProducer(payment_success_queue);//字符串文本//TextMessage textMessage=new ActiveMQTextMessage();//hash結構MapMessage mapMessage=new ActiveMQMapMessage();mapMessage.setString("out_trade_no",paymentInfo.getOrderSn());producer.send(mapMessage);session.commit();}catch (Exception e){//消息回滾try {session.rollback();} catch (JMSException e1) {e1.printStackTrace();}}finally {try {connection.close();} catch (JMSException e) {e.printStackTrace();}}}}@Overridepublic void sendDelayPaymentResultCheckQueue(String outTrade,int count) {ConnectionFactory connectionFactory = activeMQUtil.getConnectionFactory();Session session=null;Connection connection=null;try {connection = connectionFactory.createConnection();session = connection.createSession(true, Session.SESSION_TRANSACTED);} catch (JMSException e) {e.printStackTrace();}try{Queue payment_success_queue = session.createQueue("PAYMENT_CHECK_QUEUE");MessageProducer producer = session.createProducer(payment_success_queue);//字符串文本//TextMessage textMessage=new ActiveMQTextMessage();//hash結構MapMessage mapMessage=new ActiveMQMapMessage();mapMessage.setString("out_trade_no",outTrade);mapMessage.setInt("count",count);//為消息加入延遲時間mapMessage.setLongProperty(ScheduledMessage.AMQ_SCHEDULED_DELAY,1000*60);producer.send(mapMessage);session.commit();}catch (Exception e){//消息回滾try {session.rollback();} catch (JMSException e1) {e1.printStackTrace();}}finally {try {connection.close();} catch (JMSException e) {e.printStackTrace();}}}@Overridepublic Map<String, Object> checkAlipayPayment(String out_trade_no) {AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();HashMap<String, Object> resultMap = new HashMap<>();resultMap.put("out_trade_no",out_trade_no);request.setBizContent(JSON.toJSONString(resultMap));AlipayTradeQueryResponse response = null;try {response = alipayClient.execute(request);} catch (AlipayApiException e) {e.printStackTrace();}if(response.isSuccess()){System.out.println("交易創建已創建,調用成功");resultMap.put("out_trade_no",response.getOutTradeNo());resultMap.put("trade_no",response.getTradeNo());resultMap.put("trade_status",response.getTradeStatus());resultMap.put("call_back_content",response.getMsg());} else {System.out.println("有可能交易創建未創建,調用失敗");}return resultMap;} }

    8.PaymentController.java

    @Controller public class PaymentController {@AutowiredAlipayClient alipayClient;@AutowiredPaymentService paymentService;@ReferenceOrderService orderService;@RequestMapping("alipay/callback/return")@LoginRequired(loginSuccess = true)public String aliPaycallBackReturn(HttpServletRequest request, ModelMap modelMap){//更新用戶的支付狀態String sign = request.getParameter("sign");String trade_no=request.getParameter("trade_no");String out_trade_no = request.getParameter("out_trade_no");String trade_status = request.getParameter("trade_status");String total_amount = request.getParameter("total_amount");String subject = request.getParameter("subject");String call_back_content = request.getQueryString();//通過支付寶的paymentMap進行簽名驗證,2.0版本中將接口paymentMap參數去掉了,導致同步請求無法驗簽if(StringUtils.isNotBlank(sign)){//驗簽成功//進行支付更新的冪等性檢查操作PaymentInfo paymentInfo = new PaymentInfo();paymentInfo.setOrderSn(out_trade_no);//支付寶的交易憑證號paymentInfo.setPaymentStatus("已支付");paymentInfo.setAlipayTradeNo(trade_no);paymentInfo.setCallbackTime(new Date());//回調請求字符串paymentInfo.setCallbackContent(call_back_content);//更新用戶的支付狀態paymentService.updatePayment(paymentInfo);}//支付成功后,引起系統服務===》訂單服務===》庫存服務===》物流//調用mq發送支付成功的消息return "finish";}@RequestMapping("wx/submit")@LoginRequired(loginSuccess = true)public String wxPay(String outTrade, BigDecimal totalAmount, HttpServletRequest request, ModelMap modelMap){return "";}@RequestMapping("alipay/submit")@LoginRequired(loginSuccess = true)@ResponseBodypublic String aliPay(String outTrade, BigDecimal totalAmount, HttpServletRequest request, ModelMap modelMap){//獲得支付寶客戶端,不是一個鏈接而是針對http的表單請求String form="";//創建API對應的requestAlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();//同步回調地址alipayRequest.setReturnUrl(AlipayConfig.return_payment_url);alipayRequest.setNotifyUrl(AlipayConfig.notify_payment_url);Map<String,Object> map=new HashMap<>();map.put("out_trade_no",outTrade);map.put("product_code","FAST_INSTANT_TRADE_PAY");map.put("total_amount",0.01);map.put("subject","JD Apple 10 phone");String param = JSON.toJSONString(map);alipayRequest.setBizContent(param);try {form = alipayClient.pageExecute(alipayRequest).getBody();} catch (AlipayApiException e) {e.printStackTrace();}//生成用戶的支付信息OmsOrder omsOrder=orderService.getOrderByOutTradeNo(outTrade);PaymentInfo paymentInfo = new PaymentInfo();paymentInfo.setCreateTime(new Date());paymentInfo.setOrderId(omsOrder.getId());paymentInfo.setOrderSn(outTrade);paymentInfo.setPaymentStatus("未付款");paymentInfo.setSubject("JD商品");paymentInfo.setTotalAmount(totalAmount);paymentService.savePaymentInfo(paymentInfo );//向消息中間件發送一個檢查支付狀態(支付服務的延遲消息隊列)paymentService.sendDelayPaymentResultCheckQueue(outTrade,5);//提交請求到支付寶return form;}@RequestMapping("index")@LoginRequired(loginSuccess = true)public String index(String outTrade, BigDecimal totalAmount, HttpServletRequest request, ModelMap modelMap){String memberId = (String)request.getAttribute("memberId");String nickName = (String) request.getAttribute("nickName");modelMap.put("memberId",memberId);modelMap.put("outTrade",outTrade);modelMap.put("nickName",nickName);modelMap.put("totalAmount",totalAmount);return "index";} }

    總結

    以上是生活随笔為你收集整理的支付宝接口的调用的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。