OrderService.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * Author: luzheng.liu
  4. * Time: 2020/12/24 13:44
  5. */
  6. namespace app\common\service;
  7. use app\api\controller\Order;
  8. use app\api\exception\ApiException;
  9. use app\api\model\OrderModel;
  10. use app\api\model\ProductModel;
  11. use app\api\model\UserModel;
  12. use app\api\model\WriteOffModel;
  13. use app\common\until\Until;
  14. use GuzzleHttp\Client;
  15. use think\facade\Cache;
  16. use think\facade\Config;
  17. use const http\Client\Curl\Features\UNIX_SOCKETS;
  18. class OrderService {
  19. public $userName = 'heshen';
  20. public $passwd = 'heshenit@0563';
  21. public function payOrder($input) {
  22. $orderSn = Until::createSn();
  23. $productInfo = (new ProductModel())::where(['id' => $input['productId']])->find();
  24. $userInfo = (new UserModel())::where(['id' => Until::$userId])->find();
  25. if (empty($userInfo)) {
  26. throw new ApiException('无此用户');
  27. }
  28. $model = new OrderModel();
  29. try {
  30. $userId = Until::$userId;
  31. $orderType = 1;
  32. if (Until::$isAdmin) {
  33. $userId = 0;
  34. $orderType = 2;
  35. }
  36. $model->startTrans();
  37. $orderMoney = $productInfo['current_price'] * (int)abs($input['num']);
  38. $orderId = $model->insertGetId([
  39. 'order_sn' => $orderSn,
  40. 'order_money' => $orderMoney,
  41. 'product_id' => $input['productId'],
  42. 'store_id' => $input['storeId'],
  43. 'appointment_time' => $input['appointmentTime'],
  44. 'end_time' => $input['endTime'],
  45. 'mobile' => $input['mobile'],
  46. 'status' => 1,
  47. 'order_type' => $orderType,
  48. 'user_id' => $userId,
  49. ]);
  50. (new WriteOffModel())->insertGetId([
  51. 'write_off_code' => '',
  52. 'order_id' => $orderId,
  53. 'write_off_status' => 1,
  54. ]);
  55. $model->commit();
  56. } catch (\Exception $e) {
  57. $model->rollback();
  58. throw new ApiException($e->getMessage());
  59. }
  60. //{attach string支付主体的标识
  61. //mark string商品信息
  62. //money integer($int32)金额,分为单位
  63. //openId string小程序的openid
  64. //orderId string订单号
  65. //payCode string支付标识
  66. //username string用户名
  67. //}
  68. $otherData = [
  69. 'openId' => $userInfo['open_id'],
  70. 'attach' => 'storeId:'.$input['storeId'],
  71. 'money' => $orderMoney * 100,
  72. 'mark' => '购买了'.$productInfo['product_name'],
  73. 'orderId' => $orderSn,
  74. 'username' => $userInfo['name'],
  75. 'payCode' => strtoupper($this->userName),
  76. ];
  77. $wxOrderInfo = $this->createOrderForOther($otherData);
  78. $order = [
  79. 'nonceStr' => $wxOrderInfo['nonceStr'],
  80. 'package' => $wxOrderInfo['pack'],
  81. 'paySign' => $wxOrderInfo['paySign'],
  82. 'timeStamp' => $wxOrderInfo['timeStamp'],
  83. 'signType' => $wxOrderInfo['signType'],
  84. 'orderSn' => $orderSn,
  85. 'orderId' => $orderId
  86. ];
  87. return $order;
  88. }
  89. public function payAgain(int $orderId) {
  90. $model = new OrderModel();
  91. $info = $model::where(['id' => $orderId])->find();
  92. $productInfo = (new ProductModel())::where(['id' => $info['product_id']])->find();
  93. if ($info['status'] === OrderModel::IS_PAY) {
  94. throw new ApiException('订单已支付');
  95. }
  96. $userInfo = (new UserModel())::where(['id' => Until::$userId])->find();
  97. if (empty($userInfo)) {
  98. throw new ApiException('无此用户');
  99. }
  100. $otherData = [
  101. 'openId' => $userInfo['open_id'],
  102. 'attach' => 'storeId:'.$info['store_id'],
  103. 'money' => $info['order_money'] * $info['num'] * 100,
  104. 'mark' => '购买了'.$productInfo['product_name'] ?? '',
  105. 'orderId' => $info['order_sn'],
  106. 'username' => $userInfo['name'],
  107. 'payCode' => strtoupper($this->userName),
  108. ];
  109. $wxOrderInfo = $this->createOrderForOther($otherData);
  110. $order = [
  111. 'nonceStr' => $wxOrderInfo['nonceStr'],
  112. 'package' => $wxOrderInfo['pack'],
  113. 'paySign' => $wxOrderInfo['paySign'],
  114. 'timeStamp' => $wxOrderInfo['timeStamp'],
  115. 'signType' => $wxOrderInfo['signType'],
  116. 'orderSn' => $info['order_sn'],
  117. 'orderId' => $orderId
  118. ];
  119. return $order;
  120. }
  121. public function getPayToken() {
  122. $key = 'payToken' . $this->userName;
  123. $token = Cache::get($key);
  124. $token = '';
  125. if (empty($token)) {
  126. $token = $this->getAuthPay();
  127. Cache::set($key, $token,'7100');
  128. return $token;
  129. }
  130. return $token;
  131. }
  132. public function getAuthPay() {
  133. $client = new Client();
  134. $time = Until::msectime();
  135. $res = $client->request('Post',
  136. Config::get('domain')."api/auth/create", [
  137. 'json' => [
  138. 'username' => $this->userName,
  139. "password" => $this->passwd,
  140. 'timestamp' => $time,
  141. 'sign' => md5($this->userName . $time . $this->passwd)
  142. ]
  143. ]);
  144. $info = json_decode((string)$res->getBody(), true);
  145. if ($info['code'] != 200) {
  146. throw new ApiException($info['msg']);
  147. }
  148. return $info['data']['token'];
  149. }
  150. public function createOrderForOther(array $data) {
  151. $client = new Client();
  152. $res = $client->request('Post',
  153. Config::get('domain')."api/supers/wx-pay/gateway-no-order", [
  154. 'json' => $data,
  155. 'headers' => [
  156. 'API-TOKEN-V1' => $this->getPayToken()
  157. ]
  158. ]);
  159. $info = json_decode((string)$res->getBody(), true);
  160. if ($info['code'] != 200) {
  161. throw new ApiException($info['msg']);
  162. }
  163. return $info['data'];
  164. }
  165. public function notify(string $orderSn) {
  166. $model = new OrderModel();
  167. $rs = $model::where(['order_sn' => $orderSn,'status' => 2])->find();
  168. if (!empty($rs)) {
  169. return;
  170. }
  171. $model::where(['order_sn' => $orderSn])->update(['status' => 2,'pay_time' => date('Y-m-d H:i:s')]);
  172. }
  173. }