function __construct() { \Stripe\Stripe::setApiKey(EcommercePlugin::secretKey()); $order_items = array(); $user = getLoggedInUser(); $stripe_cust = $user->stripe_cust; $cart = Cache::get("cart", "session"); if (!$stripe_cust) { try { $cu = \Stripe\Customer::create(array("description" => $user->email, "source" => getInput("stripeToken"))); } catch (Exception $e) { new SystemMessage("There has been an error. Please contact us."); forward("home"); } $user->stripe_cust = $cu->id; $user->save(); } else { $cu = \Stripe\Customer::retrieve($stripe_cust); try { $cu->source = getInput("stripeToken"); } catch (Exception $e) { new SystemMessage("There has been an error. Please contact us."); forward("home"); } $cu->save(); } foreach ($cart as $guid => $details) { $product = getEntity($guid); if ($product->interval == "one_time") { $order_item = array("type" => "sku", "parent" => $product->stripe_sku, "description" => $product->description . $details); $order_items[] = $order_item; } else { try { $cu->subscriptions->create(array("plan" => $guid)); } catch (Exception $e) { new SystemMessage("There has been an error. Please contact us."); forward("home"); } } } if (!empty($order_items)) { try { $order = \Stripe\Order::create(array("items" => $order_items, "currency" => "usd", "customer" => $cu->id)); $order->pay(array("customer" => $cu->id, "email" => $user->email)); } catch (Exception $e) { new SystemMessage("There has been an error. Please contact us."); forward("home"); } } $invoice = new Invoice(); $invoice->items = $cart; $invoice->status = "paid"; $invoice->owner_guid = getLoggedInUserGuid(); $invoice->stripe_order = $order->id; $invoice->save(); Cache::delete("cart", "session"); new SystemMessage("Your purchase is complete."); forward("billing"); }