Skip to content

Commit 61e7eaf

Browse files
committed
coupon
1 parent 8d26485 commit 61e7eaf

File tree

2 files changed

+51
-70
lines changed

2 files changed

+51
-70
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ php artisan vendor:publish --provider="Code4mk\LaraStripe\LaraStripeServiceProvi
2323
* [Charge doc](https://github.com/code4mk/lara-stripe/blob/master/doc/charge.md)
2424
* [Checkout doc](https://github.com/code4mk/lara-stripe/blob/master/doc/payment-checkout.md)
2525
* [Customer doc](https://github.com/code4mk/lara-stripe/blob/master/doc/customer.md)
26-
* [Plan doc](https://github.com/code4mk/lara-stripe/blob/master/doc/plan.md)
26+
* [Prices doc](https://github.com/code4mk/lara-stripe/blob/master/doc/prices.md)
2727
* [Subscription doc](https://github.com/code4mk/lara-stripe/blob/master/doc/subscription.md)
2828
* [Coupon doc](https://github.com/code4mk/lara-stripe/blob/master/doc/coupon.md)
2929

@@ -34,3 +34,5 @@ php artisan vendor:publish --provider="Code4mk\LaraStripe\LaraStripeServiceProvi
3434
# Courtesy
3535

3636
* [stripe/stripe-php](https://github.com/stripe/stripe-php)
37+
38+
* https://jsfiddle.net/ywain/o2n3js2r/

src/Lib/StripeCoupon.php

Lines changed: 48 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,9 @@
22

33
namespace Code4mk\LaraStripe\Lib;
44

5-
/**
6-
* @author @code4mk <[email protected]>
7-
* @author @kawsarsoft <[email protected]>
8-
* @copyright Kawsar Soft. (http://kawsarsoft.com)
9-
*/
10-
11-
use Config;
12-
use Stripe\Coupon;
13-
use Stripe\Stripe;
145
use Illuminate\Support\Str;
6+
use Stripe\StripeClient;
157

16-
/**
17-
* Coupon class
18-
*
19-
* @source https://stripe.com/docs/api/coupons
20-
*/
218
class StripeCoupon
229
{
2310
/**
@@ -32,14 +19,14 @@ class StripeCoupon
3219
*
3320
* @var string
3421
*/
35-
private $currency;
22+
private $currency = 'usd';
3623

3724
/**
3825
* Coupon amount
3926
*
4027
* @var int|float
4128
*/
42-
private $amount;
29+
private $amount = 0;
4330

4431
/**
4532
* Coupon amount type
@@ -69,55 +56,35 @@ class StripeCoupon
6956
*/
7057
private $durationMonth;
7158

72-
/**
73-
* All coupon data for create
74-
*
75-
* @var [type]
76-
*/
77-
private $couponData;
59+
private $stripe;
7860

7961
public function __construct()
8062
{
81-
if (config::get('lara-stripe.driver') === 'config') {
82-
$this->secretKey = config::get('lara-stripe.secret_key');
83-
}
63+
$this->secretKey = config('lara-stripe.secret_key');
64+
$this->stripe = new StripeClient($this->secretKey);
8465
}
8566

86-
/**
87-
* Set secret key
88-
*
89-
* @param string $data
90-
* @return $this
91-
*/
92-
public function setup($data)
93-
{
94-
if (isset($data['secret_key'])) {
95-
$this->secretKey = $data['secret_key'];
96-
}
97-
98-
return $this;
99-
}
10067

10168
/**
10269
* Coupon amount
10370
*
104-
* @param int|float $amount
105-
* @param string $type [fixed,per]
106-
* @param string $currency fixed amount purpose
71+
* @param int|float $amount
72+
* @param string $currency fixed amount purpose
73+
* @param string $type [fixed,percent]
10774
*
10875
* @source https://stripe.com/docs/api/coupons/object#coupon_object-amount_off
10976
*
11077
* @return $this
11178
*/
112-
public function amount($amount, $type, $currency = 'usd')
79+
public function amount($amount, $type = 'fixed', $currency = 'usd')
11380
{
11481
if ($type === 'fixed') {
11582
$this->amount = round($amount, 2) * 100;
11683
$this->type = 'fixed';
11784
$this->currency = $currency;
11885
} else {
11986
$this->amount = $amount;
120-
$this->type = 'per';
87+
$this->type = 'percent';
12188
}
12289

12390
return $this;
@@ -126,19 +93,16 @@ public function amount($amount, $type, $currency = 'usd')
12693
/**
12794
* Coupon duration
12895
*
129-
* @param string $type [forever,once,repeating]
130-
* @param int $month
131-
*
132-
* @source https://stripe.com/docs/api/coupons/create#create_coupon-duration
133-
*
96+
* @param string $type [forever,once,repeating]
97+
* @param int $month
13498
* @return $this
13599
*/
136100
public function duration($type, $month = 1)
137101
{
138-
//forever, once, or repeating.
139102
if ($type === 'repeating') {
140103
$this->durationMonth = $month;
141104
}
105+
142106
$this->duration = $type;
143107

144108
return $this;
@@ -147,46 +111,64 @@ public function duration($type, $month = 1)
147111
/**
148112
* Coupon name & id
149113
*
150-
* @param string $name snake_case
114+
* @param string $name snake_case
151115
* @return $this
152116
*/
153117
public function name($name)
154118
{
155119
$this->name = Str::snake($name);
156-
157120
return $this;
158121
}
159122

160123
/**
161124
* Create coupon & retrieve data
162125
*
163-
* @return object
126+
* @return object
164127
*/
165-
public function get()
128+
public function create()
166129
{
167-
if ($this->type === 'per') {
168-
$this->couponData['percent_off'] = $this->amount;
130+
$couponData = [];
131+
132+
if ($this->type === 'percent') {
133+
$couponData['percent_off'] = $this->amount;
169134
} else {
170-
$this->couponData['amount_off'] = $this->amount;
171-
$this->couponData['currency'] = $this->currency;
135+
$couponData['amount_off'] = $this->amount;
136+
$couponData['currency'] = $this->currency;
172137
}
173138

174139
if ($this->name) {
175-
$this->couponData['name'] = $this->name;
176-
$this->couponData['id'] = $this->name;
140+
$couponData['name'] = $this->name;
177141
}
178142

179143
if ($this->duration === 'forever' || $this->duration === 'once') {
180-
$this->couponData['duration'] = $this->duration;
144+
$couponData['duration'] = $this->duration;
181145
} else {
182-
$this->couponData['duration'] = $this->duration;
183-
$this->couponData['duration_in_months'] = $this->durationMonth;
146+
$couponData['duration'] = $this->duration;
147+
$couponData['duration_in_months'] = $this->durationMonth;
184148
}
185149

186150
try {
187-
Stripe::setApiKey($this->secretKey);
188-
$coupon = Coupon::create($this->couponData);
151+
$coupon = $this->stripe->coupons->create($couponData);
152+
return $coupon;
153+
} catch (\Exception $e) {
154+
return (object) ['isError' => 'true', 'message' => $e->getMessage(), 'stripe' => $e->getJsonBody()['error']];
155+
}
156+
}
189157

158+
public function retrieve($id)
159+
{
160+
try {
161+
$coupon = $this->stripe->coupons->retrieve($id);
162+
return $coupon;
163+
} catch (\Exception $e) {
164+
return (object) ['isError' => 'true', 'message' => $e->getMessage(), 'stripe' => $e->getJsonBody()['error']];
165+
}
166+
}
167+
168+
public function lists()
169+
{
170+
try {
171+
$coupon = $this->stripe->coupons->all();
190172
return $coupon;
191173
} catch (\Exception $e) {
192174
return (object) ['isError' => 'true', 'message' => $e->getMessage(), 'stripe' => $e->getJsonBody()['error']];
@@ -202,10 +184,7 @@ public function get()
202184
public function delete($id)
203185
{
204186
try {
205-
Stripe::setApiKey($this->secretKey);
206-
$coupon = Coupon::retrieve($id);
207-
$coupon->delete();
208-
187+
$coupon = $this->stripe->coupons->delete($id);
209188
return $coupon;
210189
} catch (\Exception $e) {
211190
return (object) ['isError' => 'true', 'message' => $e->getMessage(), 'stripe' => $e->getJsonBody()['error']];

0 commit comments

Comments
 (0)