Paypal REST Api实现

麦考伊

我是新手,所以我使用在Paypal开发人员博客上找到的代码,因此很难为我工作。这是我正在使用的代码

class paypal {
    private $access_token;
    private $token_type;

    /**
    * Constructor
    *
    * Handles oauth 2 bearer token fetch
    * @link https://developer.paypal.com/webapps/developer/docs/api/#authentication--headers
    */
    public function __construct(){
        $postvals = "grant_type=client_credentials";
        $uri = PAYMENT_URI . "v1/oauth2/token";

        $auth_response = self::curl($uri, 'POST', $postvals, true);
        $this->access_token = $auth_response['body']->access_token;
        $this->token_type = $auth_response['body']->token_type;
    }

    /**
    * cURL
    *
    * Handles GET / POST requests for auth requests
    * @link http://php.net/manual/en/book.curl.php
    */
    private function curl($url, $method = 'GET', $postvals = null, $auth = false){
        $ch = curl_init($url);

        //if we are sending request to obtain bearer token
        if ($auth){
            $headers = array("Accept: application/json", "Accept-Language: en_US");
            curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
            curl_setopt($ch, CURLOPT_USERPWD, CLIENT_ID . ":" .CLIENT_SECRET);
            curl_setopt($ch, CURLOPT_SSLVERSION, 3);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        //if we are sending request with the bearer token for protected resources
        } else {
            $headers = array("Content-Type:application/json", "Authorization:{$this->token_type} {$this->access_token}");
        }

        $options = array(
            CURLOPT_HEADER => true,
            CURLINFO_HEADER_OUT => true,
            CURLOPT_HTTPHEADER => $headers,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_VERBOSE => true,
            CURLOPT_TIMEOUT => 10
        );

        if ($method == 'POST'){
            $options[CURLOPT_POSTFIELDS] = $postvals;
            $options[CURLOPT_CUSTOMREQUEST] = $method;
        }

        curl_setopt_array($ch, $options);

        $response = curl_exec($ch);
        $header = substr($response, 0, curl_getinfo($ch,CURLINFO_HEADER_SIZE));
        $body = json_decode(substr($response, curl_getinfo($ch,CURLINFO_HEADER_SIZE)));
        curl_close($ch);

        return array('header' => $header, 'body' => $body);
    }

    // Function for Processing Payment
    function process_payment($request) {
        $postvals = $request;
        $uri = PAYMENT_URI . "v1/payments/payment";
        return self::curl($uri, 'POST', $postvals);
    }
}

if (isset($_SESSION['payment_type']) && ($_SESSION['payment_type'] == 'paypal')) { // User has chosen to pay with Paypal.


    // Retrive Shopping cart contents
    $r = mysqli_query($dbc, "CALL get_shopping_cart_contents('$uid')");

    $request = array(
        'intent' => 'sale',
        'redirect_urls' => array(
            'return_url' =>'http://store.example.com/final',
            'cancel_url' =>'http://store.example.com/payment'
        ),
        'payer' => array(
            'payment_method' =>'paypal'
        ),
        'transactions' => array(
            'amount' => array(
                'total' =>''.number_format($order_total,2).'',
                'currency' =>'USD',
                'details' => array(
                    'subtotal' =>''.number_format($subtotal,2).'',
                    'shipping' =>''.number_format($shipping,2).''
                ),
                'item_list' => array(

                )
            ),
            'description' =>'Mike and Maureen Photography - Order ID #'.$order_id.''
        )
    );

    while ($items = mysqli_fetch_array($r, MYSQLI_ASSOC)) {

        $newitems = array(
            'quantity' =>''.$items['quantity'].'',
            'name' =>''.$items['name'].'',
            'price' =>''.get_price($items['price'],$items['sales_price']).'',
            'currency' =>'USD'
        );
        $request['transactions']['amount']['item_list']['items'][] = $newitems;  
    }

    $request = json_encode($request);

    process_payment($request);
}

我对php很好,但是整个类,公共/私有的东西都让我失望。我可以不使用该代码吗?还是会给我带来麻烦?如何运行process_payment函数而不会引发错误。我收到“致命错误:调用未定义的函数process_payment()”

我只是在贝宝类中定义了功能吗?我已经阅读了Paypal上的文档,但无法了解自己在做什么错。任何帮助都会很棒。

狄龙·韦尔奇(Dillon Welch)

函数process_payment()是paypal类的一部分。要调用它,您将必须执行此处概述的方法之一:https : //stackoverflow.com/a/4216347/1715048

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Paypal REST API在Python中的实现

来自分类Dev

Paypal Rest API的工作

来自分类Dev

Paypal Rest Api错误

来自分类Dev

PayPal Rest API直接付款

来自分类Dev

Paypal API:REST还是CLASSIC?

来自分类Dev

PayPal Rest API发票WebHook

来自分类Dev

PayPal Rest API发票WebHook

来自分类Dev

使用Paypal Merchant SDK或Paypal REST API进行定期付款

来自分类Dev

Paypal Rest API:如何删除账单计划?

来自分类Dev

PayPal REST API等同于GetExpressCheckoutDetails

来自分类Dev

PayPal从IPN过渡到REST API

来自分类Dev

使用REST API的PayPal批量付款

来自分类Dev

带有通知的PayPal REST API

来自分类Dev

在经典ASP中使用Paypal REST API

来自分类Dev

使用PayPal REST API,如何取消付款?

来自分类Dev

使用Java Rest API进行PayPal登录

来自分类Dev

PayPal REST API更新计费协议

来自分类Dev

无法为Paypal REST API创建应用

来自分类Dev

Paypal REST API-协议是否被取消?

来自分类Dev

使用PayPal REST API向成员收费

来自分类Dev

PayPal REST API OAuth响应错误

来自分类Dev

带有通知的PayPal REST API

来自分类Dev

贝宝(Paypal)API:REST还是CLASSIC?

来自分类Dev

使用Paypal REST API创建付款

来自分类Dev

PHP PayPal REST API授权调用

来自分类Dev

PayPal REST API更新计费协议

来自分类Dev

在PayPal REST API中出现500错误

来自分类Dev

使用Paypal Rest API退款给用户

来自分类Dev

Paypal Rest API:如何删除账单计划?