123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103 |
- <?php
- namespace App\Http\Controllers\V1;
- use App\Helper\Bytedance\Payment;
- use App\Models\Pay;
- use App\Models\UserConsumeRecord;
- use App\Models\UserInfo;
- use App\Models\UserRechargeRecord;
- use App\Models\UserVipRecord;
- use Carbon\Carbon;
- class PayNoticeController extends Controller
- {
- public function bytedance()
- {
- $app = new Payment($this->getByteDanceFactory());
- return $app->payNotify(function ($data, $fail) {
- //处理支付订单
- try {
- \DB::beginTransaction();
- $payId = $data['cp_orderno'];
- $pay = Pay::find($payId);
- if (!$pay) { // 如果订单不存在
- return true;
- }
- $pay->pay_type = $data['way'];
- $pay->serial_number = $data['channel_no'];
- $pay->status = $data['status'] == 'SUCCESS';
- $pay->pay_dt = Carbon::now()->toDateString();
- $pay->save();
- // 处理
- if ($pay->source == Pay::SOURCE_RECHARGE) {
- $this->recharge($payId);
- } else if ($pay->source == Pay::SOURCE_BUY_VIP) {
- $this->buyVip($payId);
- }
- \DB::commit();
- } catch (\Exception $e) {
- \DB::rollBack();
- return $fail('通信失败,请稍后再通知我');
- }
- return true;
- });
- }
- private function recharge($payId)
- {
- /* @var UserRechargeRecord $record */
- $record = UserRechargeRecord::where('pay_id', $payId)->first();
- if (!$record) {
- return true;
- }
- $user = UserInfo::find($record->user_id);
- // 记录日志
- $gold = $record->gold + $record->gift;
- app(UserConsumeRecord::class)->record(
- $user->user_id,
- UserConsumeRecord::TYPE_RECHARGE,
- $gold,
- "购买金币,combo_id:{$record->combo_id}"
- );
- // 发放奖励
- $user->integral = $user->integral + $gold;
- $user->total_integral = $user->total_integral + $gold;
- $user->save();
- $record->status = 1;
- $record->save();
- }
- private function buyVip($payId)
- {
- /* @var UserVipRecord $record */
- $record = UserVipRecord::where('pay_id', $payId)->first();
- if (!$record) {
- return true;
- }
- $user = UserInfo::find($record->user_id);
- if (empty($user->is_vip)) {
- $user->is_vip = 1;
- $user->start_at = Carbon::now()->toDateString();
- $user->end_at = Carbon::now()->addDays($record->valid_day)->toDateString();
- } else {
- list($year, $month, $day) = explode('-', $user->end_at);
- $user->end_at = Carbon::createFromDate($year, $month, $day)->addDays($record->valid_day)->toDateString();
- }
- $user->save();
- $record->status = 1;
- $record->save();
- }
- }
|