PHP: 获得PayPal Transaction信息,payment 信息(GetTransactionDetails API), PHP paypal nvp

Paypal.php

<?php

class Paypal
{
    /**
     * API Version
     */
    const VERSION = 51.0;

    /**
     * List of valid API environments
     * @var array
     */
    private $allowedEnvs = array(
        'beta-sandbox',
        'live',
        'sandbox'
    );

    /**
     * Config storage from constructor
     * @var array
     */
    private $config = array();

    /**
     * URL storage based on environment
     * @var string
     */
    private $url;

    /**
     * Build PayPal API request
     *
     * @param string $username
     * @param string $password
     * @param string $signature
     * @param string $environment
     * @throws Exception
     */
    public function __construct($username, $password, $signature, $environment = 'sandbox')
    {
        if (!in_array($environment, $this->allowedEnvs)) {
            throw new Exception('Specified environment is not allowed.');
        }
        $this->config = array(
            'username'    => $username,
            'password'    => $password,
            'signature'   => $signature,
            'environment' => $environment
        );
    }

    /**
     * Make a request to the PayPal API
     *
     * @param  string $method API method (e.g. GetBalance)
     * @param  array $params Additional fields to send in the request (e.g. array('RETURNALLCURRENCIES' => 1))
     * @return array
     * @throws Exception
     */
    public function call($method, array $params = array())
    {
        $fields = $this->encodeFields(array_merge(
            array(
                'METHOD'    => $method,
                'VERSION'   => self::VERSION,
                'USER'      => $this->config['username'],
                'PWD'       => $this->config['password'],
                'SIGNATURE' => $this->config['signature']
            ),
            $params
        ));
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $this->getUrl());
        curl_setopt($ch, CURLOPT_POST, count($fields));
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $response = curl_exec($ch);
        if (!$response) {
            throw new Exception('Failed to contact PayPal API: ' . curl_error($ch) . ' (Error No. ' . curl_errno($ch) . ')');
        }
        curl_close($ch);
        parse_str($response, $result);
        return $this->decodeFields($result);
    }

    /**
     * Prepare fields for API
     *
     * @param  array  $fields
     * @return array
     */
    private function encodeFields(array $fields)
    {
        return array_map('urlencode', $fields);
    }

    /**
     * Make response readable
     *
     * @param  array  $fields
     * @return array
     */
    private function decodeFields(array $fields)
    {
        return array_map('urldecode', $fields);
    }

    /**
     * Get API url based on environment
     *
     * @return string
     */
    private function getUrl()
    {
        if (is_null($this->url)) {
            switch ($this->config['environment']) {
                case 'sandbox':
                case 'beta-sandbox':
                    $this->url = "https://api-3t.sandbox.paypal.com/nvp";
                    break;
                default:
                    $this->url = 'https://api-3t.paypal.com/nvp';
            }
        }
        return $this->url;
    }
}

 

用法:

<?php

include 'Paypal.php';
$paypal = new Paypal('username', 'password', 'signature');

$response = $paypal->call('GetTransactionDetails', array('TransactionID' => 'your-transactioni-id-string'));
// $response = $paypal->call('PaymentDetails', array('PaymentID' => 'your-payment-id-string'));

print_r($response);

 

PHP: 获得PayPal Transaction信息,payment 信息(GetTransactionDetails API), PHP paypal nvp
PHP: 获得PayPal Transaction信息,payment 信息(GetTransactionDetails API), PHP paypal nvp

 

API操作

  • AddressVerify – 验证帐户持有人的地址和邮政编码。这是一个限制版API。要确定您是否可以使用此限制版API,请与您的PayPal集成团队联系。
  • GetTransactionDetails – 检索有关交易的信息。您指定交易ID,返回的信息包括:交易日期,金额,状态等。使用GetTransactionDetails 使用NVP / SOAP API时。
  • GetBalance – 显示PayPal帐户的可用余额。
  • PaymentDetails – 允许您开发显示自定义付款信息的应用:发件人电子邮件,货币,即时付款通知URL,付款状态等等。跟踪ID,IPN交易ID或付款密钥指定您想要的付款。使用Adaptive API时使用PaymentDetails。

参考

PHP: 获得PayPal余额(GetBalance API), PHP paypal nvp

 

 

本文:PHP: 获得PayPal Transaction信息,payment 信息(GetTransactionDetails API), PHP paypal nvp

One Comment

Leave a Reply