Alipay Refund Interface (Java)

Source: Internet
Author: User
Tags string format

View Alipay refund Documents: https://doc.open.alipay.com/docs/api.htm?spm=a219a.7629065.0.0.PbaltX&apiId=759&docType=4

Official documents written in detail, according to the document to modify the parameters of the debugging can be, most of the errors appear in the parameters above.

After reading the public request parameters and request parameters, look at the request instance and paste the request instance into your own code:


The request parameter shows clearly that there are only two required parameters: Merchant Order number (OUT_TRADE_NO) and Alipay transaction number (TRADE_NO) two, and the other must request parameter is: Refund Amount (Refund_amount)

Import SDK


Import into Project

Write your own code below

Import com.alipay.api.AlipayClient;
Import com.alipay.api.DefaultAlipayClient;
Import com.alipay.api.request.AlipayTradeRefundRequest;
Import Com.alipay.api.response.AlipayTradeRefundResponse;
Import Com.rongke.web.config.AlipayConfig;


public class Alipayrefundutil {


   /**  Alipay refund Interface
     * @param Out_trade_no The merchant order number that is passed in when the order is paid, cannot be empty at the same time as the Alipay transaction number (TRADE_NO).
     * @param trade_no Alipay transaction number
     * @param refund_amount The amount to be refunded, the amount cannot be greater than the order amount, the unit is Yuan, Supports two-bit decimal
     * @return will be prompted to return
     */
    public  Synchronized St atic  string Alipayrefundrequest (String out_trade_no,string trade_no,double refund_amount) {
    & nbsp   String returnstr = null;

        String out_request_no= "bz35581r88001";//random number   not a full refund, partial refund Use this parameter
    & nbsp   try {
            alipayclient alipayclient = new Defaultalipayclient (alipayconfi G.request_url, alipayconfig.app_id, Alipayconfig.private_refund_key, Alipayconfig.format, AlipayConfig.input_ CharSet, Alipayconfig.ali_refund_key, "RSA2");
            Alipaytraderefundrequest request = new Alipaytraderefundrequest ();
            request.setbizcontent ("{" +
            & nbsp       "\ out_trade_no\": \ "" + Out_trade_no + "\" "+
              & nbsp     "\" trade_no\ ": \" "+ Trade_no +" \ "," +
                  &N Bsp "\" refund_amount\ ": \" "+ Refund_amount +" \ "," +

"\" out_request_no\ ": \" "+ out_request_no+" \ "," +
"\ refund_reason\": \ "normal refund \" "+
" }");
Alipaytraderefundresponse response;
Response = Alipayclient.execute (request);
if (response.issuccess ()) {
SYSTEM.OUT.PRINTLN ("Alipay refund successful");
} else {
Returnstr = Response.getsubmsg ();//Failure returns an error message
}


catch (Exception e) {
E.printstacktrace ();
}
return returnstr;
}


public static void Main (String str[]) {
String strq= alipayrefundrequest ("109adb5aa64878314ab184e44f4c6f07", "", 0.1D);
System.out.println (STRQ);
}


}


Public parameter Description:

These parameters to pay the Treasure merchant platform view

APP_ID: Alipay Merchant Platform Account management view


Your Private_key: Key needs to upload the production public key to the Alipay, the key left in the code to use

Alipay_public_key: Alipay Public key

RSA2: Before the signature algorithm type is used with RSA now RSA2 the signature mismatch error

Other parameters are not modified

Parameter View location


RSA2 application of the public key can be modified, need to use the authentication code. Public key, private key generation tool The website has

Start Debugging below

I am debugging error: The first is the signature mismatch problem, the reason Signature algorithm type is RSA to build a RSA2 after the problem

Second error: Invalid argument most likely your private key is wrong or your request parameter format is wrong

View Alipay transaction refund results after running:


=========================== Below is the Alipayconfig tool class ===========

public class Alipayconfig {

↓↓↓↓↓↓↓↓↓↓ Please configure your basic information here ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓


Partner ID, a string of 16-digit pure digits beginning with 2088
public static String partner = "2088521599027790";
public static String Seller_email = "hzqishi@163.com";
Merchant's private key
public static String Private_key = "";
Merchant's private key refund use
public static String private_refund_key= "";
Alipay's public key, no need to modify the value
public static String Ali_public_key = "migfma0gcsqgsib3dqebaquaa4gnadcbiqkbgqcnxj/9qwvfgouh/ y2w89l6bkrafljhnhgpdypubv64bfqnn1pjbczkim6qrdkbolpxmkkmifynkd6raoprih3/prqeb/vsw8oom8fxn67udyuybtqa23mml9q1+ Ilizwbc2aq2ubvorfxffl75p6/b5ksing9zpgmlcuyulkxplqidaqab ";
Alipay's public key, refund use
public static String ali_refund_key= "";
For debugging, create a TXT log folder path, see the Logresult (String Sword) printing method in the Alipaycore.java class.
public static String Log_path = "d:\\";
The character encoding format currently supports GBK or Utf-8
public static String Input_charset = "Utf-8";
Parameter returns a format that supports only JSON
public static String format = "JSON";
Signature mode does not need to be modified
public static String Sign_type = "RSA";
public static String Request_url = "Https://openapi.alipay.com/gateway.do";
public static String app_id = "";
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.