PayNoticeController.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. $app = new KuaishouPayment($this->getUniFactory(2));
  63. return $app->payNotify(function ($data, $fail) {
  64. //处理支付订单
  65. try {
  66. \DB::beginTransaction();
  67. $payId = $data['out_order_no'];
  68. $pay = Pay::find($payId);
  69. if (!$pay) { // 如果订单不存在
  70. return true;
  71. }
  72. $payType = 11;
  73. if($data['channel'] == 'WECHAT'){
  74. $payType = 1;
  75. }
  76. if($data['channel'] == 'ALIPAY'){
  77. $payType = 2;
  78. }
  79. $pay->pay_type = $payType;
  80. $pay->serial_number = $data['trade_no'];
  81. $pay->status = $data['status'] == 'SUCCESS';
  82. $pay->pay_dt = Carbon::now()->toDateTimeString();
  83. $pay->save();
  84. // 处理
  85. if ($pay->source == Pay::SOURCE_RECHARGE) {
  86. $this->recharge($payId);
  87. } else if ($pay->source == Pay::SOURCE_BUY_VIP) {
  88. $this->buyVip($payId);
  89. }
  90. \DB::commit();
  91. // 成为分销商
  92. (new User())->beComeShare($pay->user_id);
  93. } catch (\Exception $e) {
  94. \DB::rollBack();
  95. \Log::error('快手支付回调错误==>'.$e->getMessage());
  96. return $fail('通信失败,请稍后再通知我');
  97. }
  98. return true;
  99. });
  100. }
  101. /**
  102. * @return Response
  103. * @throws \EasyWeChat\Kernel\Exceptions\Exception
  104. */
  105. public function wechat(): Response
  106. {
  107. $factory = $this->getUniFactory(3)->payment();
  108. return $factory->handlePaidNotify(function ($message, $fail){
  109. \Log::info('微信支付回调==>'.json_encode($message,JSON_UNESCAPED_SLASHES));
  110. //处理支付订单
  111. try {
  112. \DB::beginTransaction();
  113. $payId = $message['out_trade_no'];
  114. $pay = Pay::find($payId);
  115. if (!$pay) { // 如果订单不存在
  116. return true;
  117. }
  118. if ($message['return_code'] === 'SUCCESS') {
  119. if ($message['result_code'] === 'SUCCESS') {
  120. $pay->status = 1;
  121. $pay->serial_number = $message['transaction_id'];
  122. }elseif ($message['result_code'] === 'FAIL') {
  123. $pay->status = -1;
  124. }
  125. } else {
  126. return $fail('通信失败,请稍后再通知我');
  127. }
  128. $pay->pay_dt = Carbon::now()->toDateTimeString(); // 更新支付时间为当前时间
  129. $pay->save();
  130. // 处理
  131. if ($pay->source == Pay::SOURCE_RECHARGE) {
  132. $this->recharge($payId);
  133. } else if ($pay->source == Pay::SOURCE_BUY_VIP) {
  134. $this->buyVip($payId);
  135. }
  136. \DB::commit();
  137. // 成为分销商
  138. (new User())->beComeShare($pay->user_id);
  139. } catch (\Exception $e) {
  140. \DB::rollBack();
  141. \Log::error('微信支付回调错误==>'.$e->getMessage());
  142. return $fail('通信失败,请稍后再通知我');
  143. }
  144. return true;
  145. });
  146. }
  147. private function recharge($payId)
  148. {
  149. /* @var UserRechargeRecord $record */
  150. $record = UserRechargeRecord::with(['combo'])->where('pay_id', $payId)->first();
  151. if (!$record) {
  152. return true;
  153. }
  154. $user = UserInfo::find($record->user_id);
  155. // 记录日志
  156. $gold = $record->gold + $record->gift;
  157. app(UserConsumeRecord::class)->record(
  158. $user->user_id,
  159. UserConsumeRecord::TYPE_RECHARGE,
  160. $gold,
  161. "购买金币,combo_id:{$record->combo_id}"
  162. );
  163. // 发放奖励
  164. $user->integral = $user->integral + $gold;
  165. $user->total_integral = $user->total_integral + $gold;
  166. $user->save();
  167. $record->status = 1;
  168. $record->save();
  169. // 分销
  170. $this->share($record ,2);
  171. return [
  172. 'id' => $record->combo_id,
  173. 'title' => $record->combo->name,
  174. 'price' => $record->combo->price,
  175. 'img' => '',
  176. ];
  177. }
  178. private function buyVip($payId)
  179. {
  180. /* @var UserVipRecord $record */
  181. $record = UserVipRecord::with(['combo'])->where('pay_id', $payId)->first();
  182. if (!$record) {
  183. return true;
  184. }
  185. $user = UserInfo::find($record->user_id);
  186. if (empty($user->is_vip)) {
  187. $user->is_vip = 1;
  188. $user->start_at = Carbon::now()->toDateString();
  189. $user->end_at = Carbon::now()->addDays($record->valid_day)->toDateString();
  190. } else {
  191. list($year, $month, $day) = explode('-', $user->end_at);
  192. $user->end_at = Carbon::createFromDate($year, $month, $day)->addDays($record->valid_day)->toDateString();
  193. }
  194. $user->save();
  195. $record->status = 1;
  196. $record->save();
  197. // 分销
  198. $this->share($record,1);
  199. return [
  200. 'id' => $record->combo_id,
  201. 'title' => $record->combo->name,
  202. 'price' => $record->combo->price,
  203. 'img' => '',
  204. ];
  205. }
  206. /**
  207. * @param UserVipRecord | UserRechargeRecord $record
  208. * @param $type
  209. * @return void
  210. */
  211. private function share($record ,$type)
  212. {
  213. $user = User::find($record->user_id);
  214. // 一级分销
  215. if($user->parent_id){
  216. $config = ShareConfig::first();
  217. $pay = Pay::find($record->pay_id);
  218. if($config->share_type == 1){ // 百分比
  219. $income = $pay->order_fee * $config->share_number / 100;
  220. }else{ // 固定
  221. $income = $config->share_number;
  222. }
  223. $share = new UserShare();
  224. $share->user_id = $user->parent_id;
  225. $share->child_id = $user->id;
  226. $share->order_type = $type;
  227. $share->order_id = $record->pay_id;
  228. $share->income = $income;
  229. $share->save();
  230. $user->income = $user->income + $income;
  231. $user->total_income = $user->total_income + $income;
  232. $user->save();
  233. }
  234. }
  235. }