YzyController.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. $userLabels = UserLabel::query()->where('user_id', $order->user_id)->pluck('label_id')->toArray();
  121. $goodsLabels = GoodsLabel::query()->where('goods_id', $order->goods_id)->pluck('label_id')->toArray();
  122. $newUserLabels = array_merge($userLabels, $goodsLabels);
  123. // 删除用户所有标签
  124. UserLabel::query()->where('user_id', $order->user_id)->delete();
  125. // 生成标签
  126. foreach ($newUserLabels as $vo) {
  127. $obj = new UserLabel();
  128. $obj->user_id = $order->user_id;
  129. $obj->label_id = $vo;
  130. $obj->save();
  131. }
  132. }
  133. // 写入返利日志
  134. if ($order->user->referral_uid) {
  135. $referralLog = new ReferralLog();
  136. $referralLog->user_id = $order->user_id;
  137. $referralLog->ref_user_id = $order->user->referral_uid;
  138. $referralLog->order_id = $order->oid;
  139. $referralLog->amount = $order->amount;
  140. $referralLog->ref_amount = $order->amount * $this->systemConfig['referral_percent'];
  141. $referralLog->status = 0;
  142. $referralLog->save();
  143. }
  144. DB::commit();
  145. } catch (\Exception $e) {
  146. DB::rollBack();
  147. Log::info('【有赞云】更新支付单和订单异常:' . $e->getMessage());
  148. }
  149. exit();
  150. }
  151. // 超时自动关闭订单
  152. if ($data['status'] == 'TRADE_CLOSED') {
  153. if ($payment->status != 0) {
  154. Log::info('【有赞云】自动关闭支付单异常,本地支付单状态不正确');
  155. exit();
  156. }
  157. $order = Order::query()->where('oid', $payment->oid)->first();
  158. if ($order->status != 0) {
  159. Log::info('【有赞云】自动关闭支付单异常,本地订单状态不正确');
  160. exit();
  161. }
  162. DB::beginTransaction();
  163. try {
  164. // 关闭支付单
  165. $payment->status = -1;
  166. $payment->save();
  167. // 关闭订单
  168. $order->status = -1;
  169. $order->save();
  170. DB::commit();
  171. } catch (\Exception $e) {
  172. DB::rollBack();
  173. Log::info('【有赞云】更新支付单和订单异常');
  174. }
  175. exit();
  176. }
  177. }
  178. if ($data['type'] == 'TRADE') {
  179. if ($data['status'] == 'WAIT_BUYER_PAY') {
  180. Log::info('【有赞云】等待支付' . urldecode($data['msg']));
  181. exit();
  182. }
  183. if ($data['status'] == 'TRADE_SUCCESS') {
  184. Log::info('【有赞云】支付成功' . urldecode($data['msg']));
  185. exit();
  186. }
  187. // 用户已签收
  188. if ($data['status'] == 'TRADE_BUYER_SIGNED') {
  189. Log::info('【有赞云】用户已签收' . urldecode($data['msg']));
  190. exit();
  191. }
  192. if ($data['status'] == 'TRADE_CLOSED') {
  193. Log::info('【有赞云】超时未支付自动支付' . urldecode($data['msg']));
  194. exit();
  195. }
  196. }
  197. exit();
  198. }
  199. public function show(Request $request)
  200. {
  201. exit('show');
  202. }
  203. // 写入回调请求日志
  204. private function callbackLog($client_id, $yz_id, $kdt_id, $kdt_name, $mode, $msg, $sendCount, $sign, $status, $test, $type, $version)
  205. {
  206. $paymentCallback = new PaymentCallback();
  207. $paymentCallback->client_id = $client_id;
  208. $paymentCallback->yz_id = $yz_id;
  209. $paymentCallback->kdt_id = $kdt_id;
  210. $paymentCallback->kdt_name = $kdt_name;
  211. $paymentCallback->mode = $mode;
  212. $paymentCallback->msg = urldecode($msg);
  213. $paymentCallback->sendCount = $sendCount;
  214. $paymentCallback->sign = $sign;
  215. $paymentCallback->status = $status;
  216. $paymentCallback->test = $test;
  217. $paymentCallback->type = $type;
  218. $paymentCallback->version = $version;
  219. $paymentCallback->save();
  220. return $paymentCallback->id;
  221. }
  222. }