贝宝:MALFORMED_REQUEST

时间:2018-08-05 05:00:13

标签: php paypal-sandbox paypal-rest-sdk

<?php
require '../vendor/autoload.php';
include '../models/panier.php';
include './utils.php';
$ids=require('./paypal.php');

$panier=new Panier(1,'etat',3);
$listArt=Panier::artPanier($panier);
foreach($listArt as $l){
    echo $l->nom;echo ' ';
    echo (madToEur($l->prix));echo ' ';
}
$apiContext = new \PayPal\Rest\ApiContext(
    new \PayPal\Auth\OAuthTokenCredential(
        $ids['id'],
        $ids['secret']
    )
);
$paypalList=new \PayPal\Api\ItemList();
foreach($listArt as $l){
    $item=new \PayPal\Api\Item();
    $item->setName($l->nom);echo 'item : ';
    echo $item->getName().' ';
    $prix=madToEur($l->prix);
    $item->setPrice($prix);
    echo $item->getPrice().' ';
    $item->setCurrency('EUR');
    $item->setQuantity(1);
    echo $item->getCurrency().' '.$item->getQuantity().' ';
    $paypalList->addItem($item);echo 'added';
}
$details=new \PayPal\Api\Details();
$subtotal=Panier::getTotalPrix($panier);
$subTotalEur=madToEur($subtotal);
echo ' sub '.$subTotalEur;
$details->setSubtotal($subTotalEur);
echo ' '.$details->getSubtotal();
$amount=new \PayPal\Api\Amount();
$amount->setTotal($subTotalEur);
echo ' total amount '.$amount->getTotal();
$amount->setCurrency('EUR');echo $amount->getCurrency();
$amount->setDetails($details);
echo ' amount details '.$amount->getDetails();
$transaction=new \PayPal\Api\Transaction();
$transaction->setItemList($paypalList);echo ' itemlist '.$transaction->getItemList();
$transaction->setAmount($amount);echo ' amount '.$transaction->getAmount();
$transaction->setCustom('demo');echo ' Custom '.$transaction->getCustom();
$transaction->setDescription('desc');echo ' Description '.$transaction->getDescription();
$payment = new \PayPal\Api\Payment();
$payment->setIntent('sale');
$redirectUrls =new \PayPal\Api\RedirectUrls();
$redirectUrls->setReturnUrl('https://example.com/return');
$redirectUrls->setCancelUrl('https://example.com/cancel');
$payment->setRedirectUrls($redirectUrls);
$payment->setPayer((new \PayPal\Api\Payer())->setPaymentMethod('paypal'));
$payment->setTransactions($transaction);


echo $payment->getPayer();
echo $payment->getTransactions();

echo '\n';
echo $payment;









try {
    $payment->create($apiContext);
    echo $payment->getApprovalLink();
} catch (\PayPal\Exception\PayPalConnectionException $e) {
    var_dump(json_decode($e->getData()));
}

?>

{
    "intent": "sale",
    "redirect_urls": {
        "return_url": "https://example.com/return",
        "cancel_url": "https://example.com/cancel"
    },
    "payer": {
        "payment_method": "paypal"
    },
    "transactions": {
        "item_list": {
            "items": [
                {
                    "name": "masqueCh",
                    "price": "1.41",
                    "currency": "EUR",
                    "quantity": 1
                },
                {
                    "name": "masquePeaux",
                         "price": "1.41",
                    "currency": "EUR",
                    "quantity": 1
                },
                {
                    "name": "nomPaque",
                    "price": "1.41",
                    "currency": "EUR",
                    "quantity": 1
                }
            ]
        },
        "amount": {
            "total": "4.23",
            "currency": "EUR",
            "details": {
                "subtotal": "4.23"
            }
        },
        "custom": "demo",
        "description": "desc"
    }
}

我得到了MALFORMED_REQUEST和数据转储。应该将用户带到PayPal(在这种情况下为Sandbox)进行付款处理。我正在使用PayPal PHP SDK(REST)。我还使用了一些方法来获取我的产品的价格,但是您可以从json响应中看到我的所有方法都运行良好,但是steel无法获得批准链接,我已经发布了我的代码以及json我从中得到的:echo $ payment

0 个答案:

没有答案