PayNoticeController.php 8.5 KB

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