欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java版微信和支付宝退款接口

程序员文章站 2024-03-07 08:32:44
本文实例为大家分享了java微信退款接口和支付宝退款接口的具体代码,供大家参考,具体内容如下 1、微信退款接口  相对来说我感觉微信的退款接口还是比较好调用的...

本文实例为大家分享了java微信退款接口和支付宝退款接口的具体代码,供大家参考,具体内容如下

1、微信退款接口 

相对来说我感觉微信的退款接口还是比较好调用的,直接发送httppost请求即可;

/**
  * 
  * 微信退款
  * @param transaction_id 微信支付订单号
  * @param out_refund_no 商户订单号
  * @param total_fee 总金额
  * @param refund_fee 退款金额
  * @param op_user_id 操作人
  * @return string
  * @exception
  */
 public string wxpayrefundrequest(string transaction_id, string out_refund_no,
   int total_fee, int refund_fee, string op_user_id) {
  closeablehttpclient httpclient = null;
  closeablehttpresponse response = null;
  string strresponse = null;
  try {
   httpclient = clientcustomssl.getcloseablehttpclient();
   // 构造http请求
   httppost httppost = new httppost(configure.pay_refund_api);
   // payrefundreqdata wxdata = new payrefundreqdata(
   // "1004720096201602263541023415", "16371", 30, 30, "19417");
   payrefundreqdata wxdata = new payrefundreqdata(transaction_id,
     out_refund_no, total_fee, refund_fee, op_user_id);
   string requeststr = util.convertobj2xml(wxdata);
   stringentity se = new stringentity(requeststr.tostring());
   httppost.setentity(se);
   // 发送请求
   response = httpclient.execute(httppost);
 
   httpentity entity = response.getentity();
   if (entity != null) {
    saxreader saxreader = new saxreader();
    document document = saxreader.read(entity.getcontent());
    element rootelt = document.getrootelement();
    // 结果码
    string returncode = rootelt.elementtext("return_code");
    string resultcode = rootelt.elementtext("result_code");
    if ("success".equals(returncode)&&"success".equals(resultcode)) {
     strresponse=returncode;
    }else {
     strresponse=rootelt.elementtext("err_code_des");
    }
   }
    entityutils.consume(entity); 
  } catch (exception e) {
   logger.getlogger(getclass()).error("payrefundrequest", e);
  } finally {
   try {
    response.close();
    httpclient.close();
   } catch (ioexception e) {
    // todo auto-generated catch block
    logger.getlogger(getclass()).error("payrefundrequest关闭异常:", e);
   }
  }
  return strresponse;
 }

报错的话请检查加密的sign是否正确,还有就是调用的接口地址是否正确

2、支付宝退款接口 

支付宝直接导入支付宝封装好的jar包直接调用即可,官网

调用方法:

/**
  * 
  * 支付宝退款请求
  * @param out_trade_no 订单支付时传入的商户订单号,不能和 trade_no同时为空。
  * @param trade_no 支付宝交易号,和商户订单号不能同时为空
  * @param refund_amount 需要退款的金额,该金额不能大于订单金额,单位为元,支持两位小数
  * @return 
  * string
  * @exception 
  */
 public string alipayrefundrequest(string out_trade_no,string trade_no,double refund_amount){
 
  // 发送请求
  string strresponse = null;
  try {
   alipayclient alipayclient = new defaultalipayclient
     (alipayconfig.alipayurl,alipayconfig.appid,
       alipayconfig.private_key,alipayconfig.content_type,alipayconfig.input_charset,alipayconfig.ali_public_key);
   alipaytraderefundrequest request = new alipaytraderefundrequest();
   alipayrefundinfo alidata= new alipayrefundinfo();
   alidata.setout_trade_no(out_trade_no);
   alidata.setrefund_amount(refund_amount);
   alidata.settrade_no(trade_no);
   request.setbizcontent(jsonutils.converttostring(alidata));
   alipaytraderefundresponse response = alipayclient.execute(request);
   strresponse=response.getcode();
   if ("10000".equals(response.getcode())) {
    strresponse="退款成功";
   }else {
    strresponse=response.getsubmsg();
   }
  } catch (exception e) {
   logger.getlogger(getclass()).error("alipayrefundrequest", e);
  }
  return strresponse;
 
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。