Skip to content

Commit

Permalink
Allow for multiple line items during checkout
Browse files Browse the repository at this point in the history
  • Loading branch information
driesvints committed Dec 1, 2020
1 parent 7256c69 commit 7f4611a
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 21 deletions.
47 changes: 28 additions & 19 deletions src/Concerns/PerformsCharges.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,34 @@ public function refund($paymentIntent, array $options = [])
}

/**
* Begin a new Checkout Session for an existing Price ID.
* Begin a new Checkout Session for existing Prices.
*
* @param string $price
* @param array|string $items
* @param int $quantity
* @param array $sessionOptions
* @param array $customerOptions
* @return \Laravel\Cashier\Checkout
*/
public function checkout($price, $quantity = 1, array $sessionOptions = [], array $customerOptions = [])
public function checkout($items, array $sessionOptions = [], array $customerOptions = [])
{
$items = collect((array) $items)->map(function ($item, $key) {
// If the key is a string, we'll assume it's a Price ID and its value is its quantity.
if (is_string($key)) {
return ['price' => $key, 'quantity' => $item];
}

// If the value is a string, we'll assume it's a Price ID.
$item = is_string($item) ? ['price' => $item] : $item;

// Ensure a quantity is set.
$item['quantity'] = $item['quantity'] ?? 1;

return $item;
})->values()->all();

return Checkout::create($this, array_merge([
'allow_promotion_codes' => $this->allowPromotionCodes,
'line_items' => [[
'price' => $price,
'quantity' => $quantity,
]],
'line_items' => $items,
], $sessionOptions), $customerOptions);
}

Expand All @@ -98,19 +110,16 @@ public function checkout($price, $quantity = 1, array $sessionOptions = [], arra
*/
public function checkoutCharge($amount, $name, $quantity = 1, array $sessionOptions = [], array $customerOptions = [])
{
return Checkout::create($this, array_merge([
'allow_promotion_codes' => $this->allowPromotionCodes,
'line_items' => [[
'price_data' => [
'currency' => $this->preferredCurrency(),
'product_data' => [
'name' => $name,
],
'unit_amount' => $amount,
return $this->checkout([[
'price_data' => [
'currency' => $this->preferredCurrency(),
'product_data' => [
'name' => $name,
],
'quantity' => $quantity,
]],
], $sessionOptions), $customerOptions);
'unit_amount' => $amount,
],
'quantity' => $quantity,
]], $sessionOptions, $customerOptions);
}

/**
Expand Down
14 changes: 12 additions & 2 deletions tests/Feature/CheckoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,25 @@ public function test_customers_can_start_a_product_checkout_session()
{
$user = $this->createCustomer('customers_can_start_a_product_checkout_session');

$price = StripePrice::create([
$shirtPrice = StripePrice::create([
'currency' => 'USD',
'product_data' => [
'name' => 'T-shirt',
],
'unit_amount' => 1500,
]);

$checkout = $user->checkout($price->id, 1, [
$carPrice = StripePrice::create([
'currency' => 'USD',
'product_data' => [
'name' => 'Car',
],
'unit_amount' => 30000,
]);

$items = [$shirtPrice->id => 5, $carPrice->id];

$checkout = $user->checkout($items, [
'success_url' => 'http://example.com',
'cancel_url' => 'http://example.com',
]);
Expand Down

0 comments on commit 7f4611a

Please sign in to comment.