orderModel = new Order(); $this->userWalletModel = new Wallet(); $this->userBillModel = new Bill(); $this->thirdPayService = new ThirdPayService(); $this->thirdPayLogModel = new ThirdPayLog(); } public function payOrderSuccess($out_trade_no) { $redLock = RedLock::of(); $order = $this->orderModel->where([ "no" => $out_trade_no, "status" => \E_ORDER_STATUS::Default ])->find(); if (!$order) return $this->fail("订单不存在!"); $lock = $redLock->lock(Order::OKey($order['id'])); if (!is_array($lock)) return $this->fail("资源竞争,稍后再试!"); $massager = (new \app\api\model\massager\Massager())->findById($order["massager_id"]); if ($order["massager_id"] > 0) { if (!$massager) return $this->fail("助教不存在!"); } $membership_no = "RBM" . time() . rand(10000, 99999); if ($order["membership_config_id"] > 0 && $order["membership_amount"] > 0) { $res = $this->thirdPayLogModel->save([ "no" => $membership_no, "user_id" => $order["user_id"], "platform" => $order["pay_platform"], "type" => \E_USER_BILL_CHANGE_TYPE::MemberCharge[0], "reason" => \E_USER_BILL_CHANGE_TYPE::MemberCharge[1], "value" => null, "membership_config_id" => $order["membership_config_id"], "amount" => $order["membership_amount"], "status" => \E_BASE_STATUS::Default, "relation_no" => $out_trade_no, "createtime" => time(), "updatetime" => time() ]); if (!$res) { return $this->fail("会员订单保存异常"); } } $wallet = $this->userWalletModel->getUserWallet($order->user_id); Db::startTrans(); try { $this->orderModel->update([ "status" => \E_ORDER_STATUS::Purchase, "pay_time" => time(), "updatetime" => time(), ], ["id" => $order->id]); if ($order["membership_config_id"] > 0 && $order["membership_amount"] > 0) { $r = $this->rechargeMembershipSuccess($membership_no); if (0 === $r->code()) { throw new Exception($r->msg()); } } $bills = [ [ "user_id" => $order->user_id, "currency_type" => \E_USER_BILL_CURRENCY_TYPE::Money, "change_type" => \E_USER_BILL_CHANGE_TYPE::WxOrderConsumption[0], "change" => -($order["total_real_amount"] - $order["balance_deduction"]), "before" => 0, "after" => 0, "reason" => \E_USER_BILL_CHANGE_TYPE::WxOrderConsumption[1], "money" => 0, "give_money" => 0, "relation_no" => $order->no, "createtime" => time() ], [ "user_id" => $order->user_id, "currency_type" => \E_USER_BILL_CURRENCY_TYPE::Score, "change_type" => \E_USER_BILL_CHANGE_TYPE::ScoreOrderGive[0], "change" => $order["total_real_amount"], "before" => $wallet["score"], "after" => $wallet["score"] + $order["total_real_amount"], "reason" => \E_USER_BILL_CHANGE_TYPE::ScoreOrderGive[1], "money" => 0, "give_money" => 0, "relation_no" => $order->no, "createtime" => time() ] ]; // 余额抵扣 if ($order["balance_deduction"] > 0) { $this->userWalletModel->where("id", $wallet["id"])->setDec("money", $order["balance_deduction"]); $bills[] = [ "user_id" => $order->user_id, "currency_type" => \E_USER_BILL_CURRENCY_TYPE::Money, "change_type" => \E_USER_BILL_CHANGE_TYPE::BalanceDeduction[0], "change" => -$order["balance_deduction"], "before" => $wallet["money"], "after" => $wallet["money"] - $order["balance_deduction"], "reason" => \E_USER_BILL_CHANGE_TYPE::BalanceDeduction[1], "money" => -$order["balance_deduction"], "give_money" => 0, "relation_no" => $order->no, "createtime" => time() ]; } $this->userBillModel->saveAll($bills); $progress = []; $types = $order["store_id"] > 0 ? [ \E_ORDER_PROGRESS_TYPE::Start, \E_ORDER_PROGRESS_TYPE::Over, ] : ALL_ORDER_PROGRESS_TYPE; if ($order["reorder"] == 1) { $types = [ \E_ORDER_PROGRESS_TYPE::Take, \E_ORDER_PROGRESS_TYPE::Over, ]; } foreach ($types as $key => $value) { array_push($progress, [ "order_id" => $order->id, "order_no" => $order->no, "type" => $value, "clock_in" => 0, "index" => $key + 1, "clock_in_time" => null, "lng" => null, "lat" => null, "card_image" => null, "createtime" => time(), "updatetime" => time() ]); } if ($massager && $massager["mobile"]) { $r = TencentCloudService::tencent_cloud_vms_send($massager["mobile"], "1478969", [$order["no"]]); Log::custom_log("Vms", [$r->code(), $r->msg(), $r->data()], "Vms"); } if ($order["voucher_id"] > 0) { (new Voucher())->update([ "status" => "use" ], ["id" => $order["voucher_id"]]); } (new Progress())->insertAll($progress); $this->userWalletModel->where("id", $wallet["id"])->setInc("score", $order["total_real_amount"]); if ($order["store_id"] > 0) { StoreService::inform($order["no"], $order["store_id"], $order["city_code"], $order["service_start_date"]); } if ($massager) { Chat::init(1) ->massager([$massager["id"]]) ->send("您有新订单,请及时处理"); } Db::commit(); return $this->ok(true); } catch (Exception $e) { Db::rollback(); return $this->fail($e->getMessage()); } finally { UserService::updateGrouping($order["user_id"]); $redLock->unlock($lock); } } public function rechargeBalanceSuccess($no) { $thirdLock = new RedLock(); $tLock = $thirdLock->lock(ThirdPayLog::TPKey($no)); if (!is_array($tLock)) return $this->fail("请稍后再试!"); $pay_log = $this->thirdPayLogModel->getByNo($no); if (!$pay_log) return $this->fail("支付记录不存在!"); $value = json_decode($pay_log["value"], true); if (!is_array($value) && !isset($value["user_id"])) { return $this->fail("自定义格式错误!"); } $wallet = $this->userWalletModel->getUserWallet($value["user_id"]); Db::startTrans(); try { $change_type = \E_ORDER_PAY_TYPE::ALi === $pay_log->platform ? \E_USER_BILL_CHANGE_TYPE::BalanceAliRecharge : \E_USER_BILL_CHANGE_TYPE::BalanceWxRecharge; $bill = [ "user_id" => $value["user_id"], "currency_type" => \E_USER_BILL_CURRENCY_TYPE::Money, "change_type" => $change_type[0], "change" => $value["amount"] + $value["give"], "before" => $wallet->money + $wallet->give_money, "after" => ($wallet->money + $wallet->give_money) + $value["amount"] + $value["give"], "reason" => $change_type[1], "money" => $value["amount"], "give_money" => $value["give"], "relation_no" => $pay_log->no, "createtime" => time() ]; $this->thirdPayLogModel->update([ "status" => \E_BASE_STATUS::Normal ], ["id" => $pay_log->id]); $this->userWalletModel->where("id", $wallet->id) ->inc("money", $value["amount"]) ->inc("give_money", $value["give"]) ->update(["updatetime" => time()]); $this->userBillModel->save($bill); Db::commit(); return $this->ok(true); } catch (Exception $e) { Db::rollback(); return $this->fail($e->getMessage()); } finally { $thirdLock->unlock($tLock); } } public function rechargeMembershipSuccess($no) { $thirdLock = new RedLock(); $tLock = $thirdLock->lock(ThirdPayLog::TPKey($no)); if (!is_array($tLock)) return $this->fail("请稍后再试!"); $pay_log = $this->thirdPayLogModel->getByNo($no); if (!$pay_log) return $this->fail("支付记录不存在!"); Db::startTrans(); try { $bill = [ "user_id" => $pay_log["user_id"], "currency_type" => \E_USER_BILL_CURRENCY_TYPE::Money, "change_type" => \E_USER_BILL_CHANGE_TYPE::MemberCharge[0], "change" => -$pay_log["amount"], "before" => 0, "after" => 0, "reason" => \E_USER_BILL_CHANGE_TYPE::MemberCharge[1], "money" => 0, "give_money" => 0, "relation_no" => $pay_log->no, "createtime" => time() ]; $this->thirdPayLogModel->update([ "status" => \E_BASE_STATUS::Normal ], ["id" => $pay_log->id]); $user = (new User())->get($pay_log["user_id"]); if (is_null($user)) throw new Exception("用户不存在"); $config = (new Config())->get($pay_log["membership_config_id"]); if (is_null($config)) throw new Exception("会员卡不存在"); $now_time = time(); if ($user["vip_invalid_time"] == null || $user["vip_invalid_time"] < $now_time) {//新用户没有充值过会员 $vip_invalid_time = ($config["days"] * 24 * 60 * 60) + $now_time; } else { $vip_invalid_time = ($config["days"] * 24 * 60 * 60) + $user["vip_invalid_time"]; // 用户续约 } (new Vip())->save([ "user_id" => $user["id"], "starttime" => $now_time, "endtime" => $vip_invalid_time, "createtime" => time(), "updatetime" => time(), ]); (new User())->update([ "vip_invalid_time" => $vip_invalid_time ], ["id" => $user["id"]]); $this->userBillModel->save($bill); Db::commit(); return $this->ok(true); } catch (Exception $e) { Db::rollback(); return $this->fail($e->getMessage()); } finally { $thirdLock->unlock($tLock); } } public function refundSuccess($out_trade_no) { $redLock = RedLock::of(); $order = $this->orderModel->where([ "no" => $out_trade_no, ])->where("status", "IN", [ \E_ORDER_STATUS::Purchase, \E_ORDER_STATUS::Proceed, \E_ORDER_STATUS::WaitFeedback ])->find(); if (!$order) return $this->fail("订单不存在!"); $lock = $redLock->lock(Order::OKey($order['id'])); if (!is_array($lock)) return $this->fail("资源竞争,稍后再试!"); $trip_amount = $order["trip_amount"]; $is_user_refund_trip = $order["is_refund_trip"]; if ($is_user_refund_trip == 1) $trip_amount = 0; Db::startTrans(); try { $save_bill = [ "user_id" => $order["user_id"], "currency_type" => \E_USER_BILL_CURRENCY_TYPE::Money, "change_type" => \E_USER_BILL_CHANGE_TYPE::WxOrderRefund[0], "change" => $order["total_real_amount"] - $trip_amount - $order["balance_deduction"], "before" => 0, "after" => 0, "reason" => \E_USER_BILL_CHANGE_TYPE::WxOrderRefund[1], "money" => 0, "give_money" => 0, "relation_no" => $order["no"], "createtime" => time() ]; // 出行费到账助教账户 if ($order["massager_id"] > 0 && $trip_amount > 0) { $mWalletModel = new \app\api\model\massager\Wallet(); // 出行费到账助教账户 $mWallet = $mWalletModel->getWallet($order["massager_id"]); $mWalletModel->where("id", $mWallet->id)->setInc("profit_amount", $trip_amount); (new \app\api\model\massager\Bill())->insertAll([ [ "massager_id" => $order["massager_id"], "currency_type" => \E_USER_BILL_CURRENCY_TYPE::Money, "change_type" => \E_M_BILL_CHANGE_TYPE::Travel, "change" => $trip_amount, "before" => fixed2Float($mWallet->profit_amount), "after" => fixed2Float($mWallet->profit_amount + $trip_amount), "reason" => "出行费用", "relation_no" => $order->no, "createtime" => time() ] ]); } $auto_count = \db("cancel_order_log")->where("order_no", $order["no"])->where("createtime", "DESC")->find(); $order_status = \E_ORDER_STATUS::Cancel; if ($auto_count) { if ($auto_count["order_status_enum"] == \E_ORDER_STATUS::AdminCancel) { $order_status = \E_ORDER_STATUS::Finish; // $res = (new ProfitService())->profit($order, User::where("id", $order["user_id"])->find(), true); // if (!$res->code()) // throw new Exception($res->msg()); } else { $order_status = $auto_count["order_status_enum"]; } } $this->orderModel->update([ "status" => $order_status, "cancel_time" => time(), "updatetime" => time() ], ["id" => $order->id]); $wallet = $this->userWalletModel->getUserWallet($order["user_id"]); // 优惠券|积分 退还 if ($order["voucher_id"] > 0) { (new Voucher())->update(["status" => \E_BASE_STATUS::Normal], ["id" => $order["voucher_id"]]); } $this->userWalletModel->where("id", $wallet["id"])->setDec("score", $order["total_real_amount"]); $user_bills = [ $save_bill, [ "user_id" => $order->user_id, "currency_type" => \E_USER_BILL_CURRENCY_TYPE::Score, "change_type" => \E_USER_BILL_CHANGE_TYPE::ScoreOrderRefund[0], "change" => -$order["total_real_amount"], "before" => $wallet["score"], "after" => $wallet["score"] - $order["total_real_amount"], "reason" => \E_USER_BILL_CHANGE_TYPE::ScoreOrderRefund[1], "money" => 0, "give_money" => 0, "relation_no" => $order->no, "createtime" => time() ] ]; // 抵扣余额退还 if ($order["balance_deduction"] > 0) { array_push($user_bills, [ "user_id" => $order->user_id, "currency_type" => \E_USER_BILL_CURRENCY_TYPE::Money, "change_type" => \E_USER_BILL_CHANGE_TYPE::BalanceDeductionRefund[0], "change" => $order["balance_deduction"], "before" => $wallet["money"], "after" => $wallet["money"] + $order["balance_deduction"], "reason" => \E_USER_BILL_CHANGE_TYPE::BalanceDeductionRefund[1], "money" => $order["balance_deduction"], "give_money" => 0, "relation_no" => $order->no, "createtime" => time() ]); $this->userWalletModel->where("id", $wallet["id"])->setInc("money", $order["balance_deduction"]); } $this->userBillModel->saveAll($user_bills); Message::sendSystemMessage( \E_IDENTITY_TYPE::User, ["to_user_id" => $order->user_id], "取消订单通知", "您的订单【{$order->no}】已取消,订单金额/优惠券将原路返回您的账户!" ); $user = (new User())->get($order['user_id']); if ($user) { TencentCloudService::tencent_cloud_sms_send('1913760', $user["mobile"], [$order["no"]]); } Db::commit(); return $this->ok(true); } catch (Exception $e) { Db::rollback(); return $this->fail($e->getMessage()); } finally { UserService::updateGrouping($order["user_id"]); $redLock->unlock($lock); } } }