Balance.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | A3Mall
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Author: xzncit <158373108@qq.com>
  8. // +----------------------------------------------------------------------
  9. namespace app\common\library\payment\balance;
  10. use think\facade\Db;
  11. use app\common\service\order\Order;
  12. /**
  13. * 余额支付
  14. * Class Balance
  15. * @package app\common\library\payment\balance
  16. */
  17. class Balance {
  18. /**
  19. * 支付
  20. * @param array $order
  21. * @return array
  22. * @throws \Exception
  23. */
  24. public function pay($order=[]){
  25. try{
  26. $users = Db::name("users")->where("id",$order["user_id"])->find();
  27. if($order["order_amount"] > $users["amount"]){
  28. throw new \Exception("您的余额不足,请充值。",0);
  29. }
  30. Db::startTrans();
  31. Db::name("users")->where("id",$order["user_id"])->dec("amount",$order["order_amount"])->update();
  32. Order::payment($order["order_no"]);
  33. Db::name("order_log")->insert([
  34. 'order_id' => $order["id"],
  35. 'username' => "system",
  36. 'action' => '付款',
  37. 'result' => '成功',
  38. 'note' => '订单【' . $order["order_no"] . '】付款' . $order["order_amount"] . '元',
  39. 'create_time' => time()
  40. ]);
  41. Db::commit();
  42. sendSMS(["mobile"=>$order["mobile"],"order_no"=>$order["order_no"]],"payment_success");
  43. return [
  44. "pay" => 0,
  45. "order_id" => $order["id"],
  46. "msg" => "支付成功"
  47. ];
  48. }catch (\Exception $ex){
  49. Db::rollback();
  50. throw new \Exception($ex->getMessage(),$ex->getCode());
  51. }
  52. }
  53. }