diff --git a/components/Basket.php b/components/Basket.php index 5d7344e..1a5c9b0 100644 --- a/components/Basket.php +++ b/components/Basket.php @@ -1,8 +1,11 @@ basketItems = $this->page['basketItems'] = Cart::content(); $this->basketCount = $this->page['basketCount'] = Cart::count(); $this->basketTotal = $this->page['basketTotal'] = Cart::total(); + + if (Session::has('orderId')) { + $this->orderId = $this->page['orderId'] = Session::get('orderId'); + } } public function onAddProduct() @@ -57,4 +64,15 @@ public function onAddProduct() $this->page['basketTotal'] = Cart::total(); } + public function onCheckout() + { + $order = new ShopOrder; + $order->items = json_encode(Cart::content()->toArray()); + $order->total = Cart::total(); + $order->save(); + + Session::put('orderId', $order->id); + + return Redirect::to('shop/checkout/payment'); + } } diff --git a/composer.json b/composer.json index a2e8a07..ef549bc 100644 --- a/composer.json +++ b/composer.json @@ -11,6 +11,7 @@ ], "require": { "php": ">=5.4.0", - "gloudemans/shoppingcart": "~1.2" + "gloudemans/shoppingcart": "~1.2", + "stripe/stripe-php": "dev-master" } } diff --git a/composer.lock b/composer.lock index f2e75c9..cc0f0ea 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at http://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "3f8e21fe939dc2f4ea92f36cab4fc4ce", + "hash": "fd3287e12c88ec7cdc32cea7e986b9c7", "packages": [ { "name": "gloudemans/shoppingcart", @@ -97,12 +97,62 @@ } ], "time": "2014-09-12 21:42:43" + }, + { + "name": "stripe/stripe-php", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/stripe/stripe-php.git", + "reference": "7192b556801973954e21bf37ec572772c29727c0" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/stripe/stripe-php/zipball/7192b556801973954e21bf37ec572772c29727c0", + "reference": "7192b556801973954e21bf37ec572772c29727c0", + "shasum": "" + }, + "require": { + "ext-curl": "*", + "ext-json": "*", + "ext-mbstring": "*", + "php": ">=5.2" + }, + "require-dev": { + "simpletest/simpletest": "*" + }, + "type": "library", + "autoload": { + "classmap": [ + "lib/Stripe/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Stripe and contributors", + "homepage": "https://github.com/stripe/stripe-php/contributors" + } + ], + "description": "Stripe PHP Library", + "homepage": "https://stripe.com/", + "keywords": [ + "api", + "payment processing", + "stripe" + ], + "time": "2014-10-09 01:21:53" } ], "packages-dev": [], "aliases": [], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": { + "stripe/stripe-php": 20 + }, "prefer-stable": false, "platform": { "php": ">=5.4.0" diff --git a/routes.php b/routes.php new file mode 100644 index 0000000..1d1e1be --- /dev/null +++ b/routes.php @@ -0,0 +1,60 @@ + 'shop'], function() +{ + Route::post('order/payment/process', function() + { + $token = post('stripeToken'); + $orderId = Session::get('orderId'); + + if (!$order = ShopOrder::find($orderId)) { + + // Todo: Add some flash data and utilise the ajax stuff instead of redirect + return Redirect::to('shop/checkout/payment/'.$orderId); + } + + $order->email = Input::get('stripeEmail'); + $order->stripe_token = $token; + + $order->billing_name = post('stripeBillingName'); + $order->billing_street = post('stripeBillingAddressLine1'); + $order->billing_town = post('stripeBillingAddressCity'); + $order->billing_county = post('stripeBillingAddressState'); + $order->billing_postcode = post('stripeBillingAddressZip'); + $order->billing_country = post('stripeBillingAddressCountry'); + + $order->shipping_name = post('stripeShippingName'); + $order->shipping_street = post('stripeShippingAddressLine1'); + $order->shipping_town = post('stripeShippingAddressCity'); + $order->shipping_county = post('stripeShippingAddressState'); + $order->shipping_postcode = post('stripeShippingAddressZip'); + $order->shipping_country = post('stripeShippingAddressCountry'); + + Stripe::setApiKey('sk_test_NHbBmLzRSL7G06gpyLVraQ2Z'); + + try { + $charge = Stripe_Charge::create([ + 'amount' => $order->total * 100, + 'currency' => 'gbp', + 'card' => $token, + ]); + + $order->is_paid = true; + $order->save(); + } + catch (Stripe_CardError $e) + { + $error = $e->getJsonBody()['error']; + Flash::error($error['message']); + + $order->save(); + + Redirect::to('shop/checkout/payment/'.$orderId)->withInput(); + } + + return Redirect::to('shop/order/'.$orderId); + }); + +}); diff --git a/updates/create_orders_table.php b/updates/create_orders_table.php index 213b31d..8a5871a 100644 --- a/updates/create_orders_table.php +++ b/updates/create_orders_table.php @@ -22,11 +22,13 @@ public function up() $table->string('billing_town'); $table->string('billing_county'); $table->string('billing_postcode'); + $table->string('billing_country'); $table->string('shipping_name'); $table->string('shipping_street'); $table->string('shipping_town'); $table->string('shipping_county'); $table->string('shipping_postcode'); + $table->string('shipping_country'); $table->timestamps(); }); }