Skip to content

Commit f8833ab

Browse files
committed
prices interval count
1 parent 9dce4bb commit f8833ab

File tree

5 files changed

+29
-14
lines changed

5 files changed

+29
-14
lines changed

config/lara-stripe.php

+1
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,5 @@
1010
'public_key' => env('STRIPE_PUBLIC_KEY'),
1111
'success_url' => env('STRIPE_SUCCESS_URL'),
1212
'cancel_url' => env('STRIPE_CANCEL_URL'),
13+
'is_production' => env('STRIPE_IS_PRODUCTION'),
1314
];

doc/prices.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ $stripePrices = new StripePrices();
1111

1212
$stripePrices->product('product id') // Set the product id
1313
->amount(10.00) // Set the price amount
14-
->interval('month') // Set the billing interval (day, week, month, year)
14+
->interval('month', 1) // Set the billing interval (day, week, month, year) then interval count.
1515
->currency('usd') // Set the currency
1616
->trial(7) // Set the trial period in days (optional)
1717
->description('Subscription Plan') // Set a description (optional)

src/Lib/StripeCustomer.php

+5-11
Original file line numberDiff line numberDiff line change
@@ -185,20 +185,14 @@ public function cards($id)
185185
*
186186
* @param string|integer $cusId
187187
* @param string $cardToken genearte by stripe.js ui side.
188-
* @param integer $max how many card can add for a customer.
189188
*/
190-
public function addCard($cusId, $cardToken, $max = 3)
189+
public function addCard($cusId, $cardToken)
191190
{
192191
try {
193-
if (count($this->cards($cusId)) <= $max - 1) {
194-
$customer = $this->stripe->customers->createSource($cusId, [
195-
'source' => $cardToken,
196-
]);
197-
return $customer;
198-
}
199-
200-
return "You already exceed card quota {$max}";
201-
192+
$customer = $this->stripe->customers->createSource($cusId, [
193+
'source' => $cardToken,
194+
]);
195+
return $customer;
202196
} catch (\Exception $e) {
203197
return (object) ['isError' => 'true', 'message' => $e->getMessage()];
204198
}

src/Lib/StripePrices.php

+16-2
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class StripePrices
6565
* A brief description of the plan, hidden from customers.
6666
*/
6767
private $nickname;
68+
private $intervalCount = '';
6869

6970
public function __construct()
7071
{
@@ -102,9 +103,14 @@ public function amount($amount)
102103
* @param string $type [day,week,month,year]
103104
* @return $this
104105
*/
105-
public function interval($type)
106+
public function interval($type, $count='')
106107
{
107108
$this->interval = $type;
109+
110+
if ($count !== '') {
111+
$this->intervalCount = $count;
112+
}
113+
108114
return $this;
109115
}
110116

@@ -165,10 +171,18 @@ public function description($data)
165171
*/
166172
public function createPrice()
167173
{
174+
$recuringData = [
175+
'interval' => $this->interval,
176+
];
177+
178+
if ($this->intervalCount) {
179+
$recuringData['interval_count'] = $this->intervalCount;
180+
}
181+
168182
$priceData = [
169183
'unit_amount' => $this->amount,
170184
'currency' => $this->currency,
171-
'recurring' => ['interval' => $this->interval],
185+
'recurring' => $recuringData,
172186
'product' => $this->product,
173187
];
174188

src/Lib/StripeProducts.php

+6
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@ class StripeProducts
1010
private $stripe;
1111

1212
private $productName;
13+
public $isProduction;
1314

1415
public function __construct()
1516
{
1617
$this->secretKey = config('lara-stripe.secret_key');
1718
$this->stripe = new StripeClient($this->secretKey);
19+
$this->isProduction = config('lara-stripe.is_production');
1820
}
1921

2022
/**
@@ -40,6 +42,10 @@ public function create()
4042
'name' => $this->productName
4143
];
4244

45+
if ($this->isProduction) {
46+
$productData['livemode'] = true;
47+
}
48+
4349
try {
4450
$product = $this->stripe->products->create($productData);
4551
return $product;

0 commit comments

Comments
 (0)