PayPal php sdk可以设置付款时候的item,以及税款。下面说明下各个值的相加关系
官方文档网址
下面摘抄自官方的示例数据,删减部分与本主题无关字段。
其中 amount 是总价 total=subtotal+tax+shipping
item_list中所有item求和price*quantity=subtotal
关于税款中 amount的tax需要运算,而items中的tax值不参与运算。
{
"id": "PAY-0US81985GW1191216KOY7OXA",
"create_time": "2017-06-30T23:48:44Z",
"update_time": "2017-06-30T23:49:27Z",
"state": "APPROVED",
"intent": "order",
"payer": {
"payment_method": "paypal"
},
"transactions": [
{
"amount": {
"total": "41.15",
"currency": "USD",
"details": {
"subtotal": "30.00",
"tax": "1.15",
"shipping": "10.00"
}
},
"description": "The payment transaction description.",
"item_list": {
"items": [
{
"name": "hat",
"sku": "1",
"price": "3.00",
"tax": "3.00",
"currency": "USD",
"quantity": "5"
},
{
"name": "handbag",
"sku": "product34",
"price": "15.00",
"tax": "3.00",
"currency": "USD",
"quantity": "1"
}
]
}
}
]
}
附录 创建订单伪代码
public function createPayment($subtotal,$tax, $description, $items)
{
// Create new payer and method
$payer = new Payer();
$payer->setPaymentMethod("paypal");
// Set redirect URLs
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl('')
->setCancelUrl('');
//details中设置税款和商品总价以及运费
$detail = new Details();
$detail->setTax($tax)
->setSubtotal($subtotal)
->setShipping(0);
//details中三者相加等于amount中的total
// Set payment amount
$amount = new Amount();
$amount->setCurrency("USD")
->setDetails($detail)
->setTotal($subtotal+$tax);
$itemList = new ItemList();
foreach ($items as $item) {
$item = new Item();
$item->setDescription($item['name']);
$item->setCurrency("USD");
$item->setPrice($item['price']);
//item中的税款不参与运算,这里可以为任一值
$item->setTax($item['price'] * ($item['tip'] / 100) * $item['number']);
$item->setName($item['name']);
$item->setQuantity($item['number']);
$itemList->addItem($item);
}
// Set transaction object
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription($description);
// Create the full payment object
$payment = new Payment();
$payment->setIntent('sale')
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions(array($transaction));
// Create payment with valid API context
try {
$payment->create($this->apiContext);
// Get PayPal redirect URL and redirect the customer
$approvalUrl = $payment->getApprovalLink();
// Redirect the customer to $approvalUrl
} catch (PayPalConnectionException $ex) {
echo $ex->getCode();
echo $ex->getData();
die($ex);
} catch (Exception $ex) {
die($ex);
}
return $approvalUrl;
}
附录 按照这种方式支付后的商家截图
订单详情
其中的Sales Tax即为在detais中设置的税款
网友评论