我已经编辑了我的代码以便正确计算,但是当我尝试创建付款时,我收到格式错误的请求错误。如果有人能够提供一些有关我可能出错的信息,我会非常感激。因为我无法弄清楚为什么该请求被视为无效。
我的代码:
public function store()
{
//require('vendor/autoload.php');
$client_ID = "edited_out";
$client_Secret = "edited_out";
$apiContext = new \PayPal\Rest\ApiContext(
new \PayPal\Auth\OAuthTokenCredential(
$client_ID,
$client_Secret
)
);
// ### Payer
// A resource representing a Payer that funds a payment
// For paypal account payments, set payment method
// to 'paypal'.
$payer = new Payer();
$payer->setPaymentMethod("paypal");
// ### Itemized information
// (Optional) Lets you specify item wise
// information
$total_shipping=0;
foreach(Cart::content() as $row)
{
foreach(Inventory::where('id', '=', $row->id)->get() as $product)
{
foreach(Inventory::where( 'id', '=', $row->id)->get() as $product)
{
$total_shipping += $product->shipping;
//echo $total_shipping; } echo $total_shipping;
}
}
}
$i = 0;
$myshipping = 0;
foreach(Cart::content() as $row)
{
foreach(Inventory::where('id', '=', $row->id)->get() as $product)
{
$myshipping += $product->shipping;
$items[$i] = new Item();
$items[$i]->setQuantity($row->qty)
->setName($row->name)
->setPrice($row->price)
->setCurrency('USD');
$details[$i] = new Details();
$details[$i]->setSubtotal($row->price * $row->qty)
->setTax(($row->price * $row->qty) * 0.07)
->setShipping($product->shipping);
$i++;
}
}
$itemList = new ItemList();
$itemList->setItems($items);
// ### Additional payment details
// Use this optional field to set additional
// payment information such as tax, shipping
// charges etc.
// ### Amount
// Lets you specify a payment amount.
// You can also specify additional details
// such as shipping, tax.
$amount = new Amount();
$amount->setCurrency("USD")
->setDetails($details)
->setTotal(Cart::total() + (Cart::total() * 0.07) + $total_shipping);
// ### Transaction
// A transaction defines the contract of a
// payment - what is the payment for and who
// is fulfilling it.
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription("Payment description")
->setInvoiceNumber(uniqid());
// ### Redirect urls
// Set the urls that the buyer must be redirected to after
// payment approval/ cancellation.
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl("edited_out")
->setCancelUrl("edited_out");
// ### Payment
// A Payment Resource; create one using
// the above types and intent set to 'sale'
$payment = new Payment();
$payment->setIntent("sale")
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions(array($transaction));
// For Sample Purposes Only.
$request = clone $payment;
// ### Create Payment
// Create a payment by calling the 'create' method
// passing it a valid apiContext.
// (See bootstrap.php for more on `ApiContext`)
// The return object contains the state and the
// url to which the buyer must be redirected to
// for payment approval
try {
$payment->create($apiContext);
} catch (Exception $ex) {
//ResultPrinter::printError("Created Payment Using PayPal. Please visit the URL to Approve.", "Payment", null, $request, $ex);
//var_dump($items);
echo "<pre>" . $payment;
echo $ex->getData();
echo $product->shipping;
echo $amount->getTotal;
exit(1);
}
$approvalUrl = $payment->getApprovalLink();
return Redirect::to($approvalUrl);
}
回复/请求:
{
"intent": "sale",
"payer": {
"payment_method": "paypal"
},
"redirect_urls": {
"return_url": "http://aidevserver.co/projects/casels/public/orders",
"cancel_url": "http://aidevserver.co/projects/casels/public/checkout/cancel"
},
"transactions": [
{
"amount": {
"currency": "USD",
"details": [
{
"subtotal": "9.98",
"tax": "0.70",
"shipping": "7.60"
}
],
"total": "18.28"
},
"item_list": {
"items": [
{
"quantity": 2,
"name": "This item is a test for shipping calculation",
"price": "4.99",
"currency": "USD"
}
]
},
"description": "Payment description",
"invoice_number": "5543c8511631b"
}
]
}{"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":"3cb07a91c7227"}