| 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 |
|