我正在尝试通过 rest-api-sdk-php 1.13.0 将网站连接到 paypal,并且在点击结帐按钮进行付款时总是出现此错误:
400{"name":"MALFORMED_REQUEST","message":"传入的 JSON 请求未映射到 API ... 访问 https://api.sandbox.paypal.com/v1/payments/payment
由于我是 paypal 集成的新手,我根据 paypal 开发人员页面的手动说明和网络上的一些教程编写了我的代码。
我的 key PHP 代码是:
require 'vendor/autoload.php';
define('SITE_URL', 'http://localhost:8888/segundo_mercado');
$paypal = new \PayPal\Rest\ApiContext(
new \PayPal\Auth\OAuthTokenCredential(
'ZDSFE7B...',
'Xcfr6bG...'
)
);
还有支付操作:
use PayPal\Api\Payer;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Details;
use PayPal\Api\Amount;
use PayPal\Api\Transaction;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Payment;
require 'app/start.php';
if(!isset($_POST["price"])){
die();
}
$product = 'Produto';
$price = $_POST['price'];
$shipping = 4.00;
$total = $price + $shipping;
$payer = new Payer();
$payer->setPaymentMethod('paypal');
$item = new Item();
$item->setName($product)
->setCurrency('USD')
->setQuantity(1)
->setPrice($price);
$itemList = new ItemList();
$itemList->setItems([$item]);
$details = new Details();
$details->setShipping($shipping)
->setSubtotal($price);
$amount = new Amount();
$amount->setCurrency('USD')
->setTotal($price)
->setDetails($details);
$transaction = new Transaction();
$transaction->SetAmount($amount)
->setItemList($itemList)
->setDescription('Segundo Mercado pagamento')
->setInvoiceNumber(uniqid());
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl(SITE_URL . '/success.php?success=true')
->setCancelUrl(SITE_URL . '/success.php?success=false');
$payment = new Payment();
$payment->setIntent('sale')
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions($transaction);
try{
$payment->create($paypal);
} catch (PayPal\Exception\PayPalConnectionException $ex) {
echo $ex->getCode(); // Prints the Error Code
echo $ex->getData(); // Prints the detailed error message
die($ex);
} catch (Exception $ex) {
die($ex);
}
$approvalUrl = $payment->getApprovalLink();
我认为问题在于传递的价格或运费值是否来自 HTML 表单,但我测试了这些值并且它们是通过提交按钮发布的。
我正在尝试连接到 SANDBOX(非实时)。
这是漫长的一天,从上午 10 点开始尝试解决这个问题,但运气不佳。
最佳答案
刚解决了... 这是一件小事。 ->setTransactions([$transaction]); $transaction 必须在 [...] 内。
关于php - Paypal 错误400,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48672754/