This paper briefly introduces the processing of alipay refund request and some matters needing attention

Series of articles

Series 1 wechat App payment full resolution series 2 Alipay App Payment full resolution series 3 wechat public account payment full resolution series 4 wechat scan code payment full resolution series 5 Alipay instant payment full resolution series 6 wechat refund full resolution series 7 Alipay refund full resolution series 8 Full analysis of alipay open platform payment updates and upgrades

There are three main steps for alipay refund:

  1. Generate refund link
  2. Open the refund link, jump to the Alipay cashier, enter the password to confirm the refund
  3. Receive refund result notification asynchronously

Official documentation and Demo

Note: This refund is applicable to the three payment channels of instant payment, mobile website and mobile payment

1 Generate a refund link

This interface supports batch refunds. One transaction can be refunded for a maximum of 99 times. The total amount of refunds should not exceed the payment amount of the transaction.

$partner = "";  / / your pid
$seller_id = "";  //seller_id
$rsa_path = "";  // Path of the RSA private key
$notify_url = "";    // Receive refund result notification URL
$batch_no = "";  // Follow the batchno format of the interface
$batch_detail = "";    // Batch concatenate strings. For details, see the documentation

$MAPI_GATEWAY = "https://mapi.alipay.com/gateway.do";    // mAPI entry route

$data = array(a); $data['service'] = "refund_fastpay_by_platform_pwd"; 
$data['partner'] =$partner;
$data['_input_charset'] = "utf-8";
$data['notify_url'] = $notify_url;
$data['seller_email'] = $seller_id;    
$data['refund_date'] = date('Y-m-d H:i:s');
$data['batch_no'] = $batch_no;
$data['batch_detail'] = batch_detail;

/ / signature
$unsign_str =createLinkString(argSort($data));
$sign =rsaSign($unsign_str, $rsa_path);

$params = createLinkstringUrlencode($data);

$url = $MAPI_GATEWAY . '? ' . $params;Copy the code

Some functions:

/** * concatenates all elements of the array into a string * using the ampersand character in the pattern "parameter = parameter value"@param$para mixed specifies the array to concatenate@returnString The concatenated string */
public static function createLinkString($para) {    
  $arg  = "";    
  while (list ($key, $val) = each ($para)) {        
    if($val == "") {            
      continue;        
    }        
    $arg.=$key."=".$val."&";    
  }    
  // Remove the last & character
  $arg = substr($arg,0,count($arg)2 -);    
  // If there are escape characters, remove the escape
  if(get_magic_quotes_gpc()){
    $arg = stripslashes($arg);
  }    
  return $arg;
  }

/** * Concatenates all elements of the array into a string with the amputated character "parameter = parameter value" and urlencodes the string *@param$para mixed specifies the array to concatenate@returnString The concatenated string */
public static function createLinkstringUrlencode($para) {    
  $arg  = "";    
  while (list ($key, $val) = each ($para)) {        
    if($val == "") {            
      continue;        
    }        
    $arg.=$key."=".urlencode($val)."&";
  }    
  // Remove the last & character
  $arg = substr($arg,0,count($arg)2 -);    
  // If there are escape characters, remove the escape
  if(get_magic_quotes_gpc()){
    $arg = stripslashes($arg);
  }    
  return $arg;
  }

/** * Array sort by ASCII dictionary ascending *@param$para mixed pre-sort array *@returnMixed sorted array */
public static function argSort($para) {    
  ksort($para);    
  reset($para);    
  return $para;
}

/** * RSA signature *@param$data string Data to be signed *@param$private_RSA_PATH string User private key address *@returnMixed * failed :false * successful: signature result */
public static function rsaSign($data, $private_rsa_path) {    
  $private_rsa = file_get_contents($private_rsa_path);    
  $res = openssl_get_privatekey($private_rsa);    
  if(! $res) {return false;    
  }    
  openssl_sign($data, $sign, $res);    
  openssl_free_key($res);    
  / / base64 encoding
  $sign = base64_encode($sign);    
  return $sign;
}Copy the code

Notice of refund result

Verify signature can directly download Alipay SDK example, direct call. Download address: doc.open.alipay.com/doc2/detail… Open the server Demo which will verify the notification part out to use.

? alipay_partnerid ="";  / / your pid? alipay_public_key_path ="";  // Alipay public key path

$alipayNotify = new AlipayNotify($alipay_partnerid, $alipay_public_key_path);
$verify_result = $alipayNotify->verifyNotify();
if(! $verify_result) {// Signature verification failed todo
  die("fail");
}

// The notification was successfully received and validated
echo("success");

// Parse the status of each refund in result_Details and update the status of the refund
//todo
}

// Payment successfully processed shipment
//todoCopy the code

At the end

More articles follow my public account

My official account