我正在尝试通过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开发人员页面上的手动说明和在web上找到的一些教程编写代码。
我的密钥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表单,但我测试了这些值,它们是通过提交按钮发布的。
我正在尝试连接到沙盒(不是live)。
这是漫长的一天,因为上午10点试图解决这个问题没有运气。
发布于 2018-02-08 05:07:34
刚刚解决了这个问题...这是一件小事。->setTransactions( $transaction );$transaction必须在....
https://stackoverflow.com/questions/48672754
复制相似问题