龚哥哥 - 山里男儿 爱生活、做自己!
PHP支付宝接口类 WEB版 即时到帐接口
发表于 2015-8-31 | PHP
<?php

/**
 * 支付宝支付驱动
 * @author  Devil
 * @version v_1.0.0
 */
class Alipay
{
    private $config;

    /**
     * [__construct 构造方法, 初始化配置信息]
     */
    public function __construct()
    {
        $this->config = array(
            'alipay_key'        =>  '', //key
            'alipay_partner'    =>  '', //partner
            'alipay_account'    =>  '', //支付宝账户名称
            'notify_url'        =>  '', //异步通知地址
            'call_back_url'     =>  '', //同步返回地址
        );
    }

    /**
     * [Payment 生成即时到帐支付信息]
     * @param  [array] $order [订单数据]
     */
    public function Payment($order)
    {
        $param = array(
            'service'           => 'create_direct_pay_by_user',
            'partner'           => $this->config['alipay_partner'],
            '_input_charset'    => 'utf-8',
            'notify_url'        => $this->config['notify_url'],
            'return_url'        => $this->config['call_back_url'],

            /* 业务参数 */
            'subject'           => $order['name'],
            'out_trade_no'      => $order['number_id'],
            'price'             => $order['total_fee'],
            'quantity'          => 1,
            'payment_type'      => 1,

            /* 物流参数 */
            'logistics_type'    => 'EXPRESS',
            'logistics_fee'     => 0,
            'logistics_payment' => 'BUYER_PAY_AFTER_RECEIVE',

            /* 买卖双方信息 */
            'seller_email'      => $this->config['alipay_account']
        );
        ksort($param);
        $string = '';
        $sign  = '';

        foreach($param AS $key=>$val)
        {
            $string .= "$key=" .urlencode($val). "&";
            $sign  .= "$key=$val&";
        }

        $string = substr($string, 0, -1);
        $sign  = md5(substr($sign, 0, -1).$this->config['alipay_key']);
        header('location:https://mapi.alipay.com/gateway.do?'.$string. '&sign='.$sign.'&sign_type=MD5');
    }

    /**
     * [Respond 异步请求处理]
     * @return [string] [成功success, 失败其它]
     */
    public function Respond()
    {
        /* 参数处理 */
        if (!empty($_POST))
        {
            foreach($_POST as $key => $val)
            {
                $_GET[$key] = $val;
            }
        }
        $param = $_GET;
        ksort($param);

        /* 判断是否已经处理过 */
        $this->IsRespond($param);

        /* 签名校验 */
        $sign = '';
        foreach($param AS $key=>$val)
        {
            if($key != 'sign' && $key != 'sign_type' && $key != 'code')
            {
                $sign .= "$key=$val&";
            }
        }
        $sign = md5(substr($sign, 0, -1).$this->config['alipay_key']);
        if($Sign == $param['sign'] && $param['trade_status'] == 'TRADE_SUCCESS')
        {
            //$param['out_trade_no'] 参数是唯一标识符
            exit('success');
        }
    }

    /**
     * [IsRespond 是否处理过操作]
     * @param [array] $param [参数]
     */
    private function IsRespond($param)
    {
        //$param['out_trade_no'] 参数是唯一标识符
        //如果处理过了可以直接exit('success');
    }
}

?>

发表评论:

TOP