【问题标题】:laravel payment gross amount not match items pricelaravel 付款总额与商品价格不匹配
【发布时间】: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_amountitems_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_amountitems_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}} 第一个循环是访问 2 kg 以计算我的产品重量以及运费和商品价格。而已。现在,如果我在该列中有 2 项,则此循环将变得无用,仅当我有 1 项时才有效。
  • 我想添加一些代码作为答案等等。

标签: php laravel payment-gateway


【解决方案1】:

您的运费取决于产品的重量和数量,因此您需要像这样更改您的代码,

// getting orders items detail
  $item_details = array();
  if(!empty($item->product_data)){ //product_data is my json column
    $pp = 0;
    foreach($item->product_data as $item)
    {
      //calculate items price
      foreach($item['attributes'] as $itemsss)
      {
        $proweight = $itemsss['value'];
      }
      $pp = $pp + $item['quantity'] * $proweight * $shipping + $item['price'];
      //end calculation
        $item_details[] = array(
            'id'        => $item['id'],
            'price'     => $pp,
            'quantity'  => $item['quantity'],
            'name'      => $item['name'],
        );
    }
  }

【讨论】:

  • gross_amount 与 items_price 不匹配
  • @mafortis 一个问题,为什么加上运费后要匹配?
  • 这个网关的工作方式!我也向他们抱怨过这一点,但这是他们制作系统的方式。我告诉他们应付价格永远不会与商品价格匹配,因为会增加运费但没有结果\
  • @mafortis 恭喜,但必须进行良好的测试,然后确保它有效:D :D
  • @mafortis 哈哈我不知道那里发生了什么,你应该为我举办一个派对 :P :D
【解决方案2】:

已解决

我将函数更改为以下代码:

if(!empty($item->product_data)){
          foreach($item->product_data as $item)
          {
            //calculate items price
            foreach($item['attributes'] as $itemsss)
            {
              $proweight = $itemsss['value'];
            }


            $convals = 0;
            foreach($item['_conditions'] as $conditionf)
            {
                $convals += $conditionf['value'];
            }

            $pp = $item['price'];
            $pp1 = $proweight * $shipping;
            $pp2 = $pp + $pp1;
            //end calculation
              $item_details[] = array(
                  'id'        => $item['id'],
                  'price'     => $pp2,
                  'quantity'  => $item['quantity'],
                  'name'      => $item['name'],
              );
          }

并将我的 $item['price'] 从前端保存为全价,并添加了所有条件/优惠券,在这种情况下,我不必在我的支付功能中计算条件价格已保存为我的商品价格。 :)

感谢所有帮助。

【讨论】:

    猜你喜欢
    • 2013-10-19
    • 2014-05-02
    • 2013-10-25
    • 2021-03-05
    • 2021-12-21
    • 2016-04-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多