Chinaunix首页 | 论坛 | 博客
  • 博客访问: 95598
  • 博文数量: 109
  • 博客积分: 3411
  • 博客等级: 中校
  • 技术积分: 1155
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-01 09:04
文章分类

全部博文(109)

文章存档

2011年(109)

分类: 系统运维

2011-05-18 15:35:18


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

 

class PaypalAPI {

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

    private $api_account = '';

    private $api_password = '';

    private $api_signature = '';

    private $api_subject = '';

    private $paypalPaymentUrl = '%s';

    private $paypalApiUrl = '';

    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 是否合法

     */

    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;

    }

    /**

     * 设置属性

     */

    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 {$this->api_authentication_mode}");

                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', '')

                     ->setQuery('CANCELURL', '')

                     ->getPaymentUrl();

if( $paymentUrl ) {

    echo "To pay";

} else {

    print_r($paypal->getError());

}

复制代码

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

阅读(524) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~