Issues (1061)

subscriptions.php (2 issues)

1
<?php
2
3
/**
4
 * This file is the file which all subscription gateways should call
5
 * when a payment has been received - it sorts out the user status.
6
 *
7
 * Simple Machines Forum (SMF)
8
 *
9
 * @package SMF
10
 * @author Simple Machines https://www.simplemachines.org
11
 * @copyright 2020 Simple Machines and individual contributors
12
 * @license https://www.simplemachines.org/about/smf/license.php BSD
13
 *
14
 * @version 2.1 RC2
15
 */
16
17
// Set this to true to always log $_POST info received from payment gateways.
18
$paid_debug = false;
19
20
// Start things rolling by getting SMF alive...
21
$ssi_guest_access = true;
22
if (!file_exists(dirname(__FILE__) . '/SSI.php'))
23
	die('Cannot find SSI.php');
24
25
require_once(dirname(__FILE__) . '/SSI.php');
26
require_once($sourcedir . '/ManagePaid.php');
27
28
// For any admin emailing.
29
require_once($sourcedir . '/Subs-Admin.php');
30
31
loadLanguage('ManagePaid');
32
33
// If there's literally nothing coming in, let's take flight!
34
if (empty($_POST))
35
{
36
	header('content-type: text/html; charset=' . (empty($modSettings['global_character_set']) ? (empty($txt['lang_character_set']) ? 'ISO-8859-1' : $txt['lang_character_set']) : $modSettings['global_character_set']));
37
	die($txt['paid_no_data']);
38
}
39
40
// I assume we're even active?
41
if (empty($modSettings['paid_enabled']))
42
	exit;
43
44
// If we have some custom people who find out about problems load them here.
45
$notify_users = array();
46
if (!empty($modSettings['paid_email_to']))
47
{
48
	foreach (explode(',', $modSettings['paid_email_to']) as $email)
49
		$notify_users[] = array(
50
			'email' => $email,
51
			'name' => $txt['who_member'],
52
			'id' => 0,
53
		);
54
}
55
56
// We need to see whether we can find the correct payment gateway,
57
// we'll going to go through all our gateway scripts and find out
58
// if they are happy with what we have.
59
$txnType = '';
60
$gatewayHandles = loadPaymentGateways();
61
foreach ($gatewayHandles as $gateway)
62
{
63
	$gatewayClass = new $gateway['payment_class']();
64
	if ($gatewayClass->isValid())
65
	{
66
		$txnType = $gateway['code'];
67
		break;
68
	}
69
}
70
71
if (empty($txnType))
72
	generateSubscriptionError($txt['paid_unknown_transaction_type']);
73
74
// Get the subscription and member ID, amongst others...
75
@list($subscription_id, $member_id) = $gatewayClass->precheck();
76
77
// Integer these just in case.
78
$subscription_id = (int) $subscription_id;
79
$member_id = (int) $member_id;
80
81
// This would be bad...
82
if (empty($member_id))
83
	generateSubscriptionError($txt['paid_empty_member']);
84
85
// Verify the member.
86
$request = $smcFunc['db_query']('', '
87
	SELECT id_member, member_name, real_name, email_address
88
	FROM {db_prefix}members
89
	WHERE id_member = {int:current_member}',
90
	array(
91
		'current_member' => $member_id,
92
	)
93
);
94
// Didn't find them?
95
if ($smcFunc['db_num_rows']($request) === 0)
96
	generateSubscriptionError(sprintf($txt['paid_could_not_find_member'], $member_id));
97
$member_info = $smcFunc['db_fetch_assoc']($request);
98
$smcFunc['db_free_result']($request);
99
100
// Get the subscription details.
101
$request = $smcFunc['db_query']('', '
102
	SELECT cost, length, name
103
	FROM {db_prefix}subscriptions
104
	WHERE id_subscribe = {int:current_subscription}',
105
	array(
106
		'current_subscription' => $subscription_id,
107
	)
108
);
109
110
// Didn't find it?
111
if ($smcFunc['db_num_rows']($request) === 0)
112
	generateSubscriptionError(sprintf($txt['paid_count_not_find_subscription'], $member_id, $subscription_id));
113
114
$subscription_info = $smcFunc['db_fetch_assoc']($request);
115
$smcFunc['db_free_result']($request);
116
117
// We wish to check the pending payments to make sure we are expecting this.
118
$request = $smcFunc['db_query']('', '
119
	SELECT id_sublog, payments_pending, pending_details, end_time
120
	FROM {db_prefix}log_subscribed
121
	WHERE id_subscribe = {int:current_subscription}
122
		AND id_member = {int:current_member}
123
	LIMIT 1',
124
	array(
125
		'current_subscription' => $subscription_id,
126
		'current_member' => $member_id,
127
	)
128
);
129
if ($smcFunc['db_num_rows']($request) === 0)
130
	generateSubscriptionError(sprintf($txt['paid_count_not_find_subscription_log'], $member_id, $subscription_id));
131
$subscription_info += $smcFunc['db_fetch_assoc']($request);
132
$smcFunc['db_free_result']($request);
133
134
// Is this a refund etc?
135
if ($gatewayClass->isRefund())
136
{
137
	// If the end time subtracted by current time, is not greater
138
	// than the duration (ie length of subscription), then we close it.
139
	if ($subscription_info['end_time'] - time() < $subscription_info['length'])
140
	{
141
		// Delete user subscription.
142
		removeSubscription($subscription_id, $member_id);
143
		$subscription_act = time();
144
		$status = 0;
145
	}
146
	else
147
	{
148
		loadSubscriptions();
149
		$subscription_act = $subscription_info['end_time'] - $context['subscriptions'][$subscription_id]['num_length'];
150
		$status = 1;
151
	}
152
153
	// Mark it as complete so we have a record.
154
	$smcFunc['db_query']('', '
155
		UPDATE {db_prefix}log_subscribed
156
		SET end_time = {int:current_time}
157
		WHERE id_subscribe = {int:current_subscription}
158
			AND id_member = {int:current_member}
159
			AND status = {int:status}',
160
		array(
161
			'current_time' => $subscription_act,
162
			'current_subscription' => $subscription_id,
163
			'current_member' => $member_id,
164
			'status' => $status,
165
		)
166
	);
167
168
	// Receipt?
169
	if (!empty($modSettings['paid_email']) && $modSettings['paid_email'] == 2)
170
	{
171
		$replacements = array(
172
			'NAME' => $subscription_info['name'],
173
			'REFUNDNAME' => $member_info['member_name'],
174
			'REFUNDUSER' => $member_info['real_name'],
175
			'PROFILELINK' => $scripturl . '?action=profile;u=' . $member_id,
176
			'DATE' => timeformat(time(), false),
177
		);
178
179
		emailAdmins('paid_subscription_refund', $replacements, $notify_users);
180
	}
181
182
}
183
// Otherwise is it what we want, a purchase?
184
elseif ($gatewayClass->isPayment() || $gatewayClass->isSubscription())
185
{
186
	$cost = $smcFunc['json_decode']($subscription_info['cost'], true);
187
	$total_cost = $gatewayClass->getCost();
188
	$notify = false;
189
190
	// For one off's we want to only capture them once!
191
	if (!$gatewayClass->isSubscription())
192
	{
193
		$real_details = $smcFunc['json_decode']($subscription_info['pending_details'], true);
194
		if (empty($real_details))
195
			generateSubscriptionError(sprintf($txt['paid_count_not_find_outstanding_payment'], $member_id, $subscription_id));
196
197
		// Now we just try to find anything pending.
198
		// We don't really care which it is as security happens later.
199
		foreach ($real_details as $id => $detail)
200
		{
201
			unset($real_details[$id]);
202
			if ($detail[3] == 'payback' && $subscription_info['payments_pending'])
203
				$subscription_info['payments_pending']--;
204
			break;
205
		}
206
207
		$subscription_info['pending_details'] = empty($real_details) ? '' : $smcFunc['json_encode']($real_details);
208
209
		$smcFunc['db_query']('', '
210
			UPDATE {db_prefix}log_subscribed
211
			SET payments_pending = {int:payments_pending}, pending_details = {string:pending_details}
212
			WHERE id_sublog = {int:current_subscription_item}',
213
			array(
214
				'payments_pending' => $subscription_info['payments_pending'],
215
				'current_subscription_item' => $subscription_info['id_sublog'],
216
				'pending_details' => $subscription_info['pending_details'],
217
			)
218
		);
219
	}
220
221
	// Is this flexible?
222
	if ($subscription_info['length'] == 'F')
223
	{
224
		$found_duration = 0;
225
226
		// This is a little harder, can we find the right duration?
227
		foreach ($cost as $duration => $value)
228
		{
229
			if ($duration == 'fixed')
230
				continue;
231
			elseif ((float) $value == (float) $total_cost)
232
				$found_duration = strtoupper(substr($duration, 0, 1));
233
		}
234
235
		// If we have the duration then we're done.
236
		if ($found_duration !== 0)
237
		{
238
			$notify = true;
239
			addSubscription($subscription_id, $member_id, $found_duration);
240
		}
241
	}
242
	else
243
	{
244
		$actual_cost = $cost['fixed'];
245
246
		// It must be at least the right amount.
247
		if ($total_cost != 0 && $total_cost >= $actual_cost)
248
		{
249
			// Add the subscription.
250
			$notify = true;
251
			addSubscription($subscription_id, $member_id);
252
		}
253
	}
254
255
	// Send a receipt?
256
	if (!empty($modSettings['paid_email']) && $modSettings['paid_email'] == 2 && $notify)
257
	{
258
		$replacements = array(
259
			'NAME' => $subscription_info['name'],
260
			'SUBNAME' => $member_info['member_name'],
261
			'SUBUSER' => $member_info['real_name'],
262
			'SUBEMAIL' => $member_info['email_address'],
263
			'PRICE' => sprintf($modSettings['paid_currency_symbol'], $total_cost),
264
			'PROFILELINK' => $scripturl . '?action=profile;u=' . $member_id,
265
			'DATE' => timeformat(time(), false),
266
		);
267
268
		emailAdmins('paid_subscription_new', $replacements, $notify_users);
269
	}
270
}
271
// Maybe they're cancelling. Some subscriptions may require actively doing something, but PayPal doesn't, for example.
272
elseif ($gatewayClass->isCancellation())
273
{
274
	if (method_exists($gatewayClass, 'performCancel'))
275
		$gatewayClass->performCancel($subscription_id, $member_id, $subscription_info);
276
}
277
else
278
{
279
	// Some other "valid" transaction such as:
280
	//
281
	// subscr_signup: This IPN response (txn_type) is sent only the first time the user signs up for a subscription.
282
	// It then does not fire in any event later. This response is received somewhere before or after the first payment of
283
	// subscription is received (txn_type=subscr_payment) which is what we do process
284
	//
285
	// Should we log any of these ...
286
}
287
288
// In case we have anything specific to do.
289
$gatewayClass->close();
290
291
// Hidden setting to log the IPN info for debugging purposes.
292
if ($paid_debug === true)
0 ignored issues
show
The condition $paid_debug === true is always false.
Loading history...
293
	generateSubscriptionError($txt['subscription'], true);
294
295
/**
296
 * Log an error then exit
297
 *
298
 * @param string $text The error to log
299
 * @param bool $debug If true, won't send an email if $modSettings['paid_email'] isn't set
300
 * @return void
301
 */
302
function generateSubscriptionError($text, $debug = false)
303
{
304
	global $modSettings, $notify_users, $smcFunc;
305
306
	// Send an email?
307
	if (!empty($modSettings['paid_email']) && !$debug)
308
	{
309
		$replacements = array(
310
			'ERROR' => $text,
311
		);
312
313
		emailAdmins('paid_subscription_error', $replacements, $notify_users);
314
	}
315
316
	// Maybe we can try to give them the post data?
317
	if (!empty($_POST))
318
	{
319
		foreach ($_POST as $key => $val)
320
			$text .= '<br>' . $smcFunc['htmlspecialchars']($key) . ': ' . $smcFunc['htmlspecialchars']($val);
321
	}
322
323
	// Then just log and die.
324
	log_error($text, 'paidsubs');
325
326
	exit;
0 ignored issues
show
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
327
}
328
329
?>