PHP code example of cncal / alipay

1. Go to this page and download the library: Download cncal/alipay 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/ */

    

cncal / alipay example snippets


    Cncal\Alipay\AlipayServiceProvider::class,
    

    'Alipay' => Cncal\Alipay\Facades\Alipay::class,
    

return [
    'gateway_url' => "https://openapi.alipay.com/gateway.do",

    'app_id' => "",

    'alipay_public_key' => "",

    'merchant_private_key' => "",

    'charset' => "UTF-8",

    'sign_type' => "RSA2",

    'notify_url' => "",

    'return_url' => "",
];


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Alipay;

class AlipayController extends Controller 
{
    /**
     * 支付.
     * https://docs.open.alipay.com/api_1/alipay.trade.pay
     */
    public function pay(Request $request)
    {
       $this->validate($request, [
            // 商户订单号,64个字符以内、可包含字母、数字、下划线;需保证在商户端不重复
           'out_trade_no' => '��
     * https://docs.open.alipay.com/api_1/alipay.trade.query 
     */
    public function query(Request $request)
    {
       $this->validate($request, [
           // 订单支付时传入的商户订单号,和支付宝交易号不能同时为空 
           // trade_no, out_trade_no 如果同时存在优先取 trade_no
           'out_trade_no' => 'refund_reason', 'refund_amount']);

       $response = Alipay::refund($data);
    } 
    
    /**
     * 退款查询.
     * https://docs.open.alipay.com/api_1/alipay.trade.fastpay.refund.query
     */
    public function refundQuery(Request $request)
    {
       $this->validate($request, [
           'out_trade_no' => 'de_status = $_POST['trade_status'];
            
            if ($_POST['trade_status'] == 'TRADE_FINISHED') {
                // 交易已完成,不能退款
            } else if ($_POST['trade_status'] == 'TRADE_SUCCESS') {
                // 支付成功
            }
            
            // 不要修改或删除此行
            echo "success";
        } else {
            echo "fail";
        }
    }
    
    /**
     * 同步回调方法
     */
    public function paidReturn()
    {
        // 如果配置的同步回调地址有附加参数,例如 `?date=***`,unset 它们 (unset($_GET['data']))
        if (Alipay::check($_GET)) {
            // 一般地,跳转到支付成功页面
            $out_trade_no = $_GET['out_trade_no'];
            $trade_no = $_GET['trade_no'];
            
            return view('trade_success');
        } else {
            echo "fail";
        }
    }
}