YzyController.php 9.8 KB

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