Exemplo n.º 1
0
 /**
  * gets the payment token from stripe and marks order as paid. Methos for application fee
  */
 public function action_payconnect()
 {
     //TODO only if stripe connect enabled
     $this->auto_render = FALSE;
     $id_order = $this->request->param('id');
     //retrieve info for the item in DB
     $order = new Model_Order();
     $order = $order->where('id_order', '=', $id_order)->where('status', '=', Model_Order::STATUS_CREATED)->where('id_product', '=', Model_Order::PRODUCT_AD_SELL)->limit(1)->find();
     if ($order->loaded()) {
         if (isset($_POST['stripeToken'])) {
             //its a fraud...lets let him know
             if ($order->is_fraud() === TRUE) {
                 Alert::set(Alert::ERROR, __('We had, issues with your transaction. Please try paying with another paymethod.'));
                 $this->redirect(Route::url('default', array('controller' => 'ad', 'action' => 'checkout', 'id' => $order->id_order)));
             }
             StripeKO::init();
             // Get the credit card details submitted by the form
             $token = Core::post('stripeToken');
             // email
             $email = Core::post('stripeEmail');
             // Create the charge on Stripe's servers - this will charge the user's card
             try {
                 //in case memberships the fee may be set on the plan ;)
                 $fee = NULL;
                 if ($order->ad->user->subscription()->loaded()) {
                     $fee = $order->ad->user->subscription()->plan->marketplace_fee;
                 }
                 $application_fee = StripeKO::application_fee($order->amount, $fee);
                 //we charge the fee only if its not admin
                 if (!$order->ad->user->is_admin()) {
                     $charge = \Stripe\Charge::create(array("amount" => StripeKO::money_format($order->amount), "currency" => $order->currency, "card" => $token, "description" => $order->description, "application_fee" => StripeKO::money_format($application_fee)), array('stripe_account' => $order->ad->user->stripe_user_id));
                 } else {
                     $charge = \Stripe\Charge::create(array("amount" => StripeKO::money_format($order->amount), "currency" => $order->currency, "card" => $token, "description" => $order->description));
                 }
             } catch (Exception $e) {
                 // The card has been declined
                 Kohana::$log->add(Log::ERROR, 'Stripe The card has been declined');
                 Alert::set(Alert::ERROR, 'Stripe The card has been declined');
                 $this->redirect(Route::url('default', array('controller' => 'ad', 'action' => 'checkout', 'id' => $order->id_order)));
             }
             //mark as paid
             $order->confirm_payment('stripe', Core::post('stripeToken'));
             //only if is not admin
             if (!$order->ad->user->is_admin()) {
                 //crete new order for the application fee so we know how much the site owner is earning ;)
                 $order_app = Model_Order::new_order($order->ad, $order->ad->user, Model_Order::PRODUCT_APP_FEE, $application_fee, core::config('payment.paypal_currency'), 'id_order->' . $order->id_order . ' id_ad->' . $order->ad->id_ad);
                 $order_app->confirm_payment('stripe', Core::post('stripeToken'));
             }
             //redirect him to his ads
             Alert::set(Alert::SUCCESS, __('Thanks for your payment!'));
             $this->redirect(Route::url('oc-panel', array('controller' => 'profile', 'action' => 'orders')));
         } else {
             Alert::set(Alert::INFO, __('Please fill your card details.'));
             $this->redirect(Route::url('default', array('controller' => 'ad', 'action' => 'checkout', 'id' => $order->id_order)));
         }
     } else {
         Alert::set(Alert::INFO, __('Order could not be loaded'));
         $this->redirect(Route::url('default', array('controller' => 'ad', 'action' => 'checkout', 'id' => $order->id_order)));
     }
 }