YzyController.php 11 KB

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