-
Notifications
You must be signed in to change notification settings - Fork 0
/
stripe-class
296 lines (233 loc) · 8.31 KB
/
stripe-class
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<?php
/******************************************
* Function : subscriptions_delete();
* Description : Delete the subscription on stripe no more recurring billing
* Author : devender Kumar
* Twitter : @kantsverma
******************************************/
// First your need to downalod the stripe lib from the stripe official site then upload that in your project direcroty
// and add include the library file like below in class make sure path will be correct
include(APPPATH.'libraries/stripe_new/lib/Stripe.php');
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Stripe {
private $Apikey;
function __construct(){
parent::__construct();
// for test purpose
$this->Apikey = Stripe::setApiKey("sk_test_uelzkn6bJeWwucEFeuaCnm90");
}
/******************************************
* Function : update_subscription_plan();
* Description : This is used to upgrade or change the subscription plan on stripe
* Author : devender
* Twitter : @kantsverma
******************************************/
function update_subscription_plan()
{
$result['errors'] = '';
$result['success'] = false;
// check the previous plan of users by its id
//print_r($this->session->userdata('subscription_id'));
// previous plan id
$lastPlan = '2month_plan';
if($lastPlan != $_POST('monthly_plan') ){
// get the last date of previous plan and set it to this variable is should be strtotime
$lastDate = '';
// compare with the stripe plan id with type of plan is previous
if($_POST['plan'] == 3){
$trial_end = strtotime(date("Y-m-d", strtotime(date("Y-m-d", $lastDate))) . " +6 month");
}elseif($_POST['plan'] == 2){
$trial_end = strtotime(date("Y-m-d", strtotime(date("Y-m-d", $lastDate))) . " +3 month");
}else{
$trial_end = strtotime(date("Y-m-d", strtotime(date("Y-m-d", $lastDate))) . " +1 month");
}
$subscription_data = array(
'id'=>$this->session->userdata('subscription_id'),
'plan_id'=>$this->input->post('monthly_plan'),
'customer_id'=>$this->session->userdata('customer_id'),
'trial_end'=>$trial_end
);
$c = Stripe_Customer::retrieve($_POST['customer_id']);
if($c){
// updat ethe plan of users
try {
//print_r($this->input->post('monthly_plan'));
$plan = Stripe_Plan::retrieve($this->input->post('monthly_plan'));
if($plan->id){
$planId = $plan->id;
}else{
$planId = $_POST('monthly_plan');
}
if($planId == 3){
$planType = '6 month';
}elseif($planId == 2){
$planType = '3 month';
}else{
$planType = '1 month';
}
// update the subscription on stripe
$c->updateSubscription(array("plan" => $planId, "prorate" => false ));
// update here in database latest plan
//$this->model_account->update_subscription($subscription_data);
$result['success'] = 'Thank you for changing your subscription, you are now on the '.$planType.'-month plan!';
}catch (Exception $e) {
$result['success'] = $e->getMessage();
}
}else{
$result['success'] = 'Sorry !There is some problem with customer id please try again later';
}
}else{
$result['success'] = 'Sorry ! you have already this plan please select another';
}
echo json_encode($result);
}
/******************************************
* Function : subscriptions_delete();
* Description : Delete the subscription on stripe no more recurring billing
* Author : devender
* Twitter : @kantsverma
******************************************/
function subscriptions_delete()
{
if(@$_POST['customer_id']){
try {
// get customer from stripe using customer ID
$cu = Stripe_Customer::retrieve($_POST['customer_id']);
// set cancelled his subscription on period end on recent subscription
$cancel = $cu->cancelSubscription(array('at_period_end' => true));
if($cancel->id){
$result['success'] = 1;
}else{
$result['error'] = 'Failed to cancel subscription';
}
}catch (Exception $e) {
$result['error'] = $e->getMessage();
}
}else{
$result['error'] = 'Not found any Subscriber id';
}
echo json_encode($result);
}
/******************************************
* Function : update_payment();
* Description : Update payment on stripe
* Author : devender
* Twitter : @kantsverma
******************************************/
function update_payment()
{
$_POST['customer_id'];
try {
// get customer from stripe using customer ID
$cu = Stripe_Customer::retrieve($_POST['customer_id']);
$cu->card = array('number'=>$_POST['id_cardnumber'],
'exp_month'=>$_POST['id_month'],
'exp_year'=>$_POST['id_year'],
'cvc'=>$_POST['id_cvv'],
'name'=>$_POST['id_firstname'].' '.$_POST['id_lastname'],
'address_line1'=>$_POST['id_address1'],
'address_line2'=>$_POST['id_apt'],
'address_zip'=>$_POST['id_zip'],
'address_state'=>$_POST['id_state'],
'address_city'=>$_POST['id_city'],
'address_country'=>$_POST['country']
);
$cu->save();
} catch (Exception $e) {
$result['success'] = $e->getMessage();
}
}
/******************************************
* Function : create_new_subscription();
* Description : Creaqting new subscription on stripe
* Author : devender
* Twitter : @kantsverma
******************************************/
function create_new_subscription(){
$result['errors'] = array();
$result['success'] = false;
// stripe payment go here
$_POST['recurring'] = 'yes';
$using_discount = false;
$payment = '';
try {
// create a token to access the data from stripe
$tokens = Stripe_Token::create(array(
"card" =>
array(
'number' => $_POST['card_number'],
'exp_month' => $_POST['month'],
'exp_year' => $_POST['year'],
'cvc' => $_POST['cvv'],
'name' => $_POST['firstname'].' '.$_POST['lastname'],
'address_line1' => $_POST['address'],
'address_line2' => $_POST['apt'],
'address_zip' => $_POST['postal_code'],
'address_state' => $_POST['state'],
'address_country' => $_POST['country']
)
)
);
$token = $tokens->id;
$plan_id = strip_tags(trim(($_POST['package_id'])?$_POST['package_id']:'1'));
// check if there is any coupon used by customer
if($this->input->post('coupon_code')){
$customerArr = Stripe_Customer::create(array(
"card" => $token,
"plan" => $plan_id,
"description" => strip_tags($_POST['email']),
"coupon" => strip_tags($this->input->post('coupon_code'))
)
);
}else{
$customerArr = Stripe_Customer::create(array(
"card" => $token,
"plan" => $plan_id,
"description" => strip_tags($_POST['email'])
)
);
}
// check customr create or not
if($customerArr->id){
//// Save All Information in DataBase
$id = $this->session->userdata('login_id');
////insert information in subscriptions table
// Perform here the databse entry
$result['success'] = 1;
}else{
$result['success'] = 0;
}
} catch (Exception $e) {
$result['success'] = $e->getMessage();
}
echo json_encode($result);
}
/******************************************
* Function : validate_coupon();
* Description : check valid coupon from stripe
* Author : devender
* Twitter : @kantsverma
******************************************/
function validate_coupon()
{
$res='';
$using_discount = false;
$coupon_code = $_POST('coupon_code');
if(isset($coupon_code) && strlen(trim($coupon_code)) > 0) {
$_POST['recurring'] = 'yes';
$using_discount = true;
// we have a discount code, now check that it is valid
try {
$coupon = Stripe_Coupon::retrieve( trim( $_POST('coupon_code') ) );
// if we got here, the coupon is valid
} catch (Exception $e) {
// an exception was caught, so the code is invalid
$error = "The coupon code you entered is invalid. Please click back and enter a valid code, or leave it blank for no discount.";
$res = 'notmatched';
}
}else{
$res = 'matched';
}
return $res;
}
}