·¢²¼Ê±¼ä:2022-11-21 02:30:22 ÎÄÕÂÀ´Ô´:»¥ÁªÍø
΢²© ΢ÐÅ QQ¿Õ¼ä

΢ÐÅËÑÒ»ËÑ£¬·½±ãÒԺ󿪷¢ÓÃŶ£¡¹Ù·½ÎĵµÖ§¸¶±¦ÊÖ»úÍøÕ¾Ö§¸¶

΢ÐÅËÑÒ»ËÑ£¬·½±ãÒԺ󿪷¢ÓÃŶ£¡¹Ù·½ÎĵµÖ§¸¶±¦ÊÖ»úÍøÕ¾Ö§¸¶

Ç°ÑÔ

΢ÐÅAPPÖ§¸¶¡¢Î¢ÐÅÊÚȨ¡¢Î¢ÐÅJSAPIÖ§¸¶¡¢Î¢ÐÅÍË¿î¡¢Ö§¸¶±¦APPÖ§¸¶¡¢Ö§¸¶±¦ÊÖ»úÍøÕ¾Ö§¸¶¡¢Ö§¸¶±¦ÍË¿î¡£×÷ÕßÒѾ­·ÅÔÚ¹«ÖںţºJAVA´óÔô´¬ÉÏÁË¡£Î¢ÐÅËÑË÷δÀ´·¢Õ¹£¡

¹Ù·½Îļþ

Ö§¸¶±¦ÊÖ»úÍøÕ¾Ö§¸¶²»Ïñ΢ÐÅÖ§¸¶ÄÇôËöË飨ÔÚÕâÀïÕæµÄҪͲÛÒ»ÏÂwxpay£¬ÅäÖÃÌ«¶àÖ§¸¶±¦Ê¹ÓÃ˵Ã÷Ê飬ÎĵµÒ²²»ºÃ¿´£¡£¡£©£¬Ö§¸¶±¦¹Ù·½ÎĵµÒѾ­Ð´µÄºÜÏêϸÁË£¬ËùÒÔÕâ¸öÎÄÕÂûÓÐÏêϸ˵Ã÷²ÎÊýµÄ½âÊÍ

¿ª·ÅÄÜÁ¦Îĵµ£º

/´ò¿ª/203/107084

ÊÖ»úÍøÕ¾Ö§¸¶½Ó¿Ú

/apis/api_1/alipay.trade.wap.pay

¿ª·¢×¼±¸

¿ª·¢ÕßʹÓÃÖ§¸¶±¦ÕË»§µÇ¼¿ª·Åƽ̨£¨ÐèҪʵÃûÈÏÖ¤µÄÖ§¸¶±¦ÕË»§£©£¬¸ù¾Ýʵ¼ÊÐèÒª´´½¨Ó¦Óã¨Èç¡°Ö§¸¶Ó¦Óá±£©¡£

±¸×¢£º´´½¨Ó¦ÓÃʱӦÓÃ״̬Ϊ¡°¿ª·¢ÖС±Ö§¸¶±¦Ê¹ÓÃ˵Ã÷Ê飬²»ÄÜÕýʽÔÚÏßµ÷Óýӿڡ£

´úÂëʵÏÖÒýÈëÒÀÀµ

    
        <dependency>
            <groupId>com.alipay.sdkgroupId>
            <artifactId>alipay-sdk-javaartifactId>
            <version>4.9.5.ALLversion>
        dependency>

²ÎÊýÅäÖÃ

Ó¦ÓÃ.yml

# Ö§¸¶±¦Ïà¹Ø
ALIPAY:
 # Ó¦ÓÃID
  APP_ID: 
  # Ó¦ÓÃ˽Կ
  APP_PRIVATE_KEY: 
  #Ó¦Óù«Ô¿
  APP_PUBLIC_KEY: 
  # Ö§¸¶±¦¹«Ô¿
  ALIPAY_PUBLIC_KEY: 
  # ͳһǩÃûËã·¨
  SIGN_TYPE: RSA2
  # ͳһ×Ö·û
  CHARSET: utf-8
  # ·µ»ØÊý¾Ý¸ñʽ
  FORMAT: json
  # Ö§¸¶»ù´¡API
  SERVER_URL: https://openapi.alipay.com/gateway.do
  #Ö§¸¶±¦Ö§¸¶»Øµ÷µØÖ·
  ALIPAY_NOTIFY_URL: 
  # Ö§¸¶±¦H5ÊÕÒø̨»á×Ô¶¯Ìøת»ØÉÌ»§return_urlÖ¸¶¨µÄÒ³Ãæ¡£
  RETURN_URL:
  # Ö§¸¶±¦Api°æ±¾
  API_VERSION: 1.0
   # ²úÆ·Âë 
  PROD_CODE: 
  # ÖÕ¶ËÐÅÏ¢
  TERMINAL_INFO: 
  # ÖÕ¶ËÐÅÏ¢ÀàÐÍ
  TERMINAL_TYPE: 
?

yml ²ÎÊý

    //Ö§¸¶±¦Ïà¹Ø
    @Value("$${ALIPAY.APP_ID}")
    private String appId;
?
    @Value("$${ALIPAY.APP_PRIVATE_KEY}")
    private String appPrivatekey;
?
    @Value("$${ALIPAY.APP_PUBLIC_KEY}")
    private String appPublickey;
?
    @Value("$${ALIPAY.ALIPAY_PUBLIC_KEY}")
    private String alipayPublickey;
?
    @Value("$${ALIPAY.SIGN_TYPE}")
    private String signType;
?
    @Value("$${ALIPAY.CHARSET}")
    private String charset;
?
    @Value("$${ALIPAY.FORMAT}")
    private String format;
?
    @Value("$${ALIPAY.SERVER_URL}")
    private String serverUrl;
?
    @Value("$${ALIPAY.ALIPAY_NOTIFY_URL}")
    private String alipayNotifyUrl;
?
    @Value("$${ALIPAY.RETURN_URL}")
    private String returnUrl;
?
    @Value("$${ALIPAY.API_VERSION}")
    private String apiVersion;
?
    @Value("$${ALIPAY.PROD_CODE}")
    private String prodCode;
?
    @Value("$${ALIPAY.TERMINAL_INFO}")
    private String terminalInfo;
?
    @Value("$${ALIPAY.TERMINAL_TYPE}")
    private String terminalType;
?

³õʼ»¯Ö§¸¶±¦Ö§¸¶ÅäÖÃ

Ö§¸¶±¦ÅäÖÃ

    @Autowired
   private YmlParament ymlParament;
   
   /**
    * ÉèÖÃÖ§¸¶±¦¿Í»§¶Ë
    * @return
    */
   @Bean
   public AlipayClient setAlipayClient() {
      return new DefaultAlipayClient(
            ymlParament.getServerUrl(), 
            ymlParament.getAppId(),
            ymlParament.getAppPrivatekey(), 
            ymlParament.getFormat(), 
            ymlParament.getCharset(), 
          //ÕâÀï²»Òª¸ã´í£¬ÕâÀïÊÇÓ¦Óù«Ô¿¶ø²»ÊÇÖ§¸¶±¦¹«Ô¿
            ymlParament.getAppPublickey(), 
            ymlParament.getSignType()
            ); 
   }
   
/**
     * ÉèÖÃÍøÒ³Ö§¸¶ÇëÇó»ù´¡²ÎÊý
     * @return
     */
    @Bean
    public AlipayTradeWapPayRequest setAlipayTradeWapPayRequest() {
        AlipayTradeWapPayRequest atwpr = new AlipayTradeWapPayRequest();
        atwpr.setApiVersion(ymlParament.getApiVersion());
        atwpr.setProdCode(ymlParament.getProdCode());// ²úÆ·Âë
        atwpr.setTerminalInfo(ymlParament.getTerminalInfo());// ÖÕ¶ËÐÅÏ¢
        atwpr.setTerminalType(ymlParament.getTerminalType());// ÖÕ¶ËÐÅÏ¢ÀàÐÍ
        return atwpr;
    }
?

ÊÖ»úÍøÕ¾Ö§¸¶

Öз½Ö§¸¶

    /**
     * ÊÖ»úÍøÕ¾Ö§¸¶
     * 
     * @param ac        Ö§¸¶±¦¿Í»§¶Ë£¬´«Èë @Autowired AlipayClient
     * @param rquest    »ù´¡²ÎÊý£¬´«Èë @Autowired AlipayTradeWapPayRequest
     * @param returnUrl ¸¶¿î³É¹¦ºóÌøתµÄÒ³Ãæ
     * @param notifyUrl ¸¶¿î³É¹¦ºó֪ͨµÄÒ³Ãæ»ò·½·¨
     * @param mode      ʵ¼Ê²ÎÊý£¬²Î¿¼£ºhttps://opendocs.alipay.com/open/203/107090
     */
    public static AlipayTradeWapPayResponse wapPay(AlipayClient ac, AlipayTradeWapPayRequest rquest, String returnUrl,
            String notifyUrl, AlipayTradeWapPayModel mode) throws AlipayApiException {
        rquest.setBizModel(mode);
        rquest.setReturnUrl(returnUrl);
        rquest.setNotifyUrl(notifyUrl);
        return ac.pageExecute(rquest);
    }
?
    /**
     * ÊÖ»úÍøÕ¾Ö§¸¶£¬´¦ÀíÒµÎñ²ÎÊý
     */
    public static AlipayTradeWapPayResponse wapPay(AlipayClient ac, AlipayTradeWapPayRequest rquest, String returnUrl,
            String notifyUrl, String orderNo,String subject,String orderAmount) throws AlipayApiException {
        AlipayTradeWapPayModel mode = new AlipayTradeWapPayModel();
        mode.setOutTradeNo(orderNo);
        mode.setSubject(subject);
        mode.setTotalAmount(orderAmount);
        return wapPay(ac, rquest, returnUrl, notifyUrl, mode);
    }
?
/**
     * Ö§¸¶±¦Ö§¸¶»Øµ÷Ñé֤ǩÃû
     * Ñé֤ǩÃû£»²Î¿¼£ºhttps://opendocs.alipay.com/open/203/105286
     * @param params       ²ÎÊý
     * @param alipayPublickey ´«ÈëÖ§¸¶±¦¹«Ô¿
     * @return true ÕýÈ· false ʧ°Ü
     * @throws AlipayApiException
     */
    public static boolean rsaCheckV1(Map<String, String> params, String alipayPublickey) throws AlipayApiException {
        return AlipaySignature.rsaCheckV1(params, alipayPublickey, "utf-8", "RSA2");
    }
?

·þÎñ²ã£¨ÒµÎñÂß¼­Ê¡ÂÔ£©

    @Autowired
    private AlipayClient alipayClient;
    @Autowired
    private AlipayTradeWapPayRequest alipayTradeWapPayRequest;  
?
?
    @Transactional
    @Override
    public String insertAlipayH5PayInfo(String sendOrderNo,String subject, String orderAmount){
        try {
            AlipayTradeWapPayResponse alipayTradeWapPayResponse = ZfbPay.wapPay(alipayClient, alipayTradeWapPayRequest, ymlParament.getReturnUrl(),
                    ymlParament.getAlipayNotifyUrl(), sendOrderNo, subject, orderAmount);
            /* »ñÈ¡Ò³Ãæ³É¹¦ */
            if (!alipayTradeWapPayResponse.isSuccess()) {
                throw new RuntimeException(alipayTradeWapPayResponse.getMsg());
            }
            return alipayTradeWapPayResponse.getBody();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

¿ØÖÆÆ÷£¨ÂÔ£©

µ½ÕâÀïÖ§¸¶±¦Ö§¸¶¾Í³É¹¦ÁË£¬Î¢ÐÅÖ§¸¶ÊDz»ÊǼòµ¥¶àÁË^_^£¬È»ºóÖ§¸¶±¦»Øµ÷֪ͨ³É¹¦ºó»á»Øµ÷Ò³ÃæºÍ·þÎñÆ÷

¹Ù·½Îĵµ£º/open/203/105286

?
    @ApiOperation("Ö§¸¶±¦»Øµ÷")
    @PostMapping("callback")
    public String callback(HttpServletRequest request) throws Exception {
        Object object = new Object();
        Map<String, String> params = convertRequestParamsToMap(request);
        log.info("Ö§¸¶±¦»Øµ÷£ºÒѻص÷£¬²ÎÊýΪ====" + params);
           try {
              checkCallbackAlipay(params);
               /*´¦ÀíһЩҵÎñ*/
            } catch (Exception e) {
                e.printStackTrace();
            }
            return ZfbUtils.RETURN_SUCCESS;
        }
    }
?
/**
     * УÑéÇ©Ãû
     *ÑéÇ©Îĵµ£ºhttps://opendocs.alipay.com/open/203/105286
     */
    private void checkCallbackAlipay(Map<String, String> params) throws Exception {
        if (!"TRADE_SUCCESS".equals(params.get("trade_status"))) {
            throw new Exception("Ö§¸¶±¦Ö§¸¶Ê§°Ü£¡");
        }
        //УÑéÇ©Ãû
        if (!ZfbPay.rsaCheckV1(params, ymlParament.getAlipayPublickey())) {
            throw new Exception("Ö§¸¶±¦»Øµ÷Ç©ÃûÈÏ֤ʧ°Ü");
        }
        //УÑéһЩҵÎñÂß¼­
        //¶©µ¥ÊÇ·ñÖ§¸¶Öظ´
        //ÅжÏtotal_amountÊÇ·ñȷʵΪ¸Ã¶©µ¥µÄʵ¼Ê½ð¶î£¨¼´ÉÌ»§¶©µ¥´´½¨Ê±µÄ½ð¶î£©
        //.....
        // УÑé֪ͨÖеÄseller_id£¨»òÕßseller_email)ÊÇ·ñΪout_trade_noÕâ±Êµ¥¾ÝµÄ¶ÔÓ¦µÄ²Ù×÷·½£¨ÓеÄʱºò£¬Ò»¸öÉÌ»§¿ÉÄÜÓжà¸öseller_id/seller_email£©£¬
        // ÑéÖ¤app_idÊÇ·ñΪ¸ÃÉÌ»§±¾Éí¡£
        if (!params.get("app_id").equals(ymlParament.getAppId())) {
            throw new Exception("APPID²»¶Ô£¡");
        }
    }
?
/**
     * »ñÈ¡Ö§¸¶±¦»Øµ÷²ÎÊý
     */
private static Map<String, String> convertRequestParamsToMap(HttpServletRequest request) {
        Map<String, String> retMap = new HashMap<String, String>();
        Set<Map.Entry<String, String[]>> entrySet = request.getParameterMap().entrySet();
        for (Map.Entry<String, String[]> entry : entrySet) {
            String name = entry.getKey();
            String[] values = entry.getValue();
            int valLen = values.length;
            if (valLen == 1) {
                retMap.put(name, values[0]);
            } else if (valLen > 1) {
                StringBuilder sb = new StringBuilder();
                for (String val : values) {
                    sb.append(",").append(val);
                }
                retMap.put(name, sb.toString().substring(1));
            } else {
                retMap.put(name, "");
            }
        }
        return retMap;
    }

½»Òײéѯ

Èç¹ûÄú²»ÏëÔڻص÷Ö§¸¶µÄʱºò×öÑéÇ©£¬ÄÇôÎÒÃÇ¿ÉÒÔÖ÷¶¯²éѯ¶©µ¥ÊÇ·ñÖ§¸¶³É¹¦

 /*²éѯ¶©µ¥ÊÇ·ñÖ§¸¶³É¹¦*/
AlipayTradeQueryResponse response = orderQuery(ac, params.get("out_trade_no"));
                if (!"TRADE_SUCCESS".equals(response.getTradeStatus())){
                    throw new Exception("<==Ö§¸¶±¦Ö§¸¶Ê§°Ü==>¶©µ¥ºÅΪ¡¾"+ params.get("out_trade_no")+ "¡¿µÄ¶©µ¥");
                }
?
/**
     * ½»Òײéѯ½Ó¿Ú£¬´¦ÀíÒµÎñ²ÎÊý                            https://opendocs.alipay.com/apis/api_1/alipay.trade.query#%E5%93%8D%E5%BA%94%E5%8F%82%E6%95%B0  
     * @param outTradeNo ¶©µ¥ºÅ
     * @return
     * @throws AlipayApiException
     */
    public static AlipayTradeQueryResponse orderQuery(AlipayClient ac , String outTradeNo) throws Exception {
        AlipayTradeQueryModel model = new AlipayTradeQueryModel();
        model.setOutTradeNo(outTradeNo);
        AlipayTradeQueryResponse response = tradeQuery(ac,model);
        if(!response.isSuccess()){
            throw new Exception("µ÷ÓÃÖ§¸¶±¦²éѯ½Ó¿Úʧ°Ü");
        }
        return response;
    }
?
/*½»Òײéѯ½Ó¿Ú*/
    public static AlipayTradeQueryResponse tradeQuery(AlipayClient ac,AlipayTradeQueryModel model) throws AlipayApiException{
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        request.setBizModel(model);
        return ac.execute(request);
    }

ÁíÒ»ÊÓ½Ç

»»Ò»»»