TestPaperOrder.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\wap\model\topic;
  12. use traits\ModelTrait;
  13. use basic\ModelBasic;
  14. use app\wap\model\topic\TestPaper;
  15. use app\wap\model\user\User;
  16. use app\wap\model\user\UserBill;
  17. use app\wap\model\user\WechatUser;
  18. use app\wap\model\routine\RoutineTemplate;
  19. use app\wap\model\merchant\MerchantFlowingWater;
  20. use service\SystemConfigService;
  21. use service\WechatService;
  22. use service\WechatTemplateService;
  23. use think\Url;
  24. /**
  25. * 试卷订单 model
  26. * Class TestPaperOrder
  27. */
  28. class TestPaperOrder extends ModelBasic
  29. {
  30. use ModelTrait;
  31. protected $insert = ['add_time'];
  32. protected static $payType = ['weixin' => '微信支付', 'yue' => '余额支付', 'offline' => '线下支付', 'zhifubao' => '支付宝'];
  33. protected function setAddTimeAttr()
  34. {
  35. return time();
  36. }
  37. public static function getNewOrderId()
  38. {
  39. $count = (int)self::where('add_time', ['>=', strtotime(date("Y-m-d"))], ['<', strtotime(date("Y-m-d", strtotime('+1 day')))])->count();
  40. return 'wx' . date('YmdHis', time()) . (10000 + $count + 1);
  41. }
  42. /**
  43. * 创建订单试卷订单
  44. * @param $special
  45. * @param $pinkId
  46. * @param $pay_type
  47. * @param $uid
  48. * @param $payType
  49. * @param int $link_pay_uid
  50. * @param int $total_num
  51. * @return bool|object
  52. */
  53. public static function createTestPaperOrder($testPaper, $uid, $payType, $total_num = 1)
  54. {
  55. if (!array_key_exists($payType, self::$payType)) return self::setErrorInfo('选择支付方式有误!');
  56. $userInfo = User::getUserData($uid);
  57. if (!$userInfo) return self::setErrorInfo('用户不存在!');
  58. $total_price = $testPaper->money;
  59. if (isset($userInfo['level']) && $userInfo['level'] > 0) {
  60. $total_price = $testPaper->member_money;
  61. }
  62. $res = TestPaperObtain::PayTestPaper($testPaper->id, $uid, 2);
  63. if ($res) return self::setErrorInfo('您已购买试卷,无需再次购买!');
  64. $orderInfo = [
  65. 'uid' => $uid,
  66. 'order_id' => self::getNewOrderId(),
  67. 'test_id' => $testPaper->id,
  68. 'total_num' => $total_num,
  69. 'total_price' => $total_price,
  70. 'pay_price' => $total_price,
  71. 'pay_type' => $payType,
  72. 'paid' => 0,
  73. 'unique' => md5(time() . '' . $uid . $testPaper->id),
  74. 'mer_id' => $testPaper->mer_id,
  75. 'is_del' => 0
  76. ];
  77. $order = self::set($orderInfo);
  78. if (!$order) return self::setErrorInfo('订单生成失败!');
  79. return $order;
  80. }
  81. /**
  82. * 微信支付 为 0元时 试卷
  83. * @param $order_id
  84. * @param $uid
  85. * @return bool
  86. */
  87. public static function jsPayTestPaperPrice($order_id, $uid)
  88. {
  89. $orderInfo = self::where('uid', $uid)->where('order_id', $order_id)->where('is_del', 0)->find();
  90. if (!$orderInfo) return self::setErrorInfo('订单不存在!');
  91. if ($orderInfo['paid']) return self::setErrorInfo('该订单已支付!');
  92. $userInfo = User::getUserData($uid);
  93. if (!$userInfo) return self::setErrorInfo('用户不存在!');
  94. self::beginTrans();
  95. $res1 = UserBill::expend('购买试卷', $uid, 'now_money', 'pay_test_paper', $orderInfo['pay_price'], $orderInfo['id'], $userInfo['now_money'], '支付' . floatval($orderInfo['pay_price']) . '元购买试卷');
  96. $res2 = self::payTestPaperSuccess($order_id);
  97. $res = $res1 && $res2;
  98. self::checkTrans($res);
  99. return $res;
  100. }
  101. /**试卷微信支付
  102. * @param $orderId
  103. * @param string $field
  104. * @return array|string
  105. * @throws \think\db\exception\DataNotFoundException
  106. * @throws \think\db\exception\ModelNotFoundException
  107. * @throws \think\exception\DbException
  108. */
  109. public static function jsTestPaperPay($orderId, $field = 'order_id')
  110. {
  111. if (is_string($orderId))
  112. $orderInfo = self::where($field, $orderId)->where('is_del', 0)->find();
  113. else
  114. $orderInfo = $orderId;
  115. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  116. if ($orderInfo['paid']) exception('支付已支付!');
  117. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  118. $openid = WechatUser::uidToOpenid($orderInfo['uid']);
  119. return WechatService::jsPay($openid, $orderInfo['order_id'], $orderInfo['pay_price'], 'testpaper', SystemConfigService::get('site_name'));
  120. }
  121. /**
  122. * 微信h5支付
  123. * @param $orderId
  124. * @param string $field
  125. * @return array|string
  126. * @throws \think\db\exception\DataNotFoundException
  127. * @throws \think\db\exception\ModelNotFoundException
  128. * @throws \think\exception\DbException
  129. */
  130. public static function h5TestPaperPay($orderId, $field = 'order_id')
  131. {
  132. if (is_string($orderId))
  133. $orderInfo = self::where($field, $orderId)->find();
  134. else
  135. $orderInfo = $orderId;
  136. if (!$orderInfo || !isset($orderInfo['paid'])) exception('支付订单不存在!');
  137. if ($orderInfo['paid']) exception('支付已支付!');
  138. if ($orderInfo['pay_price'] <= 0) exception('该支付无需支付!');
  139. $site_name = SystemConfigService::get('site_name');
  140. if (!$site_name) exception('支付参数缺少:请前往后台设置->系统设置-> 填写 网站名称');
  141. return WechatService::paymentPrepare(null, $orderInfo['order_id'], $orderInfo['pay_price'], 'testpaper', self::getSubstrUTf8($site_name . '-试卷购买', 30), '', 'MWEB');
  142. }
  143. /**试卷余额支付
  144. * @param $order_id
  145. * @param $uid
  146. * @return bool
  147. * @throws \think\db\exception\DataNotFoundException
  148. * @throws \think\db\exception\ModelNotFoundException
  149. * @throws \think\exception\DbException
  150. */
  151. public static function yueTestPaperPay($order_id, $uid)
  152. {
  153. $orderInfo = self::where('uid', $uid)->where('order_id', $order_id)->where('is_del', 0)->find();
  154. if (!$orderInfo) return self::setErrorInfo('订单不存在!');
  155. if ($orderInfo['paid']) return self::setErrorInfo('该订单已支付!');
  156. if ($orderInfo['pay_type'] != 'yue') return self::setErrorInfo('该订单不能使用余额支付!');
  157. $userInfo = User::getUserData($uid);
  158. if (!$userInfo) return self::setErrorInfo('用户不存在!');
  159. if ($userInfo['now_money'] < $orderInfo['pay_price']) return self::setErrorInfo('余额不足' . floatval($orderInfo['pay_price']));
  160. self::beginTrans();
  161. $res1 = false !== User::bcDec($uid, 'now_money', $orderInfo['pay_price'], 'uid');
  162. $res2 = UserBill::expend('购买试卷', $uid, 'now_money', 'pay_test_paper', $orderInfo['pay_price'], $orderInfo['id'], bcsub($userInfo['now_money'], $orderInfo['pay_price'], 2), '余额支付' . floatval($orderInfo['pay_price']) . '元购买试卷');
  163. $res3 = self::payTestPaperSuccess($order_id);
  164. $res = $res1 && $res2 && $res3;
  165. self::checkTrans($res);
  166. return $res;
  167. }
  168. /**
  169. * //TODO 试卷支付成功后
  170. * @param $orderId
  171. * @param $notify
  172. * @return bool
  173. */
  174. public static function payTestPaperSuccess($orderId)
  175. {
  176. $order = self::where('order_id', $orderId)->where('is_del', 0)->find();
  177. if (!$order) return false;
  178. $res1 = self::where('order_id', $orderId)->where('is_del', 0)->update(['paid' => 1, 'pay_time' => time()]);
  179. $res2 = true;
  180. $res3 = true;
  181. if ($res1) {
  182. try {
  183. if ($order['pay_type'] != 'yue') {
  184. $res2 = UserBill::expend('购买试卷', $order['uid'], $order['pay_type'], 'pay_test_paper', $order['pay_price'], $order['id'], 0, '支付' . floatval($order['pay_price']) . '元购买试卷');
  185. }
  186. $res3 = MerchantFlowingWater::setMerchantFlowingWater($order, 5);
  187. TestPaperObtain::setUserTestPaper($orderId, $order['test_id'], $order['uid'], 2, 1);
  188. } catch (\Throwable $e) {
  189. }
  190. }
  191. $site_url = SystemConfigService::get('site_url');
  192. try {
  193. $wechat_notification_message = SystemConfigService::get('wechat_notification_message');
  194. if ($wechat_notification_message == 1) {
  195. WechatTemplateService::sendTemplate(WechatUser::where('uid', $order['uid'])->value('openid'), WechatTemplateService::ORDER_PAY_SUCCESS, [
  196. 'first' => '亲,您购买的试卷已支付成功',
  197. 'keyword1' => $orderId,
  198. 'keyword2' => $order['pay_price'],
  199. 'remark' => '点击查看订单详情'
  200. ], $site_url . Url::build('wap/topic/question_user'));
  201. WechatTemplateService::sendAdminNoticeTemplate([
  202. 'first' => "亲,您有一个新的试卷购买订单",
  203. 'keyword1' => $orderId,
  204. 'keyword2' => $order['pay_price'],
  205. 'remark' => '请及时处理'
  206. ]);
  207. } else {
  208. $data['character_string1']['value'] = $orderId;
  209. $data['amount3']['value'] = $order['pay_price'];
  210. $data['time2']['value'] = date('Y-m-d H:i:s', time());
  211. $data['thing6']['value'] = '您购买的试卷已支付成功!';
  212. RoutineTemplate::sendOrderSuccess($data, $order['uid'], $site_url . Url::build('wap/topic/question_user'));
  213. $dataAdmin['character_string1']['value'] = $orderId;
  214. $dataAdmin['amount3']['value'] = $order['pay_price'];
  215. $dataAdmin['time2']['value'] = date('Y-m-d H:i:s', time());
  216. $dataAdmin['thing6']['value'] = '您有一个新的试卷购买订单!';
  217. RoutineTemplate::sendAdminNoticeTemplate($dataAdmin);
  218. }
  219. } catch (\Throwable $e) {
  220. }
  221. $res = $res1 && $res2 && $res3;
  222. return false !== $res;
  223. }
  224. }