Alipay single transfer payment function

Recently, I made a function, which involves the single transfer function of WeChat payment. I have to say that the payment speed of Alipay is still very powerful.

First of all, you must apply for corporate Alipay. This thing requires a business license. The application can be completed in 1-2 days. Finally, there will be a public account to pay 8.22 yuan. Now we mainly talk about the realization of the program

First, you first need to create a developer application that will assign you an appid

2. Carry out the corresponding function application to sign the contract, and the time can generally be passed in one day.

3. Fill in the public key. The public key can be generated with the tools provided by Alipay.

 

4 After the above execution is completed, we can develop functions according to the documentation

5. Request address https://openapi.alipay.com/gateway.do

, Through the document, we know that we actually send the corresponding parameters (public request parameters and request parameters) to Alipay

6. Alipay will give us corresponding feedback after receiving the parameters. When requesting parameters, we will put all the request parameters json into the public parameter biz_content. For details, please refer to the following code

    

<?php
require_once '../zhifubao/Rsa.class.php';
require_once '../zhifubao/Base.class.php';
require_once '../zhifubao/Alipay.class.php';
//Construction of payment request can pass MD5 RSA RSA2 three parameters
$obj = new Alipay();

$data = [
    'payee_account' => 'xxxxx', //Payee account
    'amount' => 'xxxx', //amount
];

//json data in UTF-8 format
$res = iconv('gbk','utf-8',$obj->transfer($data));


echo '<pre>';
//convert to array
$res = json_decode($res,true);

print_r($res);

  

 

 

 

  Alipay class

<?php
class Alipay extends Base
{
    const TRANSFER = 'https://openapi.alipay.com/gateway.do';

    public function __construct() {


    }

    //transfer
    public function transfer($data){
        // public request parameters
        $pub_params = [
            'app_id'    => self::APPID,
            'method' => 'alipay.fund.trans.toaccount.transfer', //The interface name should fill in the fixed value alipay.fund.trans.toaccount.transfer
            'format' => 'JSON', //Currently only supports JSON
            'charset'    =>  'UTF-8',
            'sign_type' => 'RSA2',//Signature method
            'sign' => '', //signature
            'timestamp' => date('Ymd H:i:s'), //send time format 0000-00-00 00:00:00
            'version' => '1.0', //fixed to 1.0
            'biz_content' => '', //A collection of business request parameters
        ];

        // request parameters
        $api_params = [
            'out_biz_no' => date('YmdHis'),//merchant transfer order number
            'payee_type' => 'ALIPAY_LOGONID', //Payee account type
            'payee_account' => $data['payee_account'], //Payee account
            'amount'  => $data['amount'], //金额
        ];
        $pub_params['biz_content'] = json_encode($api_params,JSON_UNESCAPED_UNICODE);
        $pub_params =  $this->setRsa2Sign($pub_params);
        return $this->curlRequest(self::TRANSFER, $pub_params);
    }
}

  Base class

<?php
class Base extends RSA
{
/**
*The following information needs to be modified according to your actual situation
*/
// own private key
const APPPRIKEY = 'xxxxx'; //Modify to your own application private key
//The APPID applied for
const APPID = 'xxxxx';
//Alipay public key, not your own generated public key
const NEW_ALIPUBKE ='xxxx'; //Modify to your own Alipay public key

public function getStr($arr,$type = 'RSA'){
    //filter
    if(isset($arr['sign'])){
    unset($arr['sign']);
    }
    if(isset($arr['sign_type']) && $type == 'RSA'){
    unset($arr['sign_type']);
    }
    //sort
    ksort($arr);
    //splicing
    return  $this->getUrl($arr,false);
}
// Convert the array to a string in url format
public function getUrl($arr,$encode = true){
    if($encode){
    return http_build_query($arr);
    }else{
    return urldecode(http_build_query($arr));
    }
}

//Get the signature RSA2
public function getRsa2Sign($arr){
    return $this->rsaSign($this->getStr($arr,'RSA2'), self::APPPRIKEY,'RSA2') ;
}
//Get the array containing the signature RSA
public function setRsa2Sign($arr){
    $arr['sign'] = $this->getRsa2Sign($arr);
    return $arr;
}
public function checkSign($arr){
    if($this->getRsa2Sign($arr) == $arr['sign']){
      return true;
    }else{
      return false;
    }
}

public function curlRequest($url,$data = ''){
    $ch = curl_init();
    $params[CURLOPT_URL] = $url; //Request url address
    $params[CURLOPT_HEADER] = false; //Whether to return response header information
    $params[CURLOPT_RETURNTRANSFER] = true; //Whether to return the result
    $params[CURLOPT_FOLLOWLOCATION] = true; //Whether to redirect
    $params[CURLOPT_TIMEOUT] = 30; //timeout time
    if(!empty($data)){
    $params[CURLOPT_POST] = true;
    $params[CURLOPT_POSTFIELDS] = $data;
    }
    $params[CURLOPT_SSL_VERIFYPEER] = false;//Set when requesting https, there are other solutions
    $params[CURLOPT_SSL_VERIFYHOST] = false;//When requesting https, see other blog posts for other solutions
    curl_setopt_array($ch, $params); //Incoming curl parameters
    $content = curl_exec($ch); //execute
    curl_close($ch); //Close the connection
    return $content;
}

}

  

rsa authentication class

<?php
class RSA
{
/**
* RSA signature
* @param $data data to be signed
* @param $private_key private key string
* return signature result
*/
function rsaSign($data, $private_key,$type = 'RSA') {

$search = [
"-----BEGIN RSA PRIVATE KEY-----",
"-----END RSA PRIVATE KEY-----",
"\n",
"\r",
"\r\n"
];

$private_key=str_replace($search,"",$private_key);
$private_key=$search[0] . PHP_EOL . wordwrap($private_key, 64, "\n", true) . PHP_EOL . $search[1];
$res=openssl_get_privatekey($private_key);

if($res)
{
if($type == 'RSA'){
openssl_sign($data, $sign,$res);
}elseif($type == 'RSA2'){
//OPENSSL_ALGO_SHA256
openssl_sign($data, $sign,$res,OPENSSL_ALGO_SHA256);
}
openssl_free_key($res);
}else {
exit("The private key format is incorrect");
}
$sign = base64_encode($sign);
return $sign;
}

/**
* RSA signature verification
* @param $data data to be signed
* @param $public_key public key string
* @param $sign the signature result to be proofread
* return verification result
*/
function rsaCheck($data, $public_key, $sign,$type = 'RSA')  {
$search = [
"-----BEGIN PUBLIC KEY-----",
"-----END PUBLIC KEY-----",
"\n",
"\r",
"\r\n"
];
$public_key=str_replace($search,"",$public_key);
$public_key=$search[0] . PHP_EOL . wordwrap($public_key, 64, "\n", true) . PHP_EOL . $search[1];
$res=openssl_get_publickey($public_key);
if($res)
{
if($type == 'RSA'){
$result = (bool)openssl_verify($data, base64_decode($sign), $res);
}elseif($type == 'RSA2'){
$result = (bool)openssl_verify($data, base64_decode($sign), $res,OPENSSL_ALGO_SHA256);
}
openssl_free_key($res);
}else{
exit("The public key format is incorrect!");
}
return $result;
}

}

  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325746140&siteId=291194637