/**
* gets the payment token from stripe and marks order as paid
*/
public function action_pay()
{
$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)->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');
try {
// Create a Customer
$customer = \Stripe\Customer::create(array('card' => $token, 'email' => $order->user->email));
} catch (Exception $e) {
// The card has been declined
Kohana::$log->add(Log::ERROR, 'Stripe The card has been declined');
Alert::set(Alert::ERROR, 'The card has been declined');
$this->redirect(Route::url('default', array('controller' => 'ad', 'action' => 'checkout', 'id' => $order->id_order)));
}
//3d secure active?
if (Core::config('payment.stripe_3d_secure') == TRUE) {
try {
$three_d_secure = \Stripe\ThreeDSecure::create(array('customer' => $customer->id, 'amount' => StripeKO::money_format($order->amount), 'currency' => $order->currency, 'return_url' => Route::url('default', array('controller' => 'stripe', 'action' => '3d', 'id' => $order->id_order))));
} catch (Exception $e) {
// The card has been declined
Kohana::$log->add(Log::ERROR, 'Stripe 3D The card has been declined');
Alert::set(Alert::ERROR, 'The card has been declined 3D secure.');
$this->redirect(Route::url('default', array('controller' => 'ad', 'action' => 'checkout', 'id' => $order->id_order)));
}
//he has 3d secure redirect to stripe, if not continues normal process
if ($three_d_secure->status == 'redirect_pending') {
//so we can use later the customer to store it
Session::instance()->set('customer_id', $customer->id);
die(View::factory('post_redirect', ['redirect_url' => $three_d_secure->redirect_url])->render());
} else {
Alert::set(Alert::WARNING, 'This Card does not support 3D secure. Please try another card or use another payment method. Thanks.');
$this->redirect(Route::url('default', array('controller' => 'ad', 'action' => 'checkout', 'id' => $order->id_order)));
}
}
// Create the charge on Stripe's servers - this will charge the user's card
try {
$charge = \Stripe\Charge::create(array("amount" => StripeKO::money_format($order->amount), "currency" => $order->currency, 'customer' => $customer->id, "description" => $order->description, "metadata" => array("id_order" => $order->id_order)));
//its a plan product
if ($order->id_product >= 100) {
//save the stripe user id to be able to charge them later on renewal
$order->user->stripe_agreement = $customer->id;
$order->user->save();
}
} 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'));
//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)));
}
}