123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?php
- namespace App\Http\Controllers\V1;
- use App\Helper\ByteDance;
- use App\Helper\UniPlatform\Bytedance\Payment as BytedancePayment;
- use App\Helper\UniPlatform\Kuaishou\Payment as KuaishouPayment;
- use App\Models\Pay;
- use App\Models\UserConsumeRecord;
- use App\Models\UserInfo;
- use App\Models\UserRechargeRecord;
- use App\Models\UserVipRecord;
- use Carbon\Carbon;
- use Illuminate\Support\Facades\Log;
- class PayNoticeController extends Controller
- {
- public function bytedance()
- {
- $factory = $this->getUniFactory();
- $app = new BytedancePayment($factory);
- return $app->payNotify(function ($data, $fail) use ($factory) {
- //处理支付订单
- 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()->toDateTimeString();
- $pay->save();
- // 处理
- if ($pay->source == Pay::SOURCE_RECHARGE) {
- $goods = $this->recharge($payId);
- } else if ($pay->source == Pay::SOURCE_BUY_VIP) {
- $goods = $this->buyVip($payId);
- }
- if(isset($goods)){
- /* @var Pay $payInfo*/
- $payInfo = Pay::with(['user'])->where('id', $payId)->first($payId);
- $factory->pushOrder($payInfo->user->open_id, $payId, $goods ,'已支付');
- }
- \DB::commit();
- } catch (\Exception $e) {
- \DB::rollBack();
- \Log::error('抖音支付回调错误==>'.$e->getMessage());
- return $fail('通信失败,请稍后再通知我');
- }
- return true;
- });
- }
- public function kuaishou()
- {
- $app = new KuaishouPayment($this->getUniFactory(2));
- return $app->payNotify(function ($data, $fail) {
- //处理支付订单
- try {
- \DB::beginTransaction();
- $payId = $data['out_order_no'];
- $pay = Pay::find($payId);
- if (!$pay) { // 如果订单不存在
- return true;
- }
- $payType = 11;
- if($data['channel'] == 'WECHAT'){
- $payType = 1;
- }
- if($data['channel'] == 'ALIPAY'){
- $payType = 2;
- }
- $pay->pay_type = $payType;
- $pay->serial_number = $data['trade_no'];
- $pay->status = $data['status'] == 'SUCCESS';
- $pay->pay_dt = Carbon::now()->toDateTimeString();
- $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();
- \Log::error('快手支付回调错误==>'.$e->getMessage());
- return $fail('通信失败,请稍后再通知我');
- }
- return true;
- });
- }
- private function recharge($payId)
- {
- /* @var UserRechargeRecord $record */
- $record = UserRechargeRecord::with(['combo'])->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();
- return [
- 'id' => $record->combo_id,
- 'title' => $record->combo->name,
- 'price' => $record->combo->price
- ];
- }
- private function buyVip($payId)
- {
- /* @var UserVipRecord $record */
- $record = UserVipRecord::with(['combo'])->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();
- return [
- 'id' => $record->combo_id,
- 'title' => $record->combo->name,
- 'price' => $record->combo->price
- ];
- }
- }
|