public function cashout($app)
    {
        if (!$app->user->isLoggedIn()) {
            $app->output->redirect('/account/login');
        }
        try {
            $cashout = null;
            CashoutRequest::transaction(function () use($app, &$cashout) {
                $request = $app->router->flight->request();
                $provider_identifier = $request->data->provider_identifier ?: '';
                $provider = $request->data->provider ?: 'paypal';
                $listings = Listing::find('all', array('conditions' => array('user_id = ? AND stage = ?', $app->user->id, Listing::STAGE_COMPLETE)));
                if (empty($listings)) {
                    throw new Exception('You have submitted an invalid cashout request. There are no listings to cash out.');
                }
                foreach ($listings as $idx => &$listing) {
                    $listing->setStage('archive');
                }
                $total = 0;
                $total += array_reduce($listings, function ($carry, $listing) {
                    $carry += $listing->price;
                    return $carry;
                });
                $cashout = CashoutRequest::create(['user_id' => $app->user->id, 'provider' => $provider, 'provider_identifier' => $provider_identifier, 'total' => $total, 'status' => CashoutRequest::STATUS_REQUEST]);
                if ($cashout->is_invalid()) {
                    throw new Exception('You have submitted an invalid cashout request.');
                }
                $cashout->add($listings);
                $app->user->last_cashout = $cashout->created_at;
                $app->user->save();
                switch ($cashout->provider) {
                    case 'coinbase':
                        $result = $app->payment->coinbase_generate_address();
                        $cashout->token = $result->token->address;
                        $cashout->provider_identifier = $result->token->token_id;
                        $cashout->save();
                        break;
                    case 'stripe':
                        $result = $app->payment->stripe_generate_recipient($cashout);
                        $cashout->token = $result->id;
                        $cashout->save();
                        break;
                    default:
                        $result = $app->payment->paypal_generate_payment($cashout, '/admin/processCashout?cashout_id=' . $app->hashids->encrypt($cashout->id), '/admin/cashouts');
                        $cashout->token = $result['PayKey'];
                        $cashout->save();
                        break;
                }
            });
        } catch (PayPal_CashoutError $e) {
            $app->logger->log('Cashout request failed (PayPal_CashoutError)', 'ERROR', array('pathway' => 'paypal', 'exception' => $e), 'user');
            $app->output->json(array('error' => true, 'type' => 'warning', 'message' => $app->output->markdown->text('There was an error with processing your PayPal cashout request. Ensure that the PayPal e-mail in your 
[settings](' . $app->config->get('core.url') . '/account/settings) is valid and refresh this page. This issue has been logged.')), 500);
        } catch (Coinbase_CashoutError $e) {
            $app->logger->log('Cashout request failed (Coinbase_CashoutError)', 'ERROR', array('pathway' => 'coinbase', 'exception' => $e), 'user');
            $app->output->json(array('error' => true, 'type' => 'warning', 'message' => 'There was an error with processing your Coinbase cashout request. This issue has been logged.'), 500);
        } catch (Stripe_Error $e) {
            $app->logger->log('Cashout request failed (Stripe_Error)', 'ERROR', array('pathway' => 'stripe', 'exception' => $e), 'user');
            $app->output->json(array('error' => true, 'type' => 'warning', 'message' => 'There was an error with processing your Stripe cashout request. Ensure that the card that you are entering is a debit card. This issue has been logged.'), 500);
        } catch (Exception $e) {
            $app->logger->log('Cashout request failed', 'CRITICAL', array('pathway' => 'unknown', 'exception' => $e), 'user');
            $app->output->json(array('error' => true, 'type' => 'warning', 'message' => 'There was an error with processing your cashout request. This issue has been logged.'), 500);
        }
    }
    public function processCashout($app)
    {
        if (!$app->user->isLoggedIn() || !$app->user->isRank('Managing Director')) {
            $app->logger->log('Unauthorized access to Admin CP', 'ALERT', array(), 'admin');
            $app->output->redirect('/');
        }
        $request = $app->router->flight->request();
        $cashout_id = $request->query->cashout_id ?: -1;
        try {
            $cashout_id_dec = $app->hashids->decrypt($cashout_id);
            $cashout = CashoutRequest::find($cashout_id_dec);
            $cashout->status = CashoutRequest::STATUS_PAID;
            $cashout->save();
            switch ($cashout->provider) {
                case 'coinbase':
                    Notification::create(['user_id' => $cashout->user_id, 'receiver_id' => $cashout->user_id, 'title' => 'MONEY', 'body' => '**Funds for your Cashout Request #' . $app->hashids->encrypt($cashout->id) . ' have been dispursed!** You have been credited ' . money_format('$%.2n', $cashout->total) . '. 
[Claim your BTC](http://coinbase.com/claim/' . $cashout->provider_identifier . ') at your convenience.']);
                    break;
                case 'stripe':
                    Notification::create(['user_id' => $cashout->user_id, 'receiver_id' => $cashout->user_id, 'title' => 'MONEY', 'body' => '**Funds for your Cashout Request #' . $app->hashids->encrypt($cashout->id) . ' have been dispursed!** You have been credited ' . money_format('$%.2n', $cashout->total) . '. From Stripe\'s FAQ:
	For bank accounts, transfers will be available in the bank account the next business day if created before 21:00 UTC (2pm PST). If the transfer fails (due to a typo in the bank details, for example), it can take up to five business days for Stripe to be notified.

	Transfers to debit cards can take 1 to 2 days to complete. However, unlike with bank accounts, we\'ll know instantaneously if the debit card is not valid when it is added to the recipient.']);
                    break;
                default:
                    Notification::create(['user_id' => $cashout->user_id, 'receiver_id' => $cashout->user_id, 'title' => 'MONEY', 'body' => '**Funds for your Cashout Request #' . $app->hashids->encrypt($cashout->id) . ' have been dispursed!** You have been credited ' . money_format('$%.2n', $cashout->total) . ' via PayPal.']);
                    break;
            }
        } catch (Hashids_Invalid $e) {
            $app->logger->log('CashoutRequest ID given was invalid', 'ERROR', array('object' => 'CashoutRequest', 'id' => $id, 'pathway' => 'processCashout'), 'admin');
            $app->output->notFound();
        } catch (ActiveRecord\RecordNotFound $e) {
            $app->logger->log('No such CashoutRequest found', 'ERROR', array('object' => 'CashoutRequest', 'id' => $cashout_id, 'id_dec' => $cashout_id_dec, 'pathway' => 'processCashout'), 'admin');
            $app->output->notFound();
        }
        $app->output->redirect('/admin/cashouts');
    }
Exemple #3
0
 public function get_cooldown()
 {
     $last_cashout = CashoutRequest::find('first', array('conditions' => array('user_id = ?', $this->id), 'order' => 'created_at DESC'));
     if (empty($last_cashout)) {
         return false;
     }
     $diff = time() - strtotime($last_cashout->created_at->format('db'));
     if ($diff > self::CASHOUT_LIMIT) {
         return false;
     }
     return $diff;
 }