【发布时间】:2018-08-05 11:19:27
【问题描述】:
我有付款方式(代码如下)
public function orderspayonline(Request $request, $id){
error_log('masuk ke snap token dri ajax');
$midtrans = new Midtrans;
$item = Order::findOrFail($id);
$buyer = $item->buyer_name;
$address = $item->address;
$city = $item->city;
$postalcode = $item->postalcode;
$province = $item->province;
$email = $item->buyer_email;
$phone = $item->phone;
$carttotal = $item->price;
$shipping = $item->shipping_cost;
$transaction_details = array(
'order_id' => uniqid(),
'gross_amount' => $carttotal,
// gross amount (total price buyer has to pay) is saved in order raw included shipping method like: (total price + weight * shipping cost).
);
// getting orders items detail
$item_details = array();
if(!empty($item->product_data)){ //product_data is my json column
foreach($item->product_data as $item)
{
//calculate items price
foreach($item['attributes'] as $itemsss)
{
$proweight = $itemsss['value'];
}
$pp = $item['quantity'] * $proweight * $shipping + $item['price'];
//end calculation
$item_details[] = array(
'id' => $item['id'],
'price' => $pp,
'quantity' => $item['quantity'],
'name' => $item['name'],
);
}
}else{
$item_details[] = array(
'id' => $item->id,
'price' => $item->price,
'quantity' => $item->quantity,
'name' => $item->product,
);
}
// to be continued....
现在,正如您在我的//getting orders items detail 部分中看到的那样,我有 2 种循环类型,first one 将循环使用基于 json 的详细信息的订单 second one 将循环由管理员为用户手动创建的订单,其中数据不是 json。
之前我添加了//calculate items price 以使用商品价格计算订单重量和运费,在这种情况下,我的总金额将与我的商品价格相同,并且一切正常,没有任何错误。
问题
当我的基于 json 的订单中有多个项目时会出现问题,然后我的自定义循环将不再工作,并且我收到 gross_amount 与 items_price 不同的错误。
知道我应该在哪里改变吗?
更新
我的多商品订单json代码
{"4":{"id":4,"name":"product four","price":1500000,"quantity":2,"attributes":{"attr":{"name":"weight","value":"45"}},"conditions":[],"_conditions":[{"name":"Black","value":"10000"},{"name":"12 inch","value":"25000"}]},"1":{"id":1,"name":"product one","price":10000000,"quantity":"1","attributes":{"attr":{"name":"weight","value":"1"}},"conditions":[{}],"_conditions":[{"name":"Black","value":"10000"}]}}
我的 json 订单代码与一件商品
{"4":{"id":4,"name":"product four","price":1535000,"quantity":"1","attributes":{"attr":{"name":"weight","value":"45"}},"conditions":[{},{}],"_conditions":[{"name":"Black","value":"10000"},{"name":"12 inch","value":"25000"}]}}
更新 2
gross amount
我的gross_amount 来自名为price 的列的值是:
shipping cost * cart total weight + cart total amount
购物车总金额可能包括添加到商品的额外选项,或从商品价格中降低的优惠券。
我使用的技巧是:
将完整的价格(上调)作为我的gross amount,以便从客户那里获得运费,但问题是我的网关公司坚持我的gross_amount 和items_price 完全相同不要找我降神会,因为我还得付运费。
所以诀窍是操纵物品价格以提高它们直到它们的总和富有我的gross_amount,我通过添加:
$pp = $item['quantity'] * $proweight * $shipping + $item['price'];
现在我意识到我没有包含那些额外的选项/优惠券代码 价值观
you can find them in _conditions in my sample codes above 在我的$pp 中,这就是为什么商品价格总和永远不会超过gross_amount。
知道如何添加它们吗?
【问题讨论】:
-
问题似乎不清楚,请您解释一下。
-
@SagarGautam 问题在于 json 订单有超过 1 个项目,例如订单
5有 3 个项目。那么我无法计算该订单中每件商品的总价。 -
我认为你的内部 foreach 循环有问题,你什么也没做
-
@SagarGautam 好的,我解释一下。我的 json 订单类似于
{id:2,name:product two,price:1000,attributes:{name:weight,value:2}}第一个循环是访问2kg 以计算我的产品重量以及运费和商品价格。而已。现在,如果我在该列中有 2 项,则此循环将变得无用,仅当我有 1 项时才有效。 -
我想添加一些代码作为答案等等。
标签: php laravel payment-gateway