PayPal API - 错误400 - 传入的JSON请求未映射到API请求

时间:2016-02-27 11:34:45

标签: php paypal

这是start.php:

<?php

require "../vendor/autoload.php";

define('SITE_URL', 'http://Mundwaechter.de');



$paypal = new \PayPal\Rest\ApiContext(
        new \PayPal\Auth\OAuthTokenCredential('HIDDEN','HIDDEN')
      );

$paypal->setConfig(['mode' => 'sandbox',
                 'http.ConnectionTimeOut' => 30,
                 'log.LogEnabled' => true,
                 'log.FileName' => 'logPaypal.txt',
                 'log.LogLevel' => 'FINE',
                 'validation.level' => 'log'
                ]);

这是checkout.php (还有一些其他代码) 我把Mundwächter.de变成了Mundwaechter.de,因为Char'ä'会导致一些错误。     

session_start();


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";

Include "../php/MySQL.php";
Include "../php/Functions.php";


$first_name = $_SESSION['register_first_name'];
$last_name = $_SESSION['register_last_name'];
$email = $_SESSION['register_email'];

$first_name = strtolower($first_name);
$last_name = strtolower($last_name);
$email = strtolower($email);

$first_name = ucfirst($first_name);
$last_name = ucfirst($last_name);
$email = ucfirst($email);

$password = $_SESSION['register_password'];

$password_md5 = md5($register_password);

$code = $_POST['register_code'];


$praxis = getPraxisFromCode($code, $db);



if(hasAccount($email, $db) == true){
    echo "Hat schon einen Account";
} else {
    echo "Hat noch keinen Account";
    deleteCode($code, $db);
    createAccount($first_name, $last_name, $email, $password_md5, $praxis, $db);
}

mysql_close($db);

echo "<br>A";
$payer = new Payer();
$payer->setPaymentMethod('paypal');
echo "<br>B";
$item = new Item();
$item->setName("Mundwaechter")
    ->setCurrency('EUR')
    ->setQuantity('1')
    ->setPrice('15.00');
echo "<br>C";
$itemList = new ItemList();
$itemList->setItems([$item]);
echo "<br>D";
$details = new Details();
$details->setShipping('0.00')
        ->setTax('0.00')
        ->setSubtotal('15.00');
echo "<br>E";
$amount = new Amount();
$amount->setCurrency('EUR')
        ->setTotal('15.00')
        ->setDetails($details);
echo "<br>F";
$transaction = new Transaction();
$transaction->setAmount($amount)
            ->setItemList($itemList)
            ->setDescription('1 Mundwaechter Lizenz fuer eine Person')
            ->setInvoiceNumber(uniqid());
echo "<br>G";
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl("http://Mundwaechter.de/transaction_complete.php")
             ->setCancelUrl("http://Mundwaechter.de/transaction_failure.php");
echo "<br>H";
$payment = new Payment();
$payment->setIntent('sale')
        ->setPayer($payer)
        ->setRedirectUrls($redirectUrls)
        ->setTransactions($transaction);
echo "<br>I";



try{
    $payment->create($paypal);
} catch (Exception $ex) {
            //ResultPrinter::printError("Created Payment Using PayPal. Please visit the URL to Approve.", "Payment", null, $paypal, $ex);
            //var_dump($items);
            echo "<pre>" . $payment;
            echo $ex->getData();
            echo $product->shipping;
            echo $amount->getTotal;
            exit(1);
        }



/*
catch(PayPal\Exception\PayPalConnectionException $err){
    echo($err->getData());

}
*/
echo "<br>J";
$approvalUrl = $payment->getApprovalLink();
echo "<br>K";
header("Location: {".$approvalUrl."}");

echo "<br><hr><br>WORKING!";

?>

让我们来看看错误:

{"name":"MALFORMED_REQUEST","message":"Incoming JSON request does not map to API request","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST","debug_id":"602bfb6c4c828"}

它说JSON不正确,所以这里是JSON输出代码:

{
    "intent": "sale",
    "payer": {
        "payment_method": "paypal"
    },
    "redirect_urls": {
        "return_url": "http://Mundwaechter.de/transaction_complete.php",
        "cancel_url": "http://Mundwaechter.de/transaction_failure.php"
    },
    "transactions": {
        "amount": {
            "currency": "EUR",
            "total": "15.00",
            "details": {
                "shipping": "0.00",
                "tax": "0.00",
                "subtotal": "15.00"
            }
        },
        "item_list": {
            "items": [
                {
                    "name": "Mundwaechter",
                    "currency": "EUR",
                    "quantity": "1",
                    "price": "15.00"
                }
            ]
        },
        "description": "1 Mundwaechter Lizenz fuer eine Person",
        "invoice_number": "56d18699a1eb3"
    }
}

就这样,我总是得到错误。

谢谢Tech!

0 个答案:

没有答案