· 5 years ago · Sep 21, 2020, 05:56 PM
1
2function seamless_donations_stripe_check_for_successful_transaction() {
3 // https://stripe.com/docs/payments/checkout/accept-a-payment#payment-success
4 // https://stripe.com/docs/cli/flags
5
6 dgx_donate_debug_log('Entering Stripe checking for successful transaction');
7
8 $donation_succeeded = false;
9 $donation_session_id = $_GET["sessionid"];
10 $currency_code = get_option('dgx_donate_currency');
11 $server_mode = get_option('dgx_donate_stripe_server');
12 if ($server_mode == 'LIVE') {
13 $stripe_secret_key = get_option('dgx_donate_live_stripe_secret_key');
14 $endpoint_secret = get_option('dgx_donate_live_webhook_stripe_secret_key');
15 } else {
16 $stripe_secret_key = get_option('dgx_donate_test_stripe_secret_key');
17 //$stripe_secret_key = get_option('dgx_donate_test_stripe_api_key');
18 $endpoint_secret = get_option('dgx_donate_test_webhook_stripe_secret_key');
19 }
20
21 // Set your secret key. Remember to switch to your live secret key in production!
22 // See your keys here: https://dashboard.stripe.com/account/apikeys
23 \Stripe\Stripe::setApiKey($stripe_secret_key);
24 dgx_donate_debug_log('Stripe API key set');
25
26 $events = \Stripe\Event::all([
27 'type' => 'checkout.session.completed',
28 'created' => [
29 // Check for events created in the last 24 hours.
30 'gte' => time() - 24 * 60 * 60,
31 ],
32 ]);
33
34 dgx_donate_debug_log('Checking for donations');
35
36 foreach ($events->autoPagingIterator() as $event) {
37 $stripe_session = $event->data->object;
38 $stripe_session_id = $stripe_session->id;
39 $stripe_mode = $stripe_session->mode;
40 if ($stripe_mode == 'payment') {
41 // record a payment intent ID if a one-off donation
42 $stripe_transaction_id = $stripe_session->payment_intent;
43 } else {
44 // record an invoice ID if a subscription
45 $subscription_id = $stripe_session->subscription;
46 $stripe_transaction_id = seamless_donations_stripe_get_latest_invoice_from_subscription($subscription_id);
47 seamless_donations_add_audit_string('STRIPE-SUBSCRIPTION-' . $subscription_id, $donation_session_id);
48 }
49 $sd_session_id = $stripe_session->metadata['sd_session_id'];
50
51 if ($sd_session_id == $donation_session_id) {
52 $donation_succeeded = true;
53 dgx_donate_debug_log('Donation succeeded');
54 break;
55 }
56 }
57
58 if ($donation_succeeded) {
59 seamless_donations_process_confirmed_purchase('STRIPE', $currency_code, $donation_session_id, $stripe_transaction_id, $stripe_session);
60 } else {
61 dgx_donate_debug_log('Donation not showing as succeeded');
62 }
63 return 'PASS';
64}