Skip to content

Commit

Permalink
Add payment stuff on the frontend with Stripe
Browse files Browse the repository at this point in the history
  • Loading branch information
dshoreman committed Oct 16, 2014
1 parent fd211e7 commit 7ec4b9a
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 3 deletions.
18 changes: 18 additions & 0 deletions components/Basket.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
<?php namespace DShoreman\Shop\Components;

use Cart;
use Redirect;
use Session;
use Cms\Classes\Page;
use Cms\Classes\ComponentBase;
use DShoreman\Shop\Models\Order as ShopOrder;
use DShoreman\Shop\Models\Product as ShopProduct;

class Basket extends ComponentBase
Expand Down Expand Up @@ -41,6 +44,10 @@ public function onRun()
$this->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()
Expand All @@ -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');
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
],
"require": {
"php": ">=5.4.0",
"gloudemans/shoppingcart": "~1.2"
"gloudemans/shoppingcart": "~1.2",
"stripe/stripe-php": "dev-master"
}
}
54 changes: 52 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

use DShoreman\Shop\Models\Order as ShopOrder;

Route::group(['prefix' => '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);
});

});
2 changes: 2 additions & 0 deletions updates/create_orders_table.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
}
Expand Down

0 comments on commit 7ec4b9a

Please sign in to comment.