forked from schavery/sdf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Stripe.php
215 lines (168 loc) · 5.55 KB
/
Stripe.php
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
<?php
/**
*Do the stripe parts of the donation.
*/
namespace SDF;
require_once WP_PLUGIN_DIR . '/sdf/message.php';
require_once WP_PLUGIN_DIR . '/sdf/types.php';
class Stripe {
private $stripe_plan;
private $stripe_customer;
private $stripe_id;
private $amount;
private $amount_string;
private $token;
private $email;
private $name;
private $recurrence_type;
private $recurrence_string;
// entrypoint
public function charge(&$data) {
sdf_message_handler(MessageTypes::DEBUG, 'Entered Stripe class');
$this->amount = $data['amount-cents'];
$this->amount_string = $data['amount-string'];
$this->token = $data['token'];
$this->email = $data['email'];
$this->name = $data['name'];
$this->recurrence_type = $data['recurrence-type'];
$this->recurrence_string = $data['recurrence-string'];
$this->stripe_id = self::invoice();
}
// This function is public since we use it to test keys input to
// the options page.
public static function api($input = null) {
require_once WP_PLUGIN_DIR
. '/sdf/vendor/stripe/stripe-php/init.php';
if(!empty($input)) {
\Stripe\Stripe::setApiKey($input);
} else {
\Stripe\Stripe::setApiKey(get_option('stripe_api_secret_key'));
}
\Stripe\Stripe::setApiVersion('2015-10-01');
}
public function get_stripe_id() {
return $this->stripe_id;
}
// chunky and slow!
public function get_subscription_from_charge($charge) {
sdf_message_handler(MessageTypes::DEBUG, 'Attempting to look up Stripe subscription id for previous charge');
try {
$charge = \Stripe\Charge::retrieve($charge);
$invoice_id = $charge['invoice'];
sdf_message_handler(MessageTypes::DEBUG, 'Got the charge object');
$invoice = \Stripe\Invoice::retrieve($invoice_id);
$invoice_items = $invoice['lines']['data'];
sdf_message_handler(MessageTypes::DEBUG, 'Got the invoice object');
foreach ($invoice_items as $item) {
if(strcmp($item['type'], 'subscription') === 0) {
sdf_message_handler(MessageTypes::DEBUG, 'Found a subscription');
return $item['id'];
}
}
} catch(\Stripe\Error\InvalidRequest $e) {
sdf_message_handler(MessageTypes::DEBUG, 'Caught an exception; giving up on finding subscription');
return null;
}
}
private function invoice() {
if($this->recurrence_type == RecurrenceTypes::ONE_TIME) {
return self::single_charge();
} else {
return self::recurring_charge();
}
}
private function single_charge() {
sdf_message_handler(MessageTypes::DEBUG, 'Doing a single charge');
try {
$result = \Stripe\Charge::create(array(
'amount' => $this->amount,
'card' => $this->token,
'currency' => 'usd',
'description' => $this->email
));
sdf_message_handler(MessageTypes::DEBUG,
sprintf('Single charge created, amount: %d (cents), id: %s',
$result->amount, $result->id));
return $result->id;
} catch(\Stripe\Error\Base $e) {
sdf_message_handler(MessageTypes::ERROR, $e);
}
}
private function recurring_charge() {
sdf_message_handler(MessageTypes::DEBUG, 'Creating a recurring charge');
self::plan();
self::stripe_customer();
return self::subscribe();
}
// We assume that the plan has been created, and try to retrieve it
// and if we fail, then we create the plan
private function plan() {
$plan_id = strtolower($this->recurrence_string) . '-' . $this->amount;
try {
sdf_message_handler(MessageTypes::DEBUG,
'Attempting to get plan from Stripe');
$plan = \Stripe\Plan::retrieve($plan_id);
sdf_message_handler(MessageTypes::DEBUG, 'Plan found');
} catch(\Stripe\Error\InvalidRequest $e) {
sdf_message_handler(MessageTypes::DEBUG, 'Plan not found');
if($this->recurrence_type == RecurrenceTypes::ANNUAL) {
$recurrence = 'year';
} else {
$recurrence = 'month';
}
$new_plan = array(
'id' => $plan_id,
'currency' => 'USD',
'interval' => $recurrence,
'amount' => $this->amount,
'name' => $this->amount_string . ' ' . $recurrence . 'ly gift'
);
try {
sdf_message_handler(MessageTypes::DEBUG,
'Attempting to create new plan');
$plan = \Stripe\Plan::create($new_plan);
} catch(\Stripe\Error\Base $e) {
sdf_message_handler(MessageTypes::LOG,
__FUNCTION__ . ' : ' . $e);
sdf_message_handler(MessageTypes::ERROR,
'Something went wrong! Try again?');
}
}
sdf_message_handler(MessageTypes::DEBUG,
sprintf('Plan id is: %s', $plan->id));
$this->stripe_plan = $plan;
}
// Create the basic customer
private function stripe_customer() {
sdf_message_handler(MessageTypes::DEBUG,
sprintf('Creating Stripe customer: %s', $this->email));
$info = array(
'card' => $this->token,
'email' => $this->email,
'description' => $this->name
);
try {
$customer = \Stripe\Customer::create($info);
} catch(\Stripe\Error\Base $e) {
sdf_message_handler(MessageTypes::ERROR, $e);
}
$this->stripe_customer = $customer;
sdf_message_handler(MessageTypes::DEBUG,
sprintf('Stripe customer created: %s', $customer->id));
}
// sign up for the plan.
private function subscribe() {
sdf_message_handler(MessageTypes::DEBUG,
'Signing the customer up for the plan');
try {
$result = $this->stripe_customer->updateSubscription(
array('plan' => $this->stripe_plan->id));
sdf_message_handler(MessageTypes::DEBUG,
sprintf('Signed up customer %s for plan %s, subscription id: %s',
$result->customer, $result->plan->id, $result->id));
return $result->id;
} catch(\Stripe\Error\Base $e) {
sdf_message_handler(MessageTypes::ERROR, $e);
}
}
} // end class ?>