Skip to content

Commit 7953c7c

Browse files
committed
Implement Checkout
1 parent e1b32c9 commit 7953c7c

File tree

5 files changed

+40
-3
lines changed

5 files changed

+40
-3
lines changed

app/Http/Controllers/OrdersController.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,7 @@ public function store()
2828
]);
2929

3030
$order->addProducts($cart->items);
31+
32+
return redirect('/orders');
3133
}
3234
}

app/Models/StripePayment.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,20 @@ class StripePayment implements PaymentContract
1010
public function charge($total, $token)
1111
{
1212
// `source` is obtained with Stripe.js; see https://stripe.com/docs/payments/accept-a-payment-charges#web-create-token
13-
Charge::create([
13+
$charge = Charge::create([
1414
'amount' => $total,
1515
'currency' => 'usd',
1616
'source' => $token,
1717
'description' => 'My First Test Charge (created for API docs)', //optional
1818
], [
1919
'api_key' => config('services.stripe.secret'),
2020
]);
21+
22+
$this->total = $charge->amount;
23+
}
24+
25+
public function total()
26+
{
27+
return $this->total;
2128
}
2229
}

app/Providers/AppServiceProvider.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace App\Providers;
44

5+
use App\Contracts\PaymentContract;
6+
use App\Models\StripePayment;
57
use Illuminate\Support\ServiceProvider;
68

79
class AppServiceProvider extends ServiceProvider
@@ -13,7 +15,7 @@ class AppServiceProvider extends ServiceProvider
1315
*/
1416
public function register()
1517
{
16-
//
18+
$this->app->bind(PaymentContract::class, StripePayment::class);
1719
}
1820

1921
/**

tests/Feature/PurchaseTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ public function it_can_purchase_products()
3131

3232
$this->app->instance(PaymentContract::class, $payment);
3333

34-
$this->post('/orders', [
34+
$response = $this->post('/orders', [
3535
'stripeEmail' => 'test@example.com',
3636
'stripeToken' => $payment->getTestToken(),
3737
]);
3838

39+
$response->assertRedirect('/orders');
40+
3941
$this->assertEquals('10.00', $payment->totalCharged());
4042
}
4143

tests/Unit/StripePaymentTest.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,29 @@ public function it_can_make_real_charges_with_a_valid_token()
4848
$this->assertEquals(1000, $this->lastCharge()->amount);
4949
}
5050

51+
/**
52+
* @test
53+
*/
54+
public function it_has_a_total_charged_in_cents()
55+
{
56+
$payment = new StripePayment();
57+
58+
$token = Token::create([
59+
'card' => [
60+
'number' => '4242424242424242',
61+
'exp_month' => 1,
62+
'exp_year' => date('Y') + 1,
63+
'cvc' => '314',
64+
],
65+
], [
66+
'api_key' => config('services.stripe.secret'),
67+
]);
68+
69+
$payment->charge(1000, $token->id);
70+
71+
$this->assertEquals(1000, $payment->total());
72+
}
73+
5174
private function lastCharge()
5275
{
5376
return Charge::all([
@@ -57,6 +80,7 @@ private function lastCharge()
5780
])->data[0];
5881
}
5982

83+
6084
private function newCharges()
6185
{
6286
return Charge::all([

0 commit comments

Comments
 (0)