贝宝 REST API 实现
Posted
技术标签:
【中文标题】贝宝 REST API 实现【英文标题】:Paypal REST Api implmentation 【发布时间】:2013-10-17 06:19:21 【问题描述】:我是 curl 的新手,所以我使用的是 Paypal Developer 博客上的代码,我很难让它为我工作。这是我正在使用的代码
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 类中定义了函数吗?我已经阅读了贝宝上的文档,但我无法理解我做错了什么。任何帮助都会很棒。
【问题讨论】:
你不能打电话给process_payment()
。您必须使用 paypal
类的实例或将函数设为静态,以便可以从类外部调用。
好的,我现在弄清楚了那部分我从响应中收到一个错误,说“传入的 JSON 请求未映射到 API 请求”有没有办法可以检查或者我应该发布我的代码在这?我用 $paypal = new PayPal(); $result = $paypal->process_payment($request);
【参考方案1】:
函数 process_payment() 是 paypal 类的一部分。要调用它,您必须采用此处列出的方式之一:https://***.com/a/4216347/1715048
【讨论】:
谢谢,我知道了以上是关于贝宝 REST API 实现的主要内容,如果未能解决你的问题,请参考以下文章