所以我正在搞砸paypal沙盒,但由于某种原因,它不想让我回购一个数组。相反,它只返回null。我可以使用curl通过api检索令牌,但处理测试卡仍然返回null。以前有人有这个问题吗?
<?php
use PayPal\Api\Payer;
use PayPal\Api\Details;
use PayPal\Api\Amount;
use PayPal\Api\Transaction;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
require 'src/start.php';
$payer = new Payer();
$details = new Details();
$amount = new Amount();
$transaction = new Transaction();
$payment = new Payment();
$redirectUrls = new RedirectUrls();
//PAYER
$payer->setPaymentMethod('paypal');
//Details
//acutal prices(20 dollars is a test)
//Amount
$amount->setCurrency('GBP')
->setTotal('22.00')
->setDetails($details);
$details->setShipping('2.00')
->setTax('0.00')
->setSubtotal('20.00');
//Transaction
$transaction->setAmount($amount);
// ->setDescription('Membership');
//Payment
$payment->setIntent('sale')
->setPayer($payer)
->setTransactions([$transaction]);
//RedirectUrls
$redirectUrls->setReturnUrl('http://localhost:8000/paypaletc/pay.php?approved=true');
// ->setCancelUrl('http://localhost:8000/paypal/pay.php?approved=false');
$payment->setRedirectUrls($RedirectUrls);
try{
$payment->create($api);
} catch(PayPal\Exception\PayPalConnectionException $e){
echo $e->getMessage();
// header('Location: paypaletc/error.php');
}
var_dump($payment->getLinks());
我的start.php:
<?php
use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
// session_start();
// $_SESSION['user_id'] = 1;
// $db = newPDO('mysql:host=localhost;dbname-site', 'homestead', 'secret');
// $user = $db->prepare("
// SELECT * FROM users
// WHERE id = :user_id
// ");
// $user-execute(['user_id' => $_SESSION['user_id']]);
// $user = $user->fetchObject();
require __DIR__ . '/../vendor/autoload.php';
// API CREDS
$api = new ApiContext(
new OAuthTokenCredential(
"AfhmDCIi9Gc6cTuMv8Y4wVENTycXAkLfCWJ37v2uvQ0FJO7nqoL_JssmUaYAFBMtQD85xOfk1mHMtMb_",
"EABb--WImsBgy3Ck_MmsVD-uVa--plWBwxqzaLFt33_DDRz7cV5GkNQbfB_ZH-1z1a9NU4ictM98erdJ"
)
);
$api->setConfig([
'mode' => 'sandbox',
// 'http.ConnectionTimeOut'=> 30,
// 'log.LogEnabled' => false,
// 'log.FileName'=>'',
// 'log.LogLevel'=>'FINE',
// 'validation.level'=>'log'
]);
它返回的内容:
访问https://api.sandbox.paypal.com/v1/payments/payment.NULL时获得了Http响应代码400
答案 0 :(得分:0)
罪魁祸首是$ redirectUrls。在你的setRedirectUrls($ RedirectUrls)中,查看$ RedirectUrls中的大写字母R,这就是造成异常的原因。
$redirectUrls->setReturnUrl('http://localhost:8000/paypaletc/pay.php?approved=true')
->setCancelUrl('http://localhost:8000/paypal/pay.php?approved=false');
$payment->setRedirectUrls($redirectUrls);
这应该修复你的代码。另外,请尝试使用$e->getMessage();
$e->getData();
以下是您的最终代码的样子:
<?php
use PayPal\Api\Payer;
use PayPal\Api\Details;
use PayPal\Api\Amount;
use PayPal\Api\Transaction;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
require 'src/start.php';
$payer = new Payer();
$details = new Details();
$amount = new Amount();
$transaction = new Transaction();
$payment = new Payment();
$redirectUrls = new RedirectUrls();
//PAYER
$payer->setPaymentMethod('paypal');
//Details
//acutal prices(20 dollars is a test)
//Amount
$amount->setCurrency('GBP')
->setTotal('22.00')
->setDetails($details);
$details->setShipping('2.00')
->setTax('0.00')
->setSubtotal('20.00');
//Transaction
$transaction->setAmount($amount);
// ->setDescription('Membership');
//Payment
$payment->setIntent('sale')
->setPayer($payer)
->setTransactions([$transaction]);
//RedirectUrls
$redirectUrls->setReturnUrl('http://localhost:8000/paypaletc/pay.php?approved=true')
->setCancelUrl('http://localhost:8000/paypal/pay.php?approved=false');
$payment->setRedirectUrls($redirectUrls);
try{
$payment->create($api);
} catch(PayPal\Exception\PayPalConnectionException $e){
echo $e->getData();
// header('Location: paypaletc/error.php');
}
var_dump($payment->getLinks());