| 1 |
<?php |
| 2 |
|
| 3 |
namespace Give\PaymentGateways\Gateways\Stripe; |
| 4 |
|
| 5 |
use Give\Donations\Models\Donation; |
| 6 |
use Give\Helpers\Gateways\Stripe; |
| 7 |
use Give\PaymentGateways\Gateways\Stripe\StripePaymentElementGateway\StripePaymentElementGateway; |
| 8 |
use Give_Stripe; |
| 9 |
|
| 10 |
class LegacyStripeAdapter |
| 11 |
{ |
| 12 |
/** |
| 13 |
* Legacy Stripe gates these files by the use of give_stripe_supported_payment_methods. |
| 14 |
* This makes it possible to load the files without having to enable a legacy stripe gateway. |
| 15 |
* This also makes it possible to load the files without the use of the give_stripe_supported_payment_methods filter. |
| 16 |
* |
| 17 |
* @since 3.0.0 |
| 18 |
*/ |
| 19 |
public function loadLegacyStripeWebhooksAndFilters() |
| 20 |
{ |
| 21 |
$settings = give_get_settings(); |
| 22 |
$gatewaysFromSettings = $settings['gateways'] ?? []; |
| 23 |
$gatewaysFromOption = give_get_option('gateways_v3', []); |
| 24 |
|
| 25 |
// for some reason, the gateways from the settings are not always in the gateways from the option. |
| 26 |
// this might be a service provider race conditions. |
| 27 |
// for now im merging the two arrays to make sure we're checking both places.. |
| 28 |
$gateways = array_merge( |
| 29 |
$gatewaysFromOption, |
| 30 |
$gatewaysFromSettings |
| 31 |
); |
| 32 |
|
| 33 |
if (!class_exists('Give_Stripe_Webhooks') && array_key_exists(StripePaymentElementGateway::id(), $gateways)) { |
| 34 |
(new Give_Stripe())->include_frontend_files(); |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* This adds the Next Gen Stripe Gateway to the list of give_stripe_supported_payment_methods. |
| 40 |
* |
| 41 |
* If this is not included, then the webhooks will not be registered unless a legacy stripe gateway is enabled. |
| 42 |
* |
| 43 |
* @since 3.0.0 |
| 44 |
*/ |
| 45 |
public function addToStripeSupportedPaymentMethodsList() |
| 46 |
{ |
| 47 |
add_filter('give_stripe_supported_payment_methods', static function ($gateways) { |
| 48 |
$gatewayId = StripePaymentElementGateway::id(); |
| 49 |
|
| 50 |
if (!in_array($gatewayId, $gateways, true)) { |
| 51 |
$gateways[] = $gatewayId; |
| 52 |
} |
| 53 |
|
| 54 |
return $gateways; |
| 55 |
}); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* This adds the Stripe details to the donation details page. |
| 60 |
* |
| 61 |
* @since 3.0.0 |
| 62 |
*/ |
| 63 |
public function addDonationDetails() |
| 64 |
{ |
| 65 |
/** |
| 66 |
* Transaction ID link in donation details |
| 67 |
*/ |
| 68 |
add_filter( |
| 69 |
sprintf('give_payment_details_transaction_id-%s', StripePaymentElementGateway::id()), |
| 70 |
'give_stripe_link_transaction_id', |
| 71 |
10, |
| 72 |
2 |
| 73 |
); |
| 74 |
|
| 75 |
/** |
| 76 |
* Displays the stripe account details on donation details page. |
| 77 |
*/ |
| 78 |
add_action('give_view_donation_details_payment_meta_after', static function ($donationId) { |
| 79 |
/** @var Donation $donation */ |
| 80 |
$donation = Donation::find($donationId); |
| 81 |
|
| 82 |
if ($donation->gatewayId === StripePaymentElementGateway::id()) { |
| 83 |
$stripeAccounts = give_stripe_get_all_accounts(); |
| 84 |
$accountId = give_get_meta($donationId, '_give_stripe_account_slug', true); |
| 85 |
$accountDetail = $stripeAccounts[$accountId] ?? []; |
| 86 |
$account = 'connect' === $accountDetail['type'] ? |
| 87 |
"{$accountDetail['account_name']} ({$accountId})" : |
| 88 |
give_stripe_convert_slug_to_title($accountId); |
| 89 |
?> |
| 90 |
<div class="give-donation-stripe-account-used give-admin-box-inside"> |
| 91 |
<p> |
| 92 |
<strong><?php |
| 93 |
esc_html_e('Stripe Account:', 'give'); ?></strong><br /> |
| 94 |
<?php |
| 95 |
echo $account; ?> |
| 96 |
</p> |
| 97 |
</div> |
| 98 |
<?php |
| 99 |
} |
| 100 |
}); |
| 101 |
|
| 102 |
/** |
| 103 |
* Adds the stripe account details to donation notes and donation meta. |
| 104 |
*/ |
| 105 |
add_action('give_insert_payment', static function ($donationId) { |
| 106 |
/** @var Donation $donation */ |
| 107 |
$donation = Donation::find($donationId); |
| 108 |
|
| 109 |
if ($donation->gatewayId === StripePaymentElementGateway::id()) { |
| 110 |
Stripe::addAccountDetail($donationId, $donation->formId); |
| 111 |
} |
| 112 |
}); |
| 113 |
} |
| 114 |
} |
| 115 |
|