Example #1
0
 public function accept($withdrawId)
 {
     $withdrawal = Withdrawal::findOrFail($withdrawId);
     $paypal = new PaypalPaymentService();
     try {
         $amount = $withdrawal->amount;
         $result = $paypal->payout($withdrawal->user, $amount, $withdrawal->currency, $withdrawal->account, $withdrawal->id);
     } catch (\PayPal\Exception\PayPalConnectionException $e) {
         LogMapper::log('withdraw_error', $withdrawal->id, 'paypal', ['error' => json_decode($e->getData())]);
         return redirect('/admin/withdraw')->withErrors(['Paypal withdrawal failed', $e->getData()]);
     }
     if ($result->batch_header->batch_status == 'SUCCESS') {
         $withdrawal->admin_id = $this->user->id;
         $withdrawal->status = 'done';
         $withdrawal->transaction_number = $result->batch_header->payout_batch_id;
         $withdrawal->response = $result->toArray();
         $user = $withdrawal->user;
         $user->balance_blocked = $user->balance_blocked - $withdrawal->amount;
         $user->balance = $user->balance - $withdrawal->amount;
         if ($user->balance_blocked < 0) {
             $user->balance_blocked = 0;
         }
         if ($user->balance < 0) {
             $user->balance = 0;
         }
         $withdrawal->save();
         $user->save();
         NotificationMapper::withdrawAccept($withdrawal);
         LogMapper::log('withdraw_success', $withdrawal->id, 'paypal_success', ['amount' => $withdrawal->amount . $withdrawal->currency, 'withdrawal' => $amount . $withdrawal->currency, 'response' => $result->toArray()]);
         return redirect('/admin/withdraw/' . $withdrawal->id . '/show')->with(['success' => 'Withdraw accepted']);
     } else {
         LogMapper::log('withdraw_error', $withdrawal->id, 'paypal_failed', $result->toArray());
         return redirect('/admin/withdraw')->withErrors(['Paypal withdrawal failed', json_encode($result->toArray())]);
     }
 }