PluginProbe
GiveWP – Donation Plugin and Fundraising Platform / 4.17.0
GiveWP – Donation Plugin and Fundraising Platform v4.17.0
4.17.0 4.16.9 4.16.8.1 4.16.8 4.16.7.2 4.16.7.1 4.16.7 4.16.6.1 4.16.6 4.16.5.1 4.16.5 4.16.4 4.16.3 4.16.2 4.16.1 4.16.0 4.15.5 4.15.4 4.15.3 4.15.2 4.15.1 4.15.0 2.3.0 2.3.1 2.3.2 All 256 releases
give / src / PaymentGateways / Gateways / Stripe / Actions / GetOrCreateStripeCustomer.php

GetOrCreateStripeCustomer.php in GiveWP – Donation Plugin and Fundraising Platform 4.17.0, at src/PaymentGateways/Gateways/Stripe/Actions/GetOrCreateStripeCustomer.php

52 lines 1.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Give\PaymentGateways\Gateways\Stripe\Actions;
4
5 use Give\Donations\Models\Donation;
6 use Give\Donations\Models\DonationNote;
7 use Give\Framework\Exceptions\Primitives\Exception;
8 use Give\PaymentGateways\Gateways\Stripe\Exceptions\StripeCustomerException;
9 use Give_Stripe_Customer;
10
11 class GetOrCreateStripeCustomer
12 {
13
14 /**
15 * @since 2.20.0 add second param support to function.
16 * This param is optional because we use it only when donor subscribe for recurring donation.
17 * @since 2.21.0 Update function first argument type to Donation model
18 * @since 2.19.0
19 *
20 * @throws StripeCustomerException|Exception
21 */
22 public function __invoke(Donation $donation, string $stripePaymentMethodId = ''): Give_Stripe_Customer
23 {
24 $giveStripeCustomer = new Give_Stripe_Customer($donation->email, $stripePaymentMethodId);
25
26 if (!$giveStripeCustomer->get_id()) {
27 throw new StripeCustomerException(__('Unable to find or create stripe customer object.', 'give'));
28 }
29
30 $this->saveStripeCustomerId($donation, $giveStripeCustomer->get_id());
31
32 return $giveStripeCustomer;
33 }
34
35 /**
36 * @since 2.21.0 Update function first argument type to Donation model
37 * @since 2.19.0
38 * @throws Exception
39 */
40 protected function saveStripeCustomerId(Donation $donation, string $stripeCustomerId)
41 {
42 give()->donor_meta->update_meta($donation->donorId, give_stripe_get_customer_key(), $stripeCustomerId);
43
44 DonationNote::create([
45 'donationId' => $donation->id,
46 'content' => sprintf(__('Stripe Customer ID: %s', 'give'), $stripeCustomerId)
47 ]);
48
49 give_update_meta($donation->id, give_stripe_get_customer_key(), $stripeCustomerId);
50 }
51 }
52