Browse Source

增加价格计算逻辑

develop
李可松 4 years ago
parent
commit
2e4b4c63f6
  1. 10
      app/Http/Controllers/Api/OrderController.php
  2. 49
      app/Http/Controllers/Api/SharePayController.php

10
app/Http/Controllers/Api/OrderController.php

@ -432,19 +432,19 @@ class OrderController extends Controller
* 计算最终价格(扣除优惠券之后的价格)
* $price:原价;$coupon:优惠券;$num:产品数量;$pay_type:支付方式
* @param float $price
* @param int $num
* @param float $num
* @param int $pay_type
* @param Model $agent_product
* @param AgentProduct $agent_product
* @return float
*/
private function calc($price, $num, $pay_type, $agent_product)
private function calc(float $price, float $num, int $pay_type, AgentProduct $agent_product): float
{
/** 修改需要同步修改sharePay里面的 */
//根据支付方式计算价格
if (in_array($pay_type, [PayType::DEPOSIT_PAY, PayType::EARNEST_PAY, PayType::DOWN_PAYMENT])) {
if ($pay_type == PayType::DEPOSIT_PAY && $agent_product->deposit && $agent_product->deposit_timeout) {
return $agent_product->deposit;
}
if ($pay_type == PayType::EARNEST_PAY && $agent_product->earnest && $agent_product->earnest_timeout) {
} else if ($pay_type == PayType::EARNEST_PAY && $agent_product->earnest && $agent_product->earnest_timeout) {
return $agent_product->earnest;
}
}

49
app/Http/Controllers/Api/SharePayController.php

@ -2,8 +2,10 @@
namespace App\Http\Controllers\Api;
use App\Common\OrderStatus as Status;
use App\Common\PayType;
use App\Http\Controllers\Controller;
use App\Models\Agent;
use App\Models\AgentProduct;
use App\Models\Order;
use App\Models\User;
use EasyWeChat\Factory;
@ -36,20 +38,23 @@ class SharePayController extends Controller
$openid = $user_info['openid'];
$this->agent_id = $user_info['agent_id'];
$order = Order::query()->whereIn('status', [Status::UNPAID, Status::PAY_EARNEST])->find($order_id);
$order = Order::with(['agentProduct'])
->whereRaw('`timeout` >= NOW()')
->whereIn('status', [Status::UNPAID, Status::PAY_EARNEST])
->find($order_id);
if (!$order) {
return $this->error('订单不存在或已支付');
return $this->error('订单已支付或已超时');
}
$order->pay_user_id = $this->user_id;
$order->save();
//如果已经付定金或首付款,则仅支付尾款 TODO 还有定金支付
/*if ($order->status == Status::PAY_EARNEST) {
//如果已经付定金或首付款,则仅支付尾款
if ($order->status == Status::PAY_EARNEST) {
$price = $order->price - $order->paid_money;
} else {
$price = $this->calc($order->price, $order->num, $order->pay_type, $ap);
}*/
$price = $this->calc($order->price, $order->num, $order->pay_type, $order->agent_product);
}
$config = config('wechat.payment.default');
$config = array_merge($config, [
@ -63,7 +68,7 @@ class SharePayController extends Controller
$result = $app->order->unify([
'body' => $order->title,
'out_trade_no' => $order->order_no . '-' . $order->status, //后面加status,主要是为了方便微信支付回调时区分定金(首付款)和尾款支付
'total_fee' => round($order->price * 100), //支付金额单位为分
'total_fee' => round($price * 100), //支付金额单位为分
'notify_url' => route('wxpay_notify', ['agent_id' => $this->agent_id]), // 支付结果通知网址,如果不设置则会使用配置里的默认地址
'trade_type' => 'JSAPI',
'openid' => $openid,
@ -78,10 +83,36 @@ class SharePayController extends Controller
return $result;
}
$jump_appid = Agent::where('id', $order->agent_id)->value('appid');
$jssdk = $app->jssdk;
$goback_appid = Agent::where('id', $order->agent_id)->value('appid');
$payConfig = $jssdk->bridgeConfig($result['prepay_id'], false) +
['id' => $order->id, 'order_no' => $order->order_no, 'jump_appid' => $goback_appid]; // 返回数组
['id' => $order->id, 'order_no' => $order->order_no, 'jump_appid' => $jump_appid]; // 返回数组
return $this->success($payConfig);
}
/**
* 计算最终价格
* @param float $price 订单价格
* @param float $num 购买数量
* @param int $pay_type 支付方式
* @param AgentProduct $agent_product 代理商产品
* @return float
*/
private function calc(float $price, float $num, int $pay_type, AgentProduct $agent_product): float
{
/** 修改需要同步修改Order里面的 */
//根据支付方式计算价格
if (in_array($pay_type, [PayType::DEPOSIT_PAY, PayType::EARNEST_PAY, PayType::DOWN_PAYMENT])) {
if ($pay_type == PayType::DEPOSIT_PAY && $agent_product->deposit && $agent_product->deposit_timeout) {
return $agent_product->deposit;
} else if ($pay_type == PayType::EARNEST_PAY && $agent_product->earnest && $agent_product->earnest_timeout) {
return $agent_product->earnest;
}
}
$total_price = $price * $num;
return round($total_price, 2);
}
}
Loading…
Cancel
Save