LegacyServiceProvider.php
5 years ago
Onboarding.php
5 years ago
PaymentGateways.php
5 years ago
RestAPI.php
5 years ago
Routes.php
5 years ago
ServiceProvider.php
5 years ago
PaymentGateways.php
195 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\ServiceProviders; |
| 4 | |
| 5 | use Give\Controller\PayPalWebhooks; |
| 6 | use Give\Framework\Migrations\MigrationsRegister; |
| 7 | use Give\Helpers\Hooks; |
| 8 | use Give\PaymentGateways\PaymentGateway; |
| 9 | use Give\PaymentGateways\PayPalCommerce\AdvancedCardFields; |
| 10 | use Give\PaymentGateways\PayPalCommerce\AjaxRequestHandler; |
| 11 | use Give\PaymentGateways\PayPalCommerce\DonationProcessor; |
| 12 | use Give\PaymentGateways\PayPalCommerce\Models\MerchantDetail; |
| 13 | use Give\PaymentGateways\PayPalCommerce\RefreshToken; |
| 14 | use Give\PaymentGateways\PayPalCommerce\Repositories\MerchantDetails; |
| 15 | use Give\PaymentGateways\PayPalCommerce\Repositories\PayPalAuth; |
| 16 | use Give\PaymentGateways\PayPalCommerce\ScriptLoader; |
| 17 | use Give\PaymentGateways\PayPalCommerce\onBoardingRedirectHandler; |
| 18 | use Give\PaymentGateways\PayPalCommerce\PayPalClient; |
| 19 | use Give\PaymentGateways\PayPalCommerce\PayPalCommerce; |
| 20 | use Give\PaymentGateways\PayPalCommerce\Repositories\Webhooks; |
| 21 | use Give\PaymentGateways\PayPalCommerce\Webhooks\WebhookRegister; |
| 22 | use Give\PaymentGateways\PayPalStandard\Migrations\SetPayPalStandardGatewayId; |
| 23 | use Give\PaymentGateways\PayPalStandard\PayPalStandard; |
| 24 | use Give\PaymentGateways\PaypalSettingPage; |
| 25 | use Give\PaymentGateways\Stripe\DonationFormElements; |
| 26 | use Give\PaymentGateways\Stripe\ApplicationFee; |
| 27 | use Give\PaymentGateways\Stripe\Repositories\AccountDetail as AccountDetailRepository; |
| 28 | |
| 29 | /** |
| 30 | * Class PaymentGateways |
| 31 | * |
| 32 | * The Service Provider for loading the Payment Gateways |
| 33 | * |
| 34 | * @since 2.8.0 |
| 35 | */ |
| 36 | class PaymentGateways implements ServiceProvider { |
| 37 | /** |
| 38 | * Array of PaymentGateway classes to be bootstrapped |
| 39 | * |
| 40 | * @var string[] |
| 41 | */ |
| 42 | public $gateways = [ |
| 43 | PayPalStandard::class, |
| 44 | PayPalCommerce::class, |
| 45 | ]; |
| 46 | |
| 47 | /** |
| 48 | * Array of SettingPage classes to be bootstrapped |
| 49 | * |
| 50 | * @var string[] |
| 51 | */ |
| 52 | private $gatewaySettingsPages = [ |
| 53 | PaypalSettingPage::class, |
| 54 | ]; |
| 55 | |
| 56 | /** |
| 57 | * @inheritDoc |
| 58 | */ |
| 59 | public function register() { |
| 60 | give()->bind( |
| 61 | 'PAYPAL_COMMERCE_ATTRIBUTION_ID', |
| 62 | static function() { |
| 63 | return 'GiveWP_SP_PCP'; |
| 64 | } |
| 65 | ); // storage |
| 66 | |
| 67 | give()->singleton( PayPalWebhooks::class ); |
| 68 | give()->singleton( Webhooks::class ); |
| 69 | give()->singleton( DonationFormElements::class ); |
| 70 | give()->singleton( |
| 71 | ApplicationFee::class, |
| 72 | function() { |
| 73 | return new ApplicationFee( |
| 74 | give( AccountDetailRepository::class )->getAccountDetail( |
| 75 | give_stripe_get_connected_account_options()['stripe_account'] |
| 76 | ) |
| 77 | ); |
| 78 | } |
| 79 | ); |
| 80 | |
| 81 | $this->registerPayPalCommerceClasses(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * @inheritDoc |
| 86 | */ |
| 87 | public function boot() { |
| 88 | add_filter( 'give_register_gateway', [ $this, 'bootGateways' ] ); |
| 89 | add_action( 'admin_init', [ $this, 'handleSellerOnBoardingRedirect' ] ); |
| 90 | add_action( 'give-settings_start', [ $this, 'registerPayPalSettingPage' ] ); |
| 91 | Hooks::addFilter( 'give_form_html_tags', DonationFormElements::class, 'addFormHtmlTags', 99 ); |
| 92 | |
| 93 | $this->registerMigrations(); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Handle seller on boarding redirect. |
| 98 | * |
| 99 | * @since 2.8.0 |
| 100 | */ |
| 101 | public function handleSellerOnBoardingRedirect() { |
| 102 | give( onBoardingRedirectHandler::class )->boot(); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Register all payment gateways setting pages with GiveWP. |
| 107 | * |
| 108 | * @since 2.8.0 |
| 109 | */ |
| 110 | public function registerPayPalSettingPage() { |
| 111 | foreach ( $this->gatewaySettingsPages as $page ) { |
| 112 | give()->make( $page )->boot(); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Registers all of the payment gateways with GiveWP |
| 118 | * |
| 119 | * @since 2.8.0 |
| 120 | * |
| 121 | * @param array $gateways |
| 122 | * |
| 123 | * @return array |
| 124 | */ |
| 125 | public function bootGateways( array $gateways ) { |
| 126 | foreach ( $this->gateways as $gateway ) { |
| 127 | /** @var PaymentGateway $gateway */ |
| 128 | $gateway = give( $gateway ); |
| 129 | |
| 130 | $gateways[ $gateway->getId() ] = [ |
| 131 | 'admin_label' => $gateway->getName(), |
| 132 | 'checkout_label' => $gateway->getPaymentMethodLabel(), |
| 133 | ]; |
| 134 | |
| 135 | $gateway->boot(); |
| 136 | } |
| 137 | |
| 138 | return $gateways; |
| 139 | } |
| 140 | |
| 141 | /** |
| 142 | * Registers the classes for the PayPal Commerce gateway |
| 143 | * |
| 144 | * @since 2.8.0 |
| 145 | */ |
| 146 | private function registerPayPalCommerceClasses() { |
| 147 | give()->singleton( AdvancedCardFields::class ); |
| 148 | give()->singleton( DonationProcessor::class ); |
| 149 | give()->singleton( PayPalClient::class ); |
| 150 | give()->singleton( RefreshToken::class ); |
| 151 | give()->singleton( AjaxRequestHandler::class ); |
| 152 | give()->singleton( ScriptLoader::class ); |
| 153 | give()->singleton( WebhookRegister::class ); |
| 154 | give()->singleton( Webhooks::class ); |
| 155 | give()->singleton( MerchantDetails::class ); |
| 156 | give()->singleton( PayPalAuth::class ); |
| 157 | |
| 158 | give()->singleton( |
| 159 | MerchantDetail::class, |
| 160 | static function () { |
| 161 | /** @var MerchantDetails $repository */ |
| 162 | $repository = give( MerchantDetails::class ); |
| 163 | |
| 164 | return $repository->getDetails(); |
| 165 | } |
| 166 | ); |
| 167 | |
| 168 | give()->resolving( |
| 169 | MerchantDetails::class, |
| 170 | static function ( MerchantDetails $details ) { |
| 171 | $details->setMode( give_is_test_mode() ? 'sandbox' : 'live' ); |
| 172 | } |
| 173 | ); |
| 174 | |
| 175 | give()->resolving( |
| 176 | Webhooks::class, |
| 177 | static function ( Webhooks $repository ) { |
| 178 | $repository->setMode( give_is_test_mode() ? 'sandbox' : 'live' ); |
| 179 | } |
| 180 | ); |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * Register migrations |
| 185 | * |
| 186 | * @since 2.9.1 |
| 187 | */ |
| 188 | private function registerMigrations() { |
| 189 | /* @var MigrationsRegister $migrationRegisterer */ |
| 190 | $migrationRegisterer = give( MigrationsRegister::class ); |
| 191 | |
| 192 | $migrationRegisterer->addMigration( SetPayPalStandardGatewayId::class ); |
| 193 | } |
| 194 | } |
| 195 |