This paper briefly introduces the application, access, use, confirmation of payment results and other related processes of Alipay instant payment, namely PC payment

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

1 application

Log in to Alipay merchant service and enter instant payment. Sign up.

After signing the contract, the following parameters are required to complete the payment:

  1. partnerid
  2. sellerid
  3. Rsa private key
  4. Alipay public Key

1.1 partnerid

Merchant partner identity.

Enter my merchant service to get the PID

Paste_Image.png

Note: Alipay also has an open platform, each application has an AppID, and a PID can have multiple Appids. However, the three payment channels, mobile payment, instant payment and payment on mobile website, are payment methods that only need PID to pay, and do not need application

1.2 sellerid

Login alipay account, usually email or mobile phone number

1.3 RSA Private Key and Alipay Public Key

Rsa private key The public key is generated by itself, and then the public key is uploaded to Alipay. The private key is saved by itself.

See documentation on the official website.

Paste_Image.png

2 Access Process

Refer to alipay mobile payment access document

The main steps are:

  1. Generate payment address (on the server side, need to generate signature)
  2. Open the address jump to alipay cashier for code scanning or password payment
  3. The server receives the payment result asynchronously

2.1 Generate payment address

$partner = "";  / / your pid
$seller_id = "";  //seller_id
$subject = "Alipay Mobile Payment Test";  // Transaction theme
$body = "Alipay mobile payment test detail";  // Transaction details
$total_fee = "0.01";    // The unit of payment is yuan
$out_trade_no = "";  // Transaction no generated by your own business system can be uniquely identified
$rsa_path = "";  // Path of the RSA private key
$notify_url = "";    // Receive payment result notification URL

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

$data = array(a); $data['service'] = "create_direct_pay_by_user"; 
$data['partner'] =$partner;
$data['_input_charset'] = "utf-8";
$data['notify_url'] = $notify_url;
$data['out_trade_no'] = $out_trade_no;    
$data['subject'] = $subject;
$data['payment_type'] = "1";
$data['seller_id'] = seller_id;
$data['total_fee'] = $total_fee;
$data['body'] = $body;

/ / 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

3 Asynchronous result notification

Note: in particular, it is necessary to correctly deal with repeated notifications after successful verification of notification results, and to place multiple shipments resulting in capital losses

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");

if($_POST['trade_status']! = ="TRADE_SUCCESS" && $_POST['trade_status']! = ="TRADE_FINISHED") {    
  if($_POST['trade_status'= = ="WAIT_BUYER_PAY") {                    
    // Wait bueyer pay notifications can be ignored
    die("success");    
  } else if($_POST['trade_status'= = ="TRADE_CLOSED" && $_POST['refund_status'= = ="REFUND_SUCCESS") {   // A full refund is also successful
    // A notification to closed may be triggered when the refund is successful, which can also be regarded as successful payment
  } else {    // Payment failed
    // Payment failure handles todo}}// Payment successfully processed shipment
//todoCopy the code

Five other

  1. After receiving the synchronous payment result, the client recommends polling and checking the server for a period of time to obtain the result of the server. The final payment status is subject to the server

At the end

More articles follow my public account

My official account