PayNoticeController.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <?php
  2. namespace App\Http\Controllers\V1;
  3. use App\Helper\ByteDance;
  4. use App\Helper\UniPlatform\Bytedance\Payment as BytedancePayment;
  5. use App\Helper\UniPlatform\Kuaishou\Payment as KuaishouPayment;
  6. use App\Models\Pay;
  7. use App\Models\ShareConfig;
  8. use App\Models\User;
  9. use App\Models\UserConsumeRecord;
  10. use App\Models\UserInfo;
  11. use App\Models\UserRechargeRecord;
  12. use App\Models\UserShare;
  13. use App\Models\UserVipRecord;
  14. use Carbon\Carbon;
  15. use Illuminate\Http\JsonResponse;
  16. use Illuminate\Support\Facades\Log;
  17. use Symfony\Component\HttpFoundation\Response;
  18. class PayNoticeController extends Controller
  19. {
  20. public function bytedance(): JsonResponse
  21. {
  22. $factory = $this->getUniFactory();
  23. $app = new BytedancePayment($factory);
  24. return $app->payNotify(function ($data, $fail) use ($factory) {
  25. //处理支付订单
  26. try {
  27. \DB::beginTransaction();
  28. $payId = $data['cp_orderno'];
  29. $pay = Pay::find($payId);
  30. if (!$pay) { // 如果订单不存在
  31. return true;
  32. }
  33. $pay->pay_type = $data['way'];
  34. $pay->serial_number = $data['channel_no'];
  35. $pay->status = $data['status'] == 'SUCCESS';
  36. $pay->pay_dt = Carbon::now()->toDateTimeString();
  37. $pay->save();
  38. // 处理
  39. if ($pay->source == Pay::SOURCE_RECHARGE) {
  40. $goods = $this->recharge($payId);
  41. } else if ($pay->source == Pay::SOURCE_BUY_VIP) {
  42. $goods = $this->buyVip($payId);
  43. }
  44. if(isset($goods)){
  45. /* @var Pay $payInfo*/
  46. $payInfo = Pay::with(['user'])->where('pay_id', $payId)->first();
  47. $factory->pushOrder($payInfo->user->open_id, $payId, $goods ,'已支付');
  48. }
  49. \DB::commit();
  50. // 成为分销商
  51. (new User())->beComeShare($pay->user_id);
  52. } catch (\Exception $e) {
  53. \DB::rollBack();
  54. \Log::error('抖音支付回调错误==>'.$e->getMessage());
  55. return $fail('通信失败,请稍后再通知我');
  56. }
  57. return true;
  58. });
  59. }
  60. public function kuaishou(): JsonResponse
  61. {
  62. $factory = $this->getUniFactory(2);
  63. $app = new KuaishouPayment($factory);
  64. return $app->payNotify(function ($data, $fail) use($factory) {
  65. //处理支付订单
  66. try {
  67. \DB::beginTransaction();
  68. $payId = $data['out_order_no'];
  69. $pay = Pay::find($payId);
  70. if (!$pay) { // 如果订单不存在
  71. return true;
  72. }
  73. $payType = 11;
  74. if($data['channel'] == 'WECHAT'){
  75. $payType = 1;
  76. }
  77. if($data['channel'] == 'ALIPAY'){
  78. $payType = 2;
  79. }
  80. $pay->pay_type = $payType;
  81. $pay->serial_number = $data['trade_no'];
  82. $pay->status = $data['status'] == 'SUCCESS';
  83. $pay->pay_dt = Carbon::now()->toDateTimeString();
  84. $pay->save();
  85. // 处理
  86. if ($pay->source == Pay::SOURCE_RECHARGE) {
  87. $goods = $this->recharge($payId);
  88. } else if ($pay->source == Pay::SOURCE_BUY_VIP) {
  89. $goods = $this->buyVip($payId);
  90. }
  91. if(isset($goods)){
  92. /* @var Pay $payInfo*/
  93. $payInfo = Pay::with(['user'])->where('pay_id', $payId)->first();
  94. $factory->pushOrder($payInfo->user->open_id, $payId);
  95. }
  96. \DB::commit();
  97. // 成为分销商
  98. (new User())->beComeShare($pay->user_id);
  99. } catch (\Exception $e) {
  100. \DB::rollBack();
  101. \Log::error('快手支付回调错误==>'.$e->getMessage());
  102. return $fail('通信失败,请稍后再通知我');
  103. }
  104. return true;
  105. });
  106. }
  107. public function kuaishouSettle()
  108. {
  109. $app = new KuaishouPayment($this->getUniFactory(2));
  110. return $app->payNotify(function ($data, $fail) {
  111. return true;
  112. });
  113. }
  114. /**
  115. * @return Response
  116. * @throws \EasyWeChat\Kernel\Exceptions\Exception
  117. */
  118. public function wechat(): Response
  119. {
  120. $factory = $this->getUniFactory(3)->payment();
  121. return $factory->handlePaidNotify(function ($message, $fail){
  122. \Log::info('微信支付回调==>'.json_encode($message,JSON_UNESCAPED_SLASHES));
  123. //处理支付订单
  124. try {
  125. \DB::beginTransaction();
  126. $payId = $message['out_trade_no'];
  127. $pay = Pay::find($payId);
  128. if (!$pay) { // 如果订单不存在
  129. return true;
  130. }
  131. if ($message['return_code'] === 'SUCCESS') {
  132. if ($message['result_code'] === 'SUCCESS') {
  133. $pay->status = 1;
  134. $pay->serial_number = $message['transaction_id'];
  135. }elseif ($message['result_code'] === 'FAIL') {
  136. $pay->status = -1;
  137. }
  138. } else {
  139. return $fail('通信失败,请稍后再通知我');
  140. }
  141. $pay->pay_dt = Carbon::now()->toDateTimeString(); // 更新支付时间为当前时间
  142. $pay->save();
  143. // 处理
  144. if ($pay->source == Pay::SOURCE_RECHARGE) {
  145. $this->recharge($payId);
  146. } else if ($pay->source == Pay::SOURCE_BUY_VIP) {
  147. $this->buyVip($payId);
  148. }
  149. \DB::commit();
  150. // 成为分销商
  151. (new User())->beComeShare($pay->user_id);
  152. } catch (\Exception $e) {
  153. \DB::rollBack();
  154. \Log::error('微信支付回调错误==>'.$e->getMessage());
  155. return $fail('通信失败,请稍后再通知我');
  156. }
  157. return true;
  158. });
  159. }
  160. private function recharge($payId)
  161. {
  162. /* @var UserRechargeRecord $record */
  163. $record = UserRechargeRecord::with(['combo'])->where('pay_id', $payId)->first();
  164. if (!$record) {
  165. return true;
  166. }
  167. $user = UserInfo::find($record->user_id);
  168. // 记录日志
  169. $gold = $record->gold + $record->gift;
  170. app(UserConsumeRecord::class)->record(
  171. $user->user_id,
  172. UserConsumeRecord::TYPE_RECHARGE,
  173. $gold,
  174. "购买金币,combo_id:{$record->combo_id}"
  175. );
  176. // 发放奖励
  177. $user->integral = $user->integral + $gold;
  178. $user->total_integral = $user->total_integral + $gold;
  179. $user->save();
  180. $record->status = 1;
  181. $record->save();
  182. // 分销
  183. $this->share($record ,2);
  184. return [
  185. 'id' => $record->combo_id,
  186. 'title' => $record->combo->name,
  187. 'price' => $record->combo->price,
  188. 'img' => '',
  189. ];
  190. }
  191. private function buyVip($payId)
  192. {
  193. /* @var UserVipRecord $record */
  194. $record = UserVipRecord::with(['combo'])->where('pay_id', $payId)->first();
  195. if (!$record) {
  196. return true;
  197. }
  198. $user = UserInfo::find($record->user_id);
  199. if (empty($user->is_vip)) {
  200. $user->is_vip = 1;
  201. $user->start_at = Carbon::now()->toDateString();
  202. $user->end_at = Carbon::now()->addDays($record->valid_day)->toDateString();
  203. } else {
  204. list($year, $month, $day) = explode('-', $user->end_at);
  205. $user->end_at = Carbon::createFromDate($year, $month, $day)->addDays($record->valid_day)->toDateString();
  206. }
  207. $user->save();
  208. $record->status = 1;
  209. $record->save();
  210. // 分销
  211. $this->share($record,1);
  212. return [
  213. 'id' => $record->combo_id,
  214. 'title' => $record->combo->name,
  215. 'price' => $record->combo->price,
  216. 'img' => '',
  217. ];
  218. }
  219. /**
  220. * @param UserVipRecord | UserRechargeRecord $record
  221. * @param $type
  222. * @return void
  223. */
  224. private function share($record ,$type)
  225. {
  226. $user = User::find($record->user_id);
  227. // 一级分销
  228. if($user->parent_id){
  229. $config = ShareConfig::first();
  230. $pay = Pay::find($record->pay_id);
  231. if($config->share_type == 1){ // 百分比
  232. $income = $pay->order_fee * $config->share_number / 100;
  233. }else{ // 固定
  234. $income = $config->share_number;
  235. }
  236. $share = new UserShare();
  237. $share->user_id = $user->parent_id;
  238. $share->child_id = $user->id;
  239. $share->order_type = $type;
  240. $share->order_id = $record->pay_id;
  241. $share->income = $income;
  242. $share->save();
  243. $parent = User::find($user->parent_id);
  244. $parent->income = $user->income + $income;
  245. $parent->total_income = $user->total_income + $income;
  246. $parent->save();
  247. }
  248. }
  249. }