PaymentOrder.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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\api\service;
  10. use app\common\library\payment\PaymentException;
  11. use mall\basic\Bonus;
  12. use mall\basic\Distribution;
  13. use mall\basic\Promotion;
  14. use mall\basic\Shopping;
  15. use mall\basic\Store;
  16. use mall\basic\Users;
  17. use mall\utils\Check;
  18. use think\facade\Db;
  19. use think\facade\Request;
  20. use app\common\models\Area as AreaModel;
  21. use app\common\library\payment\Payment;
  22. use app\common\service\order\Order as OrderService;
  23. class PaymentOrder extends Service {
  24. /**
  25. * 获取用户优惠劵
  26. * @param $amount
  27. * @return array
  28. * @throws \think\db\exception\DataNotFoundException
  29. * @throws \think\db\exception\DbException
  30. * @throws \think\db\exception\ModelNotFoundException
  31. */
  32. public static function getBonusData($amount){
  33. $bonus = Db::name("users_bonus")
  34. ->alias("u")
  35. ->field("b.*,u.id as users_bonus_id")
  36. ->join("promotion_bonus b","u.bonus_id=b.id","LEFT")
  37. ->where("u.user_id",Users::get("id"))
  38. ->where("u.status",0)
  39. ->where("b.end_time > " . time())
  40. ->where('order_amount <= 0 OR ' . $amount . ' >= order_amount')
  41. ->select()->toArray();
  42. $coupon = [];
  43. foreach($bonus as $key=>$value){
  44. $coupon[$key] = [
  45. "id"=>$value["users_bonus_id"],
  46. "name"=>$value["name"],
  47. "condition"=>$value["order_amount"] <= 0 ? "无门槛" : "满".$value["order_amount"].'可用',
  48. "startAt"=>date("Y-m-d",$value["start_time"]),
  49. "endAt"=>date("Y-m-d",$value["end_time"]),
  50. "price"=>(int)$value["amount"],
  51. "valueDesc"=>number_format($value["amount"]),
  52. "unitDesc"=>"元",
  53. "reason"=>""
  54. ];
  55. }
  56. return $coupon;
  57. }
  58. /**
  59. * 确认订单
  60. * @param array $params
  61. * @return array
  62. * @throws \think\db\exception\DataNotFoundException
  63. * @throws \think\db\exception\DbException
  64. * @throws \think\db\exception\ModelNotFoundException
  65. */
  66. public static function confirm($params=[]){
  67. $id = strip_tags($params["id"]??"");
  68. $bonus_id = strip_tags($params["bonus_id"]??0);
  69. $address_id = strip_tags($params["address_id"]??0);
  70. $shipping_type = intval($params["shipping_type"]??0);
  71. $store_id = intval($params["store_id"]??0);
  72. $product_id = strip_tags($params["sku_id"]??0);
  73. $type = strip_tags($params["type"]??"");
  74. $num = intval($params["num"]??1);
  75. $array = array_map("intval",explode(",",$id));
  76. $array = array_filter($array,function ($res){
  77. return $res != 0;
  78. });
  79. if(count($array) <= 0){
  80. throw new \Exception("请选择需要购买的商品",0);
  81. }else if(empty($type) || !in_array($type,["buy","cart","group","point","second","special","regiment"])){
  82. throw new \Exception("非法操作",0);
  83. }
  84. $cart = [];
  85. if($type == "cart"){
  86. $rs = Db::name("cart")->where("user_id",Users::get("id"))->where("id","in", implode(",",$array))->select()->toArray();
  87. if(empty($rs)){
  88. throw new \Exception("请选择商品后在提交订单",0);
  89. }
  90. foreach($rs as $k=>$v){
  91. $cart[$k] = [
  92. "activity_id"=>0,
  93. "type"=>0,
  94. "goods_id"=>$v["goods_id"],
  95. "product_id"=>$v["product_id"],
  96. "spec_key"=>$v["spec_key"],
  97. "nums"=>$v["goods_nums"]
  98. ];
  99. }
  100. }else{
  101. if($num <= 0) $num = 1;
  102. $promotion = Promotion::checkOrderType($id,$product_id,$num,$type);
  103. array_push($cart,$promotion);
  104. }
  105. $data = Shopping::get($cart);
  106. $data["bonus"] = self::getBonusData($data["real_amount"]);
  107. // 检查是否开启到店自提
  108. $data["is_shipping"] = Db::name("setting")->where("name","is_shipping")->value("value");
  109. $address = Db::name("users_address")->where("user_id",Users::get("id"))->select()->toArray();
  110. $addressData = ["default"=>[],"list"=>[],"store"=>[]];
  111. foreach ($address as $key=>$value){
  112. $v = [
  113. "id"=>$value["id"],
  114. "name"=>$value["accept_name"],
  115. "tel"=>$value["mobile"],
  116. "province"=>$value["province"],
  117. "city"=>$value["city"],
  118. "area"=>$value["area"],
  119. "address"=>AreaModel::getArea([$value["province"],$value["city"],$value["area"]],',') . ' ' . $value["address"],
  120. ];
  121. if($address_id == $value["id"] || (empty($addressData["default"]) && $value["is_default"])){
  122. $addressData["default"] = $v;
  123. }
  124. $addressData["list"][] = $v;
  125. }
  126. $data["address"] = $addressData;
  127. $addressData["default"] = empty($addressData["default"]) ? (isset($addressData["list"][0]) ? $addressData["list"][0] : []) : $addressData["default"];
  128. try {
  129. if($shipping_type == 2){
  130. $store = Db::name("store")->where("id",$store_id)->find();
  131. Store::get($data,$store);
  132. }else{
  133. Distribution::get($data,$addressData["default"]);
  134. }
  135. if($bonus_id > 0){
  136. Bonus::apply($data,$bonus_id);
  137. }
  138. }catch (\Exception $e){
  139. throw new \Exception($e->getMessage(),$e->getCode() > 0 ? 1 : 0,$e->getCode());
  140. }
  141. $data["users_price"] = Users::get("amount");
  142. $data["users_point"] = Users::get("point");
  143. $data["store"] = [];
  144. if($data["is_shipping"]){
  145. $store = Db::name("store")->where(["status"=>0,"is_del"=>0])->select()->toArray();
  146. $data["store"] = array_map(function ($value){
  147. return [
  148. "id"=>$value["id"],
  149. "name"=>$value["shop_name"],
  150. "tel"=>$value["phone"],
  151. "address"=>AreaModel::getArea([$value["province"],$value["city"],$value["area"]],',') . ' ' . $value["address"]
  152. ];
  153. }, $store);
  154. }
  155. foreach($data["item"] as $key=>$value){
  156. if(isset($value["fictitious_array"])){
  157. unset($data["item"][$key]["fictitious_array"]);
  158. }
  159. }
  160. return $data;
  161. }
  162. /**
  163. * 创建订单并且调起支付
  164. * @param array $params
  165. * @return array|mixed
  166. * @throws \think\db\exception\DataNotFoundException
  167. * @throws \think\db\exception\DbException
  168. * @throws \think\db\exception\ModelNotFoundException
  169. */
  170. public static function create($params=[]){
  171. $id = strip_tags($params["id"]??"");
  172. $bonus_id = strip_tags($params["bonus_id"]??0);
  173. $address_id = strip_tags($params["address_id"]??0);
  174. $shipping_type = intval($params["shipping_type"]??0);
  175. $store_id = strip_tags($params["store_id"]??0);
  176. $product_id = strip_tags($params["sku_id"]??"");
  177. $type = strip_tags($params["type"]??"");
  178. $payment_id = strip_tags($params["payment"]??"");
  179. $remarks = strip_tags($params["remarks"]??"");
  180. $num = intval($params["num"]??1);
  181. $source = intval($params["source"]??1);
  182. $array = array_map("intval",explode(",",$id));
  183. $array = array_filter($array,function ($res){
  184. return $res != 0;
  185. });
  186. if(count($array) <= 0){
  187. throw new \Exception("请选择需要购买的商品",0);
  188. }else if(empty($type) || !in_array($type,["buy","cart","group","point","second","special","regiment"])){
  189. throw new \Exception("非法操作",0);
  190. }
  191. if(!in_array($source,[1,2,3,4])){
  192. $source = 1;
  193. }
  194. $payment_id = Payment::instance()->getPayType($payment_id,$source);
  195. $cart = [];
  196. if($type == "cart"){
  197. $rs = Db::name("cart")->where("user_id",Users::get("id"))->where("id","in", implode(",",$array))->select()->toArray();
  198. if(empty($rs)){
  199. throw new \Exception("请选择商品后在提交订单",0);
  200. }
  201. foreach($rs as $k=>$v){
  202. $cart[$k] = [
  203. "activity_id"=>0,
  204. "type"=>0,
  205. "cart_id"=>$v["id"],
  206. "goods_id"=>$v["goods_id"],
  207. "product_id"=>$v["product_id"],
  208. "spec_key"=>$v["spec_key"],
  209. "nums"=>$v["goods_nums"]
  210. ];
  211. }
  212. }else{
  213. if($num <= 0) $num = 1;
  214. $promotion = Promotion::checkOrderType($id,$product_id,$num,$type);
  215. array_push($cart,$promotion);
  216. }
  217. // 检查是否开启到店自提
  218. $is_shipping = Db::name("setting")->where("name","is_shipping")->value("value");
  219. if($shipping_type == 2 && $is_shipping){
  220. if(($store = Db::name("store")->where("id",$store_id)->find()) == false){
  221. throw new \Exception("您选择的到店自提地址不存在,请重新选择",0);
  222. }
  223. }else{
  224. if(($address = Db::name("users_address")->where(["id"=>$address_id,"user_id"=>Users::get("id")])->find()) == false){
  225. throw new \Exception("您选择的地址不存在,请重新选择",0);
  226. }
  227. }
  228. if($bonus_id > 0 && ($bonus = Db::name("users_bonus")->where("user_id",Users::get("id"))->where("id",$bonus_id)->find()) == false){
  229. throw new \Exception("您选择的优惠劵不存在!",0);
  230. }
  231. if(($payment = Db::name("payment")->where("code",$payment_id)->find()) == false){
  232. throw new \Exception("您选择的支付方式不存在!",0);
  233. }
  234. if(Check::strlen($remarks) > 100){
  235. throw new \Exception("留言内容不得超过100个字符",0);
  236. }
  237. try {
  238. $data = Shopping::get($cart);
  239. if($shipping_type == 2){
  240. $data["shipping_type"] = $shipping_type;
  241. $data["store_id"] = $store["id"];
  242. Store::get($data,$store);
  243. }else{
  244. Distribution::get($data,$address);
  245. $data["address"] = $address;
  246. }
  247. if($bonus_id > 0){
  248. Bonus::apply($data,$bonus_id);
  249. }
  250. if(isset($data["real_point"]) && $data["real_point"] > Users::get("point")){
  251. throw new \Exception("您的积分不足,不能兑换商品",0);
  252. }
  253. $data["payment"] = $payment;
  254. $data["remarks"] = $remarks;
  255. $data["source"] = $source;
  256. $data['order_id'] = OrderService::createOrder($data);
  257. Bonus::updateStatus($bonus_id,$data['order_id']);
  258. $result = Payment::instance()->setOrderData($data['order_id'])->pay($payment);
  259. if($type == 'cart'){
  260. Shopping::delete(array_map(function ($res){
  261. return $res["cart_id"];
  262. },$cart));
  263. }
  264. Promotion::updateStatus($data);
  265. } catch (PaymentException $ex){
  266. return $ex->getRaw();
  267. } catch (\Exception $e){
  268. throw new \Exception($e->getMessage(),$e->getCode() > 0 ? 1 : 0);
  269. }
  270. $result["shop_count"] = Db::name("cart")->where("user_id",Users::get("id"))->count();
  271. return $result;
  272. }
  273. public static function payment($params=[]){
  274. $order_id = intval($params["order_id"]??0);
  275. $payment_id = trim(strip_tags($params["payment_id"]??""));
  276. $source = intval($params["source"]??1);
  277. if(!in_array($source,[1,2,3,4])){
  278. $source = 1;
  279. }
  280. $payment_id = Payment::instance()->getPayType($payment_id,$source);
  281. try{
  282. if(($payment = Db::name("payment")->where("code",$payment_id)->find()) == false){
  283. throw new \Exception("支付方式不存在!",0);
  284. }
  285. Db::name("order")->where(["user_id"=>Users::get("id"),"id"=>$order_id])->update(["pay_type"=>$payment["id"]]);
  286. return Payment::instance()->setOrderData($order_id)->pay($payment);
  287. }catch (PaymentException $ex){
  288. return $ex->getRaw();
  289. }catch (\Exception $ex){
  290. throw new \Exception($ex->getMessage(),$ex->getCode());
  291. }
  292. }
  293. /**
  294. * 会员充值
  295. * @param array $params
  296. * @return array
  297. * @throws \think\db\exception\DataNotFoundException
  298. * @throws \think\db\exception\DbException
  299. * @throws \think\db\exception\ModelNotFoundException
  300. */
  301. public static function recharge($params=[]){
  302. $payment = trim(strip_tags($params["payment"]??""));
  303. $source = intval($params["source"]??"");
  304. $price = floatval($params["price"]??0);
  305. if(empty($price) || $price <= 0){
  306. throw new \Exception("请输入您要充值的金额!",0);
  307. }
  308. if(!in_array($source,[1,2,3,4])){
  309. $source = 1;
  310. }
  311. if($payment == "balance"){
  312. throw new \Exception("非法操作",0);
  313. }
  314. $payment_id = Payment::instance()->getPayType($payment,$source);
  315. if(!$payment = Db::name("payment")->where("code",$payment_id)->find()){
  316. throw new \Exception("您选择的支付方式不存在!",0);
  317. }
  318. Db::name("users_rechange")->insert([
  319. "user_id"=>Users::get("id"),
  320. "pay_type"=>$payment["id"],
  321. "order_no"=>"P".orderNo(),
  322. "order_amount"=>$price,
  323. "payment_name"=>$payment["name"],
  324. "status"=>0,
  325. "create_time"=>time()
  326. ]);
  327. $order_id = Db::name("users_rechange")->getLastInsID();
  328. return Payment::instance()->setRechargeData($order_id)->pay($payment);
  329. }
  330. }