Home  >  Article  >  Backend Development  >  Alipay development function of PHP function

Alipay development function of PHP function

王林
王林Original
2023-05-19 08:18:221563browse

With the popularity of mobile payments and the continuous development of Internet technology, Alipay has become the preferred tool for many businesses and individuals to conduct transactions and fund transfers. For developers, it is very necessary to be proficient in Alipay's development functions. This article will introduce some commonly used PHP functions to help developers better implement Alipay-related functions.

  1. Alipay configuration function

Before using the Alipay function, Alipay configuration is required, which mainly includes the configuration of public key, private key, APP ID, Alipay gateway and other information. The following are common Alipay configuration functions.

(1) Set the Alipay gateway and APP ID

define('ALIPAY_GATEWAY', 'https://openapi.alipay.com/gateway.do');
define('APP_ID', 'your_app_id');

(2) Set the application public key, application private key and Alipay public key

define('APP_PRIVATE_KEY_PATH', 'path/to/app_private_key.pem');
define('ALIPAY_PUBLIC_KEY_PATH', 'path/to/alipay_public_key.pem');
  1. Alipay payment function

(1) Initiate payment request

function buildRequest($params)
{
    $md5 = rsaSign(md5Sign($params));
    $params['sign'] = $md5;
    $params['sign_type'] = 'RSA2';

    $result = doPost(ALIPAY_GATEWAY, $params);

    return $result;
}

(2) MD5 signature function

function md5Sign($params)
{
    ksort($params);
    $stringToBeSigned = '';

    foreach ($params as $k => $v) {
        if ('sign' != $k && 'sign_type' != $k && '' != $v) {
            $stringToBeSigned .= "$k=$v&";
        }
    }

    $stringToBeSigned = substr($stringToBeSigned, 0, -1);
    $stringToBeSigned .= APP_PRIVATE_KEY;

    return md5($stringToBeSigned);
}

(3) RSA2 signature function

function rsaSign($data) 
{
    $res = openssl_get_privatekey(file_get_contents(APP_PRIVATE_KEY_PATH));
    openssl_sign($data, $signature, $res, OPENSSL_ALGO_SHA256);
    openssl_free_key($res);

    return base64_encode($signature);
}
  1. Alipay refund function

(1) Initiate a refund request

function buildRefundRequest($params)
{
    $result = doPost(ALIPAY_GATEWAY, $params);

    return $result;
}

(2) Verify the refund status

function checkRefundStatus($result)
{
    $response = json_decode($result, true);

    if ($response['alipay_trade_refund_response']['code'] == '10000') {
        return true;
    } else {
        return false;
    }
}
  1. Alipay query function

(1) Initiate a query request

function buildQueryRequest($params)
{
    $params['method'] = 'alipay.trade.query';

    $result = doPost(ALIPAY_GATEWAY, $params);

    return $result;
}

(2) Parse the query results

function parseQueryResult($result)
{
    $response = json_decode($result, true);

    if ($response['alipay_trade_query_response']['code'] == '10000') {
        return true;
    } else {
        return false;
    }
}

In summary, this article introduces the common PHP functions in Alipay development, including Alipay configuration function, payment function, refund function and query function. By mastering these functions, developers can more easily implement various Alipay-related functions. At the same time, in order to ensure the security and smooth progress of transactions, developers need to follow Alipay's development specifications and processes, strictly implement the regulations on withdrawals and refunds, and frequently update Alipay-related security policies and technical means.

The above is the detailed content of Alipay development function of PHP function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn