BasePayService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Bear
  5. * Date: 2019/8/12
  6. * Time: 下午7:14
  7. */
  8. namespace app\main\service;
  9. use app\common\library\Ip;
  10. use app\common\library\Redis;
  11. use app\common\model\Goods;
  12. use app\main\constants\AdminConstants;
  13. use app\main\constants\ErrorCodeConstants;
  14. use app\main\constants\OrderContents;
  15. use app\main\constants\PayConstants;
  16. use app\main\InternalInterface\PayInterface;
  17. use app\main\model\object\OrderObject;
  18. use app\main\model\object\ReturnObject;
  19. use app\main\model\object\UserObject;
  20. use app\main\model\object\WxPayObject;
  21. use app\main\model\object\WxPayParamsObject;
  22. use fast\Random;
  23. abstract class BasePayService extends BaseService implements PayInterface
  24. {
  25. /**
  26. * @var OrderObject
  27. */
  28. public $oOrder;
  29. /**
  30. * @var WxPayObject
  31. */
  32. public $oWxPay;
  33. /**
  34. * @var WxPayParamsObject
  35. */
  36. public $oWxPayParams;
  37. /**
  38. * @var UserObject
  39. */
  40. public $oUser;
  41. /**
  42. * @var Goods
  43. */
  44. public $mGoods;
  45. /**
  46. * @param $payment_method
  47. * @return $this
  48. */
  49. public function init($payment_method)
  50. {
  51. $pay = OfficialAccountsService::instance()
  52. ->getWxpayModel()
  53. ->where('payment_method', $payment_method)
  54. ->where('status', PayConstants::WXPAY_STATUS_ON)
  55. ->find();
  56. if (!$pay) {
  57. throw new \Exception('支付方式不存在,请换一种支付方式');
  58. }
  59. $this->oWxPay = (new WxPayObject())->bind($pay->toArray());
  60. $this->oOrder = new OrderObject();
  61. return $this;
  62. }
  63. /**
  64. * 设置用户属性
  65. * @param UserObject $user
  66. * @return $this
  67. */
  68. public function setUser(UserObject $user)
  69. {
  70. $this->oUser = $user;
  71. return $this;
  72. }
  73. /**
  74. * 获取用户属性
  75. * @return UserObject
  76. */
  77. public function getUser()
  78. {
  79. return $this->oUser;
  80. }
  81. /**
  82. * 设置主动参数
  83. * @param $wxPayParams
  84. * @return $this
  85. */
  86. public function setParams($wxPayParams)
  87. {
  88. $this->oWxPayParams = (new WxPayParamsObject())->bind($wxPayParams);
  89. return $this;
  90. }
  91. /**
  92. * 检查活动是否有效
  93. * @param null $activity_id
  94. * @return ReturnObject
  95. */
  96. public function checkActivity($activity_id = null)
  97. {
  98. if (!$activity_id) {
  99. $activity_id = $this->oWxPayParams->activityId;
  100. }
  101. if (!$activity_id) {
  102. return $this->getReturn();
  103. }
  104. $activity = ActivityService::instance()->getActivityModel()->where('id', $activity_id)->find();
  105. if (!$activity) {
  106. return $this->setCode(ErrorCodeConstants::PARAMS_ERROR_INVALID)->setMsg('活动不存在')->getReturn();
  107. }
  108. if ($activity['starttime'] > time() || $activity['endtime'] < time()) {
  109. return $this->setCode(ErrorCodeConstants::PARAMS_ERROR_INVALID)->setMsg('活动未进行')->getReturn();
  110. }
  111. return $this->getReturn();
  112. }
  113. /**
  114. * @return \app\main\model\object\ReturnObject|array
  115. */
  116. public function saveOrder()
  117. {
  118. try {
  119. $goods_id = $this->oWxPayParams->id ?? 0;
  120. if (!$this->mGoods = GoodsService::instance()->getGoodsModel()->getGoodsInfoById($goods_id)) {
  121. throw new \Exception('商品不存在');
  122. }
  123. $checkActivity = $this->checkActivity();
  124. if ($checkActivity->code != ErrorCodeConstants::SUCCESS) {
  125. return $checkActivity;
  126. }
  127. //初始化订单信息
  128. $this->initOrderInfo();
  129. $cacheKey = OrderService::instance()->getOrderCacheKey($this->oOrder)->data;
  130. if ($cache = Redis::instance()->get($cacheKey)) {
  131. LogService::debug('调用缓存:' . $cacheKey . ':' . $cache);
  132. return $this->setData(json_decode($cache, true))->getReturn();
  133. }
  134. //订单扣量&订单转移
  135. list($admin_id, $group_id) = $this->initOrderDeductionOrTransfer();
  136. $this->oOrder->admin_id = $admin_id;
  137. //订单赏金处理
  138. $this->initOrderReward();
  139. //三方平台订单创建
  140. $pay_json = $this->createOrder();
  141. //系统创建订单
  142. if (!$ordersId = OrderService::instance()->getOrderModel()->insertGetId($this->oOrder->toArray())) {
  143. throw new \Exception("订单创建失败");
  144. }
  145. if ($pay_json->code == ErrorCodeConstants::SUCCESS) {
  146. $pay_json->data['order_id'] = $this->oOrder->out_trade_no;
  147. Redis::instance()->setex($cacheKey, PayConstants::getPayTimeoutSeconds(), json_encode($pay_json->data));
  148. }
  149. //统计相关信息,扣量不统计
  150. if (!$this->oOrder->deduct) {
  151. $channel_id = WebUserService::instance()->getUserChannelId()->data;
  152. $business = $this->oWxPayParams->businessLine ?? PayConstants::BUSINESS_APP;
  153. OrderCollectService::instance()->addCreateOrderCollect($this->oOrder->user_id, $admin_id, (bool)$this->oOrder->day, $channel_id, $group_id, $business, (bool)$this->oOrder->activity_id);
  154. }
  155. return $pay_json;
  156. } catch (\Exception $e) {
  157. return $this->setCode(ErrorCodeConstants::EXCEPTION)->setMsg($e->getMessage())->getReturn();
  158. }
  159. }
  160. /**
  161. * 订单扣量与转移
  162. */
  163. protected function initOrderDeductionOrTransfer()
  164. {
  165. $admin_id = WebUserService::instance()->getUserAdminId()->data;
  166. $group_id = model('AuthGroupAccess')->getGroupId($admin_id);
  167. $channel_id = $this->oUser->channel_id;
  168. $is_activity = (bool)$this->oWxPayParams->activityId;
  169. try {
  170. if ($channel_id) {//订单KL
  171. if ($adminInfo = AdminKlService::instance()->orderKL($admin_id, $channel_id, $this->oOrder->toArray(), $is_activity)->data) {
  172. $this->oOrder->deduct = OrderContents::ORDER_DEDUCT_YES;
  173. $this->oOrder->admin_id = $adminInfo['admin_id'];
  174. $this->oOrder->resource_id = $admin_id;
  175. }
  176. }
  177. if ($group_id == AdminConstants::ADMIN_GROUP_ID_AGENT && empty($this->oOrder->deduct)) { //订单ZY
  178. if ($adminInfo = AdminKlService::instance()->orderTransfer($admin_id, $channel_id, $this->oOrder->toArray(), $is_activity)) {
  179. $this->oOrder->benefit = $adminInfo['benefit'];
  180. $this->oOrder->money_benefit = round($this->mGoods['money'] * $adminInfo['benefit'], 2);
  181. $this->oOrder->admin_id = $channel_id;
  182. $this->oOrder->resource_id = $admin_id;
  183. $admin_id = $channel_id;
  184. $group_id = AdminConstants::ADMIN_GROUP_ID_CHANNEL;
  185. }
  186. }
  187. } catch (\Exception $e) {
  188. LogService::error('订单KL失败, Error:' . $e->getMessage());
  189. }
  190. return [$admin_id, $group_id];
  191. }
  192. /**
  193. * 订单的赏金处理
  194. */
  195. final protected function initOrderReward()
  196. {
  197. $admin_id = WebUserService::instance()->getUserAdminId()->data;
  198. $adminExtend = AdminService::instance()->getAdminExtendModel()->getInfo($this->oOrder->admin_id);
  199. $is_activity = (bool)$this->oWxPayParams->activityId;
  200. $group_id = model('AuthGroupAccess')->getGroupId($admin_id);
  201. //不是活动订单,如果订单没有扣量也没有转移,判断订单是否有赏金
  202. if (
  203. !$is_activity &&
  204. empty($this->oOrder->deduct) &&
  205. empty($this->oOrder->resource_id) &&
  206. $group_id == 4 &&
  207. !empty($adminExtend['invite_id'])
  208. ) {
  209. $invite_admin = model('AdminExtend')->where(['admin_id' => $adminExtend['invite_id']])->find();
  210. $admin_agent = model('Admin')->where('id', $adminExtend['invite_id'])->find();
  211. if ($invite_admin && $admin_agent['status'] == 'normal') {
  212. $this->oOrder->reward_money = round($this->mGoods['money'] * $invite_admin['reward_benefit'], 2);
  213. $this->oOrder->reward_admin_id = $adminExtend['invite_id'];
  214. $this->oOrder->reward_benefit = $invite_admin['reward_benefit'];
  215. }
  216. }
  217. }
  218. /**
  219. * 初始化订单信息
  220. */
  221. protected function initOrderInfo()
  222. {
  223. $this->oOrder->admin_id = WebUserService::instance()->getUserAdminId()->data;
  224. $adminExtend = AdminService::instance()->getAdminExtendModel()->getInfo($this->oOrder->admin_id);
  225. $benefit = $adminExtend['benefit'];
  226. if($this->oWxPay->business_line == PayConstants::BUSINESS_APP){
  227. $benefit = $adminExtend['benefit_app'];
  228. }
  229. //初始化订单信息
  230. $this->oOrder->user_id = $this->oUser->id;
  231. $this->oOrder->benefit = $benefit;
  232. $this->oOrder->money = $this->mGoods['money'];
  233. $this->oOrder->money_benefit = round($this->mGoods['money'] * $benefit, 2);
  234. $this->oOrder->category = $this->mGoods['category_id'];
  235. $this->oOrder->ip = Ip::ip();
  236. $this->oOrder->referral_id = $this->oUser->referral_id;
  237. $this->oOrder->referral_id_permanent = $this->oUser->referral_id_permanent;
  238. $this->oOrder->book_id = $this->oWxPayParams->bookId;
  239. $this->oOrder->chapter_id = $this->oWxPayParams->chapterId;
  240. $this->oOrder->goods_id = $this->mGoods['id'];
  241. $this->oOrder->kandian = $this->mGoods['kandian'];
  242. $this->oOrder->free_kandian = $this->mGoods['free_kandian'];
  243. $this->oOrder->day = $this->mGoods['day'];
  244. $this->oOrder->type = $this->mGoods['type'];
  245. $this->oOrder->state = OrderContents::ORDER_STATE_TO_PAY;
  246. $this->oOrder->activity_id = $this->oWxPayParams->activityId;
  247. $this->oOrder->wxpay_id = $this->oWxPay->id;
  248. $this->oOrder->payment_method = $this->oWxPay->payment_method;
  249. $this->oOrder->createtime = time();
  250. $this->oOrder->updatetime = time();
  251. $this->oOrder->business_line = $this->oWxPay->business_line;
  252. $this->oOrder->ext = $this->oUser->ext;
  253. //需要三方支付补充信息
  254. $this->oOrder->payid = '';
  255. $this->oOrder->pay_json = '';
  256. $this->oOrder->out_trade_no = '';
  257. $this->oOrder->pdorderid = '';
  258. }
  259. /**
  260. * 获取订单编号
  261. * @param $user_id
  262. * @return \app\main\model\object\ReturnObject
  263. */
  264. protected function getOutTradeNo($user_id)
  265. {
  266. $number = date('YmdHis') . '_' . $user_id . '_' . Random::build('alnum', 4);
  267. return $this->setData($number)->getReturn();
  268. }
  269. /**
  270. * 获取跳转地址
  271. * @return \app\main\model\object\ReturnObject
  272. */
  273. public function getRedirectUrl()
  274. {
  275. $url = $_SERVER['HTTP_REFERER'] ?? '';
  276. if ($url) {
  277. $referral = parse_url($url);
  278. $url = $referral['scheme'] . '://' . $referral['host'] . $referral['path'] . '?status=return';
  279. }
  280. return $this->setData($url)->getReturn();
  281. }
  282. /**
  283. * 调用第三方服务创建订单
  284. * @return ReturnObject
  285. */
  286. abstract function createOrder();
  287. }