PHP code example of techleeone / alipay-php

1. Go to this page and download the library: Download techleeone/alipay-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

techleeone / alipay-php example snippets


composer 

// 业务请求参数
$bizcontent = [
    // [可选]对一笔交易的具体描述信息。如果是多种商品,请将商品描述字符串累加传给body
    // 'body'            => '我是测试数据',
    // [必填]商品的标题/交易标题/订单标题/订单关键字等。
    'subject'      => 'App支付测试',
    // [必填]商户网站唯一订单号
    'out_trade_no' => '20170125test011',
    // [必填]订单总金额,单位为元,精确到小数点后两位,取值范围[0.01,100000000]
    'total_amount' => '0.01',
    // [可选]该笔订单允许的最晚付款时间,逾期将关闭交易。默认为配置中的timeout_express值
    // 'timeout_express' => '30m',
];
// 如果是电脑网站支付、手机网站支付,执行execute方法后页面将自动跳转至支付宝支付页面,用户支付完成后,返回至return_url地址,并异步通知notify_url地址。
// 如果是app支付,执行execute方法后会返回一串app请求所用的orderstring,例如:alipay_sdk=alipay-sdk-php-20161101&app_id=2016091300500827&biz_conten……
$result = $pay->biz($bizcontent)->execute();

// 可选用法
// 1、是否自动跳转至支付宝支付页面,
$jumpAuto = false; // 如果为false,将返回一个html表单字符串
$result   = $pay->biz($bizcontent)->execute($jumpAuto);
echo $result;
// 2、针对每笔订单独立设置异步通知地址或返回地址
$pay->setNotifyUrl('异步地址');
$pay->setReturnUrl('同步地址');


$bizcontent = [
    'out_trade_no'  => '20170125test01', // 订单支付时传入的商户订单号,不能和 trade_no同时为空。
    // 'trade_no'      => '2014112611001004680073956707', // 支付宝交易号,和商户订单号不能同时为空
    'refund_amount' => '0.01', // 需要退款的金额,该金额不能大于订单金额,单位为元,支持两位小数
    'refund_reason' => '正常退款', // 退款的原因说明
];
$result = $pay->biz($bizcontent)->refund();
if ($result->code == 10000) {
    // 成功
}