我正在尝试使用PHP进行“Pay with PayPal帐户”,并且我不断收到响应“传入的JSON请求未映射到API请求”。 我已经检查了我用jsonlint发送的JSON,它是有效的JSON。它似乎与此付款类型的样本中发送的内容相匹配。
require __DIR__ . '/../bootstrap.php';
use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction;
session_start();
// ### Payer
$payer = new Payer();
$payer->setPaymentMethod("paypal");
// ### Itemized information
$item1 = new Item();
$item1->setName($item1Name)
->setCurrency($currency)
->setQuantity($item1Quantity)
->setPrice($item1Price);
$item2 = new Item();
$item2->setName($item2Name)
->setCurrency($currency)
->setQuantity($item2Quantity)
->setPrice($item2Price);
$itemList = new ItemList();
$itemList->setItems(array($item1, $item2));
// ### Additional payment details
$details = new Details();
$details->setShipping($shipping)
->setTax($tax)
->setSubtotal($subtotal);
// ### Amount
$amount = new Amount();
$amount->setCurrency($currency)
->setTotal(number_format(($subtotal + ($shipping + $tax)), 2))
->setDetails($subtotal);
// ### Transaction
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription("Payment description");
// ### Redirect urls
$baseUrl = getBaseUrl();
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl("$baseUrl/ExecutePayment.php?success=true")
->setCancelUrl("$baseUrl/ExecutePayment.php?success=false");
// ### Payment
$payment = new Payment();
$payment->setIntent("sale")
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions(array($transaction));
// ### Create Payment
$payment->create($apiContext);
} catch (PayPal\Exception\PPConnectionException $ex) {
echo "Exception: " . $ex->getMessage() . PHP_EOL;
var_dump($ex->getData());
exit(1);
}
// ### Get redirect url
foreach($payment->getLinks() as $link) {
if($link->getRel() == 'approval_url') {
$redirectUrl = $link->getHref();
break;
}
}
// ### Redirect buyer to PayPal website
$_SESSION['paymentId'] = $payment->getId();
if(isset($redirectUrl)) {
header("Location: $redirectUrl");
exit;
}
PayPal.log包含以下内容:
PayPal\Core\PPHttpConnection: Connecting to https://api.sandbox.paypal.com/v1/oauth2/token
PayPal\Core\PPHttpConnection: Payload grant_type=client_credentials
PayPal\Core\PPHttpConnection: Adding header User-Agent: PayPalSDK/rest-sdk-php 0.6.0 (lang=PHP;v=5.3.3;bit=64;os=********************;machine=x86_64;openssl=**********;curl=7.15.5)
PayPal\Core\PPHttpConnection: Adding header Authorization: Basic ***********************************
PayPal\Core\PPHttpConnection: Adding header Accept: */*
PayPal\Core\PPHttpConnection: Connecting to https://api.sandbox.paypal.com/v1/payments/payment
PayPal\Core\PPHttpConnection: Payload {"intent":"sale","payer":{"payment_method":"paypal"},"redirect_urls":{"return_url":"http:\/\/www.mysite.com\/paypal-test\/sample\/payments\/ExecutePayment.php?success=true","cancel_url":"http:\/\/www.mysite.com\/paypal-test\/sample\/payments\/ExecutePayment.php?success=false"},"transactions":[{"amount":{"currency":"EUR","total":"554.00","details":"550.00"},"item_list":{"items":[{"name":"Item 1 Name","currency":"EUR","quantity":50,"price":"7.00"},{"name":"Item 2 Name","currency":"EUR","quantity":20,"price":"10.00"}]},"description":"Payment description"}]}
PayPal\Core\PPHttpConnection: Adding header Content-Type: application/json
PayPal\Core\PPHttpConnection: Adding header User-Agent: PayPalSDK/rest-sdk-php 0.6.0 (lang=PHP;v=5.3.3;bit=64;os=*******************;machine=x86_64;openssl=************;curl=7.15.5)
PayPal\Core\PPHttpConnection: Adding header Authorization: Bearer gSJ0P0foNQcWg3V76VjvSietNLejlF4-kfSFNkTcyCk
PayPal\Core\PPHttpConnection: Adding header PayPal-Request-Id: 773226502471885139220716063968
非常感谢任何帮助。
提前致谢。
答案 0 :(得分:1)
我已经弄清楚了。我将一个数字传递给Amount-> SetDetails而不是所需的对象。
// ### Amount
$amount = new Amount();
$amount->setCurrency($currency)
->setTotal(number_format(($subtotal + ($shipping + $tax)), 2))
->setDetails($subtotal);
帮助我弄明白的一件事是将PayPal样本(/sample/PayPal.log)的日志与我自己尝试的日志进行比较。
避免灾难!