0
嗨,我在贝宝的PHP SDK新手,我真的需要帮助了我如何获取多项目产品名称,数量&价格为贝宝PHP REST SDK,多项目设置中的PayPal PHP SDK
我用的jQuery的添加多物品到购物车,下面是我的源代码 月1日,这是我在function.php代码
<div class="product clearfix" style="padding:8px;">
<div class="card-block">
<div class="product-image">
<a href="product.php?id={$row['product_id']}"><img src="../resources/{$product_image}" alt="{$row['product_title']}" class="selected"></a>
<a href="product.php?id={$row['product_id']}"><img src="../resources/{$product_image2}" alt="{$row['product_title']}"></a>
</div>
<div class="product-desc center">
<a href="product.php?id={$row['product_id']}">
<div class="product-title"><h3 style="font-size:15px;">{$row['product_title']}</h3></div>
</a>
<a href="include/ajax/add_cart_message.php?id={$row['product_id']}" id="btn1" class="productItem button button-rounded button-reveal button-medium button-border tright" data-lightbox="ajax" data-name="{$row['product_title']}" data-s="{$row['quantity_diff']}" data-price="{$row['product_price']}" data-id="{$row['product_id']}01" data-itemcode="{$row['item_number']}">${$row['product_price']} <i class="icon-shopping-cart"></i> <span id="variab1">{$row['quantity_diff']}</span></a>
<a href="include/ajax/add_cart_message.php?id={$row['product_id']}" id="btn2" class="productItem button button-rounded button-reveal button-medium button-border tright" data-lightbox="ajax" data-name="{$row['product_title']}" data-s="{$row['quantity_diff2']}" data-price="{$row['product_price2']}" data-id="{$row['product_id']}02" data-itemcode="{$row['item_number']}">${$row['product_price2']} <i class="icon-shopping-cart"></i> <span id="variab2">{$row['quantity_diff2']}</span></a>
<a href="include/ajax/add_cart_message.php?id={$row['product_id']}" id="btn3" class="productItem button button-rounded button-reveal button-medium button-border tright" data-lightbox="ajax" data-name="{$row['product_title']}" data-s="{$row['quantity_diff3']}" data-price="{$row['product_price3']}" data-id="{$row['product_id']}03" data-itemcode="{$row['item_number']}">${$row['product_price3']} <i class="icon-shopping-cart"></i> <span id="variab3">{$row['quantity_diff3']}</span></a>
</div>
</div>
</div>
</div>
点击将数据发送到jQuery的每个按钮收集起来并输出到HTML模式类似下面
<!-- modals -->
<div class="modal fade" id="cart" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header"><button type="button" class="close" data-dismiss="modal">×</button><h4 class="modal-title">Shopping Cart</h4></div>
<div class="modal-body" style="padding:0px;">
<form action="order_process.php" method="post">
<table class="table table-hover">
<thead style="background:#1ABC9C; color:white;">
<tr>
<th>Item Image</th>
<th>Item Name</th>
<th>Cost</th>
<th>Qty</th>
<th style="text-align:right;">Subtotal</th>
<th style="text-align:center;">Remove</th>
</tr>
</thead>
<tbody id="output">
</tbody>
</table>
</div>
<div class="modal-footer"><input type="submit" class="btn btn-primary btn-block" value="Checkout with PayPal"><button type="button" class="btn btn-warning" data-dismiss="modal">Continue Shopping</button></div>
</div>
</div>
</div>
以下是jQuery脚本将数据输出到#output
</script>
<script>
var shopcart =[];
jQuery(document).ready(function($){
outputCart();
$('#output').on('click','.remove-item',function(){
var itemToDelete = $('.remove-item').index(this);
shopcart.splice(itemToDelete,1);
sessionStorage['sca'] = JSON.stringify(shopcart);
outputCart();
})
$('.productItem').click(function (e){
e.preventDefault();
var imgpath = $(this).closest('.product').find('img' + '.selected').attr('src');
if (!imgpath) {
imgpath = 'default.jpg';
}
console.log(imgpath);
var iteminfo = $(this.dataset)[0];
iteminfo.qty = 1;
var itemincart = false;
$.each(shopcart, function (index, value) {
///console.log(index + ' ' + value.id);
if(value.id == iteminfo.id) {
iteminfo.imgpath = imgpath;
value.qty = parseInt(value.qty) + parseInt(iteminfo.qty);
itemincart = true;
}
})
if(!itemincart){
iteminfo.imgpath = imgpath;
shopcart.push(iteminfo);
}
console.log(shopcart);
sessionStorage["sca"] = JSON.stringify(shopcart);
outputCart();
///
})
function outputCart() {
if (sessionStorage["sca"] != null) {
shopcart = JSON.parse(sessionStorage["sca"].toString());
$('#checkoutdiv').show();
}
var holderHTML = '';
var total = 0;
var itemCnt = 0;
$.each(shopcart, function (index, value) {
var stotal = value.qty * value.price;
total += stotal;
itemCnt += parseInt(value.qty);
holderHTML += '<tr><td><a href=product.php?id=' + formatId(value.id) + '><img src="' + value.imgpath + '" width="100px"></a></td><td><input type="hidden" name="item_name_' + a + '" value="' + value.name + ' ' + value.s + '"><a href=product.php?id=' + formatId(value.id) + '>' + value.name + '(' + value.s + ')<br>#' + value.itemcode + '</a></td><td class="center"><input type="hidden" name="amount_' + a + '" value="' + formatMoney(value.price) + '"> ' + formatMoney(value.price) + ' </td><td><input size="5" type="number" class="dynquantity" name="quantity_' + a + '" value="' + value.qty + '" data-id="' + value.id + '"></td><td class="center"> ' + formatMoney(stotal) + '</td><td class="center"><span class="btn btn-danger remove-item">x</span></td></tr>';
})
holderHTML += '<tr><td colspan="4" style="text-align:right;">Total: </td><td style="text-align:right;">' + formatMoney(total) +'</td><td>';
$('#output').html(holderHTML);
$('.total').html(formatMoney(total));
$('.items').html(itemCnt);
}
function formatMoney(m){
return '$' + (m);
}
function formatId(n){
return (n/100).toFixed(0);
}
})
</script>
在order_process.php
,我该如何获取这些DATAS到PayPal PHP SDK?我尽力而为,它根本不工作,请帮助
<?php
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/startpp.php';
if (!isset($_POST['item_name_[]'], $_POST['amount_[]'], $_POST['quantity_[]'])) {
}
$product = $_POST['item_name_[]'];
$price = $_POST['amount_[]'];
$quantity = $_POST['quantity_[]'];
$total = $price * $quantity;
$payer = new Payer();
$payer->setPaymentMethod('paypal');
$item1 = new Item();
$item1->setName($product)
->setCurrency('CAD')
->setQuantity($quantity)
->setPrice($price);
$item2 = new Item();
$item2->setName($product)
->setCurrency('CAD')
->setQuantity($quantity)
->setPrice($price);
$item3 = new Item();
$item3->setName($product)
->setCurrency('CAD')
->setQuantity($quantity)
->setPrice($price);
$item4 = new Item();
$item4->setName($product)
->setCurrency('CAD')
->setQuantity($quantity)
->setPrice($price);
$item5 = new Item();
$item5->setName($product)
->setCurrency('CAD')
->setQuantity($quantity)
->setPrice($price);
$item6 = new Item();
$item6->setName($product)
->setCurrency('CAD')
->setQuantity($quantity)
->setPrice($price);
$item7 = new Item();
$item7->setName($product)
->setCurrency('CAD')
->setQuantity($quantity)
->setPrice($price);
$itemList = new ItemList();
$itemList->setItems([$item1, $item2, $item3, $item4, $item5, $item6, $item7]);
$amount = new Amount();
$amount->setCurrency('CAD')
->setTotal($total)
->setDetail($detail);
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItmeList($itemList)
->setDescription('PayForSomething Payment')
->setInvoiceNumber(uniqid());
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl(SITE_URL . '/pay.php?success=true')
->setCancelUrl(SITE_URL . '/pay.php?success=false');
$payment = new Payment();
$payment->setIntent('sale')
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions([$transaction]);
try {
$payment->create($paypal);
} catch (Exception $e) {
die($e);
}
echo $approvalUrl = $payment->getApprovalLink();
谢谢,我纠正了它,但我的浏览器仍然显示注意:未定义的索引:item_name [],金额[]等.. –
请修改您的问题并添加您的所有错误信息。如果没有他们,很难发现什么是错误的,因为你需要挖掘你所有的代码,而错误信息使得它很清楚。我扩大了我的答案。 – Pharaoh