imadin

Paypal高级API付款代码范例

2011-05-18 15:20  卫佳  阅读(718)  评论(0编辑  收藏  举报

不仅仅Paypal,支付宝、财付通都一样一样的。

<?php

class PaypalAPI {

    private $api_authentication_mode = \'\';// 3TOKEN UNIPAY

    private $api_account = \'\';

    private $api_password = \'\';

    private $api_signature = \'\';

    private $api_subject = \'\';

    private $paypalPaymentUrl = \'https://www.paypal.com/webscr&cmd=_express-checkout&token=%s\';

    private $paypalApiUrl = \'https://api-3t.paypal.com/nvp\';

    private $paypalVersion = \'60.0\';

    private $query = array();

    private $goodNum = 0;

    private $amt = 0;

    private $error = \'\';

    /**

     * 获取支付URL

     */

    public function getPaymentUrl() {

        $this->initializeQuery( \'payment\' );

        $response = $this->request( $this->query );

        if( !$response ) {

            return FALSE;

        } else {

            return vsprintf($this->paypalPaymentUrl, $response[\'TOKEN\']);

        }

    }

    /**

     * 验证 TOKEN 是否合法wedding veil

     */

    public function validateToken($token) {

        $this->initializeQuery( \'validate\' );

        $this->setQuery(\'TOKEN\', $token);

        return $this->request( $this->query );

    }

    /**

     * 验证信息

     */

    public function getError() {

        return $this->error;

}

    /**

     * 添加商品

     */

    public function addGood($goodName, $price, $num=1, $desc=\'\') {

        $this->setQuery(\'L_NAME\'.$this->goodNum, $goodName);

        $this->setQuery(\'L_AMT\'.$this->goodNum, $price);

        $this->setQuery(\'L_QTY\'.$this->goodNum, $num);

        if( !empty($desc) ) {

            $this->setQuery(\'L_DESC\'.$this->goodNum, $desc);

        }

        $this->amt += $num*$price;

        $this->setQuery(\'AMT\', $this->amt);

        $this->goodNum++;

        return $this;

    }

    /**

     * 设置属性wedding crown

     */

    public function setAttr($key, $val) {

        $this->$key = $val;

        return $this;

    }

    /**

     *

     */

    public function setQuery($name, $val) {

        $this->query[$name] = $val;

        return $this;

    }

    private function initializeQuery( $type ) {

        $query = Array();

        $method = Array( \'validate\'=>\'GetExpressCheckoutDetails\', \'payment\'=>\'SetExpressCheckout\');

        $this->setQuery(\'METHOD\', isset($method[$type]) ? $method[$type] : \'\');

        $this->setQuery(\'VERSION\', $this->paypalVersion);

        switch( $this->api_authentication_mode ) {

            case \'3TOKEN\':

                $this->setQuery(\'PWD\', $this->api_password);

                $this->setQuery(\'USER\', $this->api_account);

                $this->setQuery(\'SIGNATURE\', $this->api_signature);

                break;

            case \'UNIPAY\':

                $this->setQuery(\'SUBJECT\', $this->api_subject);

                break;

            default:

                throw new Exception("faild authentication of <b>{$this->api_authentication_mode}</b>");

                break;

        }

    }

    private function request( $post ) {

        $ch = curl_init();

        $chOptions = array(

                CURLOPT_URL => $this->paypalApiUrl,

                CURLOPT_POST => TRUE,

                CURLOPT_POSTFIELDS => is_array($post) ? http_build_query($post) : $post,

                CURLOPT_VERBOSE => TRUE,

                CURLOPT_SSL_VERIFYPEER => FALSE,

                CURLOPT_SSL_VERIFYHOST => FALSE,

                CURLOPT_RETURNTRANSFER => TRUE,

            );

        curl_setopt_array($ch, $chOptions);

        $response = curl_exec($ch);

        if( curl_errno($ch) ) {

            $errno = curl_errno($ch);

            $error = curl_error($ch);

            curl_close($ch);

            throw new Exception( " {$errno} - {$error} " );

        }

        curl_close($ch);

        parse_str( urldecode($response), $response );

        if( $response[\'ACK\'] != \'Success\' ) {

            $this->error = $response;

            return false;

        }

        return $response;

    }

}

$paypal = new PaypalAPI();

$paymentUrl = $paypal->setAttr(\'api_account\', \'高级API帐号\')->setAttr(\'api_password\', \'API帐号密码而非paypal帐户密码\')->setAttr(\'api_signature\', \'密钥\')

                     ->addGood(\'test\',10)

                     ->addGood(\'针织毛衣\',103.5,2)

                     ->setQuery(\'RETURNURL\', \'http://www.baidu.com/\')

                     ->setQuery(\'CANCELURL\', \'http://www.baidu.com/\')

                     ->getPaymentUrl();

if( $paymentUrl ) {

    echo "<a href=\'$paymentUrl\' target=\'_blank\'>To pay</a>";

} else {

    print_r($paypal->getError());

}

复制代码

如果顺利,你将会看到类似的页面:

分类:

技术点:

相关文章: