【发布时间】:2015-09-10 23:20:47
【问题描述】:
我一直试图通过this interactive tutorial page 的示例找出 PHP SDK。我稍微修改了代码以适应我自己的应用程序的目的,并包含我自己的 clientId 和 clientSecret,但还不足以构成重大违规。您将在下面的注释代码中看到我所做的更改。
我的问题是 payment->create() 方法不接受我的凭据。它抛出 400 代码异常,并显示消息“无效的凭据”。我做错了什么?
代码:(输出如下)
<!DOCTYPE html>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
//I wrote the PayPalAutoloader and stored it in the PayPal folder.
//the autoloader uses the namespace data to parse the folder structure
//and loads the corresponding object or throws an Exception if the file
//is not found.
require_once 'PayPal-PHP-SDK/PayPalAutoloader.php';
//added my own INI file, where I can store API related values.
$ini = \parse_ini_file('libsec.ini', true);
$clientId = $ini['PayPal']['clientId'];
$clientSecret = $ini['PayPal']['clientSecret'];
//Step 1 of 5 from website
$sdkConfig = [ "mode" => "sandbox"];
$oauthCredential = new \PayPal\Auth\OAuthTokenCredential($clientId, $clientSecret, $sdkConfig);
$apiContext = new \PayPal\Rest\ApiContext($oauthCredential);
//end Step 1 of 5
//observation, apiContext is never actually used in Step 1 and is redefined in Step 2
//I found that to send the request I had to call getAccessToken (not mentioned in interactive tutorial)
$accessToken = $oauthCredential->getAccessToken($sdkConfig);
//peaking at the accessToken value
var_dump( $accessToken );
//Begin Step 2 of 5 from website
//reuse sdkConfig from Step 1, it is unchanged
$credentials = "Bearer {$accessToken}";
$apiContext = new \PayPal\Rest\ApiContext($credentials, 'Request' . time() );
$apiContext->setConfig($sdkConfig);
$payer = new \PayPal\Api\Payer();
$payer->setPaymentMethod("paypal");
$amount = new \PayPal\Api\Amount();
$amount->setCurrency("USD");
$amount->setTotal("483.00");
$transaction = new \PayPal\Api\Transaction();
$transaction->setDescription("Annual Dues");
$transaction->setAmount($amount);
//removed as $baseURL is not actually used by any of the code that follows.
//the results of the code are the same after commenting out the baseURL variable.
//$baseUrl = getBaseUrl();
$rootURL = $ini['General']['rootURL'];
$successfulTransactionURL = $rootURL . $ini['PayPal']['success_bill_pay'];
$cancelledTransactionURL = $rootURL . $ini['PayPal']['cancel_bill_pay'];
$redirectUrls = new \PayPal\Api\RedirectUrls();
$redirectUrls->setReturnUrl($successfulTransactionURL);
$redirectUrls->setCancelUrl($cancelledTransactionURL);
$payment = new \PayPal\Api\Payment();
$payment->setIntent("sale");
$payment->setPayer($payer);
$payment->setRedirectUrls($redirectUrls);
$payment->setTransactions($transaction);
//a var_dump is added in the create method to look at the payment formatted as JSON prior to the
//REST API call.
$result = $payment->create($apiContext);
//peaking at result
var_dump( $result );
?>
</body>
</html>
上述输出是来自同一程序运行的 3 个单独的输出实例。第一个字符串是 $accessToken 的 var_dump() ,用于验证是否正在检索令牌。每次运行都会成功检索到此令牌,并且值会更改。
第二个输出是在 Payment::create() 方法中形成的 $json 对象的 var_dump。此 json 对象通过 POST 方法传递给 API 系统,以对“/v1/payments/payment”的硬编码地址进行 REST 调用。
输出的第三部分是异常,它已经传播到屏幕并阻止了 Payment::create() 方法的完成。
【问题讨论】: