YzyController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. namespace App\Http\Controllers\Api;
  3. use App\Components\Yzy;
  4. use App\Http\Controllers\Controller;
  5. use App\Http\Models\Coupon;
  6. use App\Http\Models\CouponLog;
  7. use App\Http\Models\Goods;
  8. use App\Http\Models\GoodsLabel;
  9. use App\Http\Models\Order;
  10. use App\Http\Models\Payment;
  11. use App\Http\Models\PaymentCallback;
  12. use App\Http\Models\ReferralLog;
  13. use App\Http\Models\User;
  14. use App\Http\Models\UserLabel;
  15. use Illuminate\Http\Request;
  16. use Log;
  17. use DB;
  18. /**
  19. * 有赞云支付
  20. * Class YzyController
  21. *
  22. * @package App\Http\Controllers
  23. */
  24. class YzyController extends Controller
  25. {
  26. // 接收GET请求
  27. public function index(Request $request)
  28. {
  29. \Log::info("YZY-GET:" . var_export($request->all()));
  30. }
  31. // 接收POST请求
  32. public function store(Request $request)
  33. {
  34. \Log::info("YZY-POST:" . var_export($request->all()));
  35. $json = file_get_contents('php://input');
  36. $data = json_decode($json, true);
  37. if (!$data) {
  38. Log::info('YZY-POST:回调数据无法解析,可能是非法请求');
  39. exit();
  40. }
  41. // 判断消息是否合法
  42. $msg = $data['msg'];
  43. $sign_string = $this->systemConfig['youzan_client_id'] . "" . $msg . "" . $this->systemConfig['youzan_client_secret'];
  44. $sign = md5($sign_string);
  45. if ($sign != $data['sign']) {
  46. Log::info('YZY-POST:回调数据签名错误,可能是非法请求');
  47. exit();
  48. } else {
  49. // 返回请求成功标识给有赞
  50. var_dump(["code" => 0, "msg" => "success"]);
  51. }
  52. // 先写入回调日志
  53. $this->callbackLog($data['client_id'], $data['id'], $data['kdt_id'], $data['kdt_name'], $data['mode'], $data['msg'], $data['sendCount'], $data['sign'], $data['status'], $data['test'], $data['type'], $data['version']);
  54. // msg内容经过 urlencode 编码,进行解码
  55. $msg = json_decode(urldecode($msg), true);
  56. if ($data['type'] == 'TRADE_ORDER_STATE') {
  57. // 读取订单信息
  58. $yzy = new Yzy();
  59. $result = $yzy->getTradeByTid($msg['tid']);
  60. if (isset($result['error_response'])) {
  61. Log::info('【有赞云】回调订单信息错误:' . $result['error_response']['msg']);
  62. exit();
  63. }
  64. $payment = Payment::query()->where('qr_id', $result['response']['trade']['qr_id'])->first();
  65. if (!$payment) {
  66. Log::info('【有赞云】回调订单不存在');
  67. exit();
  68. }
  69. // 等待支付
  70. if ($data['status'] == 'WAIT_BUYER_PAY') {
  71. Log::info('【有赞云】等待支付' . urldecode($data['msg']));
  72. exit();
  73. }
  74. // 交易成功
  75. if ($data['status'] == 'TRADE_SUCCESS') {
  76. if ($payment->status != '0') {
  77. Log::info('【有赞云】回调订单状态不正确');
  78. exit();
  79. }
  80. // 处理订单
  81. DB::beginTransaction();
  82. try {
  83. // 更新支付单
  84. $payment->pay_way = $msg['pay_type'] == '微信支付' ? 1 : 2; // 1-微信、2-支付宝
  85. $payment->status = 1;
  86. $payment->save();
  87. // 更新订单
  88. $order = Order::query()->with(['user'])->where('oid', $payment->oid)->first();
  89. $order->status = 2;
  90. $order->save();
  91. // 优惠券置为已使用
  92. $coupon = Coupon::query()->where('id', $order->coupon_id)->first();
  93. if ($coupon) {
  94. if ($coupon->usage == 1) {
  95. $coupon->status = 1;
  96. $coupon->save();
  97. }
  98. // 写入日志
  99. $couponLog = new CouponLog();
  100. $couponLog->coupon_id = $coupon->id;
  101. $couponLog->goods_id = $order->goods_id;
  102. $couponLog->order_id = $order->oid;
  103. $couponLog->save();
  104. }
  105. // 如果买的是套餐,则先将之前购买的所有套餐置都无效,并扣掉之前所有套餐的流量
  106. $goods = Goods::query()->where('id', $order->goods_id)->first();
  107. if ($goods->type == 2) {
  108. $existOrderList = Order::query()
  109. ->with(['goods'])
  110. ->whereHas('goods', function ($q) {
  111. $q->where('type', 2);
  112. })
  113. ->where('user_id', $order->user_id)
  114. ->where('oid', '<>', $order->oid)
  115. ->where('is_expire', 0)
  116. ->get();
  117. foreach ($existOrderList as $vo) {
  118. Order::query()->where('oid', $vo->oid)->update(['is_expire' => 1]);
  119. User::query()->where('id', $order->user_id)->decrement('transfer_enable', $vo->goods->traffic * 1048576);
  120. }
  121. }
  122. // 把商品的流量加到账号上
  123. User::query()->where('id', $order->user_id)->increment('transfer_enable', $goods->traffic * 1048576);
  124. // 套餐就改流量重置日,流量包不改
  125. if ($goods->type == 2) {
  126. // 将商品的有效期和流量自动重置日期加到账号上
  127. $traffic_reset_day = in_array(date('d'), [29, 30, 31]) ? 28 : abs(date('d'));
  128. User::query()->where('id', $order->user_id)->update(['traffic_reset_day' => $traffic_reset_day, 'expire_time' => date('Y-m-d', strtotime("+" . $goods->days . " days", strtotime($order->user->expire_time))), 'enable' => 1]);
  129. } else {
  130. // 将商品的有效期和流量自动重置日期加到账号上
  131. User::query()->where('id', $order->user_id)->update(['expire_time' => date('Y-m-d', strtotime("+" . $goods->days . " days")), 'enable' => 1]);
  132. }
  133. // 写入用户标签
  134. if ($goods->label) {
  135. // 取出现有的标签
  136. $userLabels = UserLabel::query()->where('user_id', $order->user_id)->pluck('label_id')->toArray();
  137. $goodsLabels = GoodsLabel::query()->where('goods_id', $order->goods_id)->pluck('label_id')->toArray();
  138. $newUserLabels = array_merge($userLabels, $goodsLabels);
  139. // 删除用户所有标签
  140. UserLabel::query()->where('user_id', $order->user_id)->delete();
  141. // 生成标签
  142. foreach ($newUserLabels as $vo) {
  143. $obj = new UserLabel();
  144. $obj->user_id = $order->user_id;
  145. $obj->label_id = $vo;
  146. $obj->save();
  147. }
  148. }
  149. // 写入返利日志
  150. if ($order->user->referral_uid) {
  151. $referralLog = new ReferralLog();
  152. $referralLog->user_id = $order->user_id;
  153. $referralLog->ref_user_id = $order->user->referral_uid;
  154. $referralLog->order_id = $order->oid;
  155. $referralLog->amount = $order->amount;
  156. $referralLog->ref_amount = $order->amount * $this->systemConfig['referral_percent'];
  157. $referralLog->status = 0;
  158. $referralLog->save();
  159. }
  160. DB::commit();
  161. } catch (\Exception $e) {
  162. DB::rollBack();
  163. Log::info('【有赞云】更新支付单和订单异常:' . $e->getMessage());
  164. }
  165. exit();
  166. }
  167. // 超时自动关闭订单
  168. if ($data['status'] == 'TRADE_CLOSED') {
  169. if ($payment->status != 0) {
  170. Log::info('【有赞云】自动关闭支付单异常,本地支付单状态不正确');
  171. exit();
  172. }
  173. $order = Order::query()->where('oid', $payment->oid)->first();
  174. if ($order->status != 0) {
  175. Log::info('【有赞云】自动关闭支付单异常,本地订单状态不正确');
  176. exit();
  177. }
  178. DB::beginTransaction();
  179. try {
  180. // 关闭支付单
  181. $payment->status = -1;
  182. $payment->save();
  183. // 关闭订单
  184. $order->status = -1;
  185. $order->save();
  186. DB::commit();
  187. } catch (\Exception $e) {
  188. DB::rollBack();
  189. Log::info('【有赞云】更新支付单和订单异常');
  190. }
  191. exit();
  192. }
  193. }
  194. if ($data['type'] == 'TRADE') {
  195. if ($data['status'] == 'WAIT_BUYER_PAY') {
  196. Log::info('【有赞云】等待支付' . urldecode($data['msg']));
  197. exit();
  198. }
  199. if ($data['status'] == 'TRADE_SUCCESS') {
  200. Log::info('【有赞云】支付成功' . urldecode($data['msg']));
  201. exit();
  202. }
  203. // 用户已签收
  204. if ($data['status'] == 'TRADE_BUYER_SIGNED') {
  205. Log::info('【有赞云】用户已签收' . urldecode($data['msg']));
  206. exit();
  207. }
  208. if ($data['status'] == 'TRADE_CLOSED') {
  209. Log::info('【有赞云】超时未支付自动支付' . urldecode($data['msg']));
  210. exit();
  211. }
  212. }
  213. exit();
  214. }
  215. public function show(Request $request)
  216. {
  217. exit('show');
  218. }
  219. // 写入回调请求日志
  220. private function callbackLog($client_id, $yz_id, $kdt_id, $kdt_name, $mode, $msg, $sendCount, $sign, $status, $test, $type, $version)
  221. {
  222. $paymentCallback = new PaymentCallback();
  223. $paymentCallback->client_id = $client_id;
  224. $paymentCallback->yz_id = $yz_id;
  225. $paymentCallback->kdt_id = $kdt_id;
  226. $paymentCallback->kdt_name = $kdt_name;
  227. $paymentCallback->mode = $mode;
  228. $paymentCallback->msg = urldecode($msg);
  229. $paymentCallback->sendCount = $sendCount;
  230. $paymentCallback->sign = $sign;
  231. $paymentCallback->status = $status;
  232. $paymentCallback->test = $test;
  233. $paymentCallback->type = $type;
  234. $paymentCallback->version = $version;
  235. $paymentCallback->save();
  236. return $paymentCallback->id;
  237. }
  238. }