Payment.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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;
  10. use app\common\library\payment\wechat\WeChat;
  11. use app\common\library\payment\balance\Balance;
  12. use mall\basic\Users;
  13. use mall\utils\CString;
  14. use think\facade\Db;
  15. use app\common\models\Setting as SettingModel;
  16. use app\common\service\order\Order;
  17. class Payment {
  18. private static $instance;
  19. protected $order = [];
  20. private function __construct() {}
  21. public static function instance(){
  22. if (!self::$instance instanceof self) {
  23. self::$instance = new self();
  24. }
  25. return self::$instance;
  26. }
  27. /**
  28. * 设置订单数据
  29. * @param $order_id
  30. * @return $this
  31. * @throws PaymentException
  32. * @throws \think\db\exception\DataNotFoundException
  33. * @throws \think\db\exception\DbException
  34. * @throws \think\db\exception\ModelNotFoundException
  35. */
  36. public function setOrderData($order_id){
  37. if(!$order = Db::name("order")->where("id",$order_id)->find()){
  38. throw new \Exception("您要支付的订单不存在!",0);
  39. }
  40. if($order["pay_status"] == 1){
  41. throw new PaymentException("您的订单已支付,请勿重复支付。",0,[
  42. "pay" => 99,
  43. "order_id" => $order["id"],
  44. "msg" => "您的订单已支付,请勿重复支付。"
  45. ]);
  46. }
  47. // 检查是否为积分订单
  48. if($order["type"] == 1){
  49. Db::name("users")->where("id",Users::get("id"))->update([
  50. "point"=>Db::raw("point-".$order["real_point"])
  51. ]);
  52. Db::name("users_log")->insert([
  53. "user_id" => Users::get("id"),
  54. "order_no" => $order["order_no"],
  55. "action" => 1,
  56. "operation" => 1,
  57. "point" => $order["real_point"],
  58. "description" => "成功购买了订单号:{$order["order_no"]}中的商品,积分减少{$order["real_point"]}",
  59. "create_time" => time()
  60. ]);
  61. }
  62. // 如果订单金额小于等于0 支付成功
  63. if($order["order_amount"] <= 0){
  64. Db::name("order_log")->insert([
  65. 'order_id' => $order["id"],
  66. 'username' => "system",
  67. 'action' => '付款',
  68. 'result' => '成功',
  69. 'note' => '订单【' . $order["order_no"] . '】付款' . $order["order_amount"] . '元',
  70. 'create_time' => time()
  71. ]);
  72. Order::payment($order["order_no"]);
  73. throw new PaymentException("支付成功",1,[
  74. "pay" => 0,
  75. "order_id" => $order["id"],
  76. "msg" => "支付成功"
  77. ]);
  78. }
  79. $goods_array = Db::name("order_goods")->where("order_id",$order_id)->order("id","asc")->value("goods_array");
  80. $goods_title = SettingModel::getStringData("web_name");
  81. if(!empty($goods_array)){
  82. $goods_array = json_decode($goods_array,true);
  83. $goods_title = "-" . CString::msubstr($goods_array["title"],30,false);
  84. }
  85. $order["web_title"] = $goods_title;
  86. $this->order = $order;
  87. return $this;
  88. }
  89. public function pay($payment){
  90. $payType = explode("-",$payment["code"]);
  91. $method = isset($payType[1]) ? $payType[1] : $payType[0];
  92. switch($payType[0]){
  93. case "wechat":
  94. return (new WeChat())->$method($this->order);
  95. case "balance":
  96. return (new Balance())->pay($this->order);
  97. }
  98. }
  99. public function getPayType($payment_id,$source){
  100. switch($payment_id){
  101. case "wechat":
  102. return $this->getWechatID($source);
  103. default:
  104. return $payment_id;
  105. }
  106. }
  107. public function getWechatID($source){
  108. switch($source){
  109. case 1:
  110. return "wechat-h5";
  111. case 2:
  112. return "wechat-mp";
  113. case 3:
  114. return "wechat-mini";
  115. case 4:
  116. return "wechat-app";
  117. case 5:
  118. return "wechat-qrcode";
  119. }
  120. }
  121. }