PayPalCommerceGateway.php
113 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Give\PaymentGateways\Gateways\PayPalCommerce; |
| 4 | |
| 5 | use Exception; |
| 6 | use Give\DonationForms\Actions\GenerateDonationFormValidationRouteUrl; |
| 7 | use Give\Framework\Support\Scripts\Concerns\HasScriptAssetFile; |
| 8 | use Give\Helpers\Language; |
| 9 | use Give\PaymentGateways\PayPalCommerce\Models\MerchantDetail; |
| 10 | use Give\PaymentGateways\PayPalCommerce\PayPalCommerce; |
| 11 | use Give\PaymentGateways\PayPalCommerce\Repositories\MerchantDetails; |
| 12 | |
| 13 | /** |
| 14 | * An extension of the PayPalCommerce gateway in GiveWP that supports the NextGenPaymentGatewayInterface. |
| 15 | */ |
| 16 | class PayPalCommerceGateway extends PayPalCommerce |
| 17 | { |
| 18 | use HasScriptAssetFile; |
| 19 | |
| 20 | /** |
| 21 | * This function uses to render the credit card form for v2 donation forms. |
| 22 | * |
| 23 | * @since 3.0.0 |
| 24 | * |
| 25 | * @param int $formId |
| 26 | * @param array $args |
| 27 | * |
| 28 | * @return string |
| 29 | */ |
| 30 | public function getLegacyFormFieldMarkup(int $formId, array $args): string |
| 31 | { |
| 32 | return parent::getLegacyFormFieldMarkup($formId, $args); |
| 33 | } |
| 34 | |
| 35 | /** |
| 36 | * @since 3.1.0 set translations for script |
| 37 | * @since 3.0.0 |
| 38 | */ |
| 39 | public function enqueueScript(int $formId) |
| 40 | { |
| 41 | $assets = $this->getScriptAsset(GIVE_PLUGIN_DIR . 'build/payPalCommerceGateway.asset.php'); |
| 42 | $handle = $this::id(); |
| 43 | |
| 44 | wp_enqueue_script( |
| 45 | $handle, |
| 46 | GIVE_PLUGIN_URL . 'build/payPalCommerceGateway.js', |
| 47 | $assets['dependencies'], |
| 48 | $assets['version'], |
| 49 | true |
| 50 | ); |
| 51 | |
| 52 | Language::setScriptTranslations($handle); |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * @since 3.19.1 Add validate URL |
| 57 | * |
| 58 | * @throws Exception |
| 59 | */ |
| 60 | public function formSettings(int $formId): array |
| 61 | { |
| 62 | return [ |
| 63 | 'ajaxUrl' => admin_url('admin-ajax.php'), |
| 64 | 'donationFormId' => $formId, |
| 65 | 'donationFormNonce' => wp_create_nonce("give_donation_form_nonce_{$formId}"), |
| 66 | 'sdkOptions' => $this->getPayPalSDKOptions($formId), |
| 67 | 'validateUrl' => (new GenerateDonationFormValidationRouteUrl())(), |
| 68 | ]; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * List of PayPal query parameters: https://developer.paypal.com/docs/checkout/reference/customize-sdk/#query-parameters |
| 73 | * |
| 74 | * @since 3.0.0 |
| 75 | * @throws Exception |
| 76 | */ |
| 77 | private function getPayPalSDKOptions(int $formId): array |
| 78 | { |
| 79 | /* @var MerchantDetail $merchantDetailModel */ |
| 80 | $merchantDetailModel = give(MerchantDetail::class); |
| 81 | |
| 82 | /* @var MerchantDetails $merchantDetailRepository */ |
| 83 | $merchantDetailRepository = give(MerchantDetails::class); |
| 84 | |
| 85 | // Add hosted fields if payment field type is auto and connect account type supports custom payments. |
| 86 | $paymentFieldType = give_get_option('paypal_payment_field_type', 'auto'); |
| 87 | $paymentComponents[] = 'buttons'; |
| 88 | if ('auto' === $paymentFieldType && $merchantDetailModel->supportsCustomPayments) { |
| 89 | $paymentComponents[] = 'hosted-fields'; |
| 90 | } |
| 91 | |
| 92 | $data = [ |
| 93 | // data-namespace is required for multiple PayPal SDKs to load in harmony. |
| 94 | 'data-namespace' => 'givewp/paypal-commerce', |
| 95 | 'client-id' => $merchantDetailModel->clientId, |
| 96 | 'merchant-id' => $merchantDetailModel->merchantIdInPayPal, |
| 97 | 'components' => implode(',', $paymentComponents), |
| 98 | 'disable-funding' => 'credit', |
| 99 | 'intent' => 'capture', |
| 100 | 'vault' => 'false', |
| 101 | 'data-partner-attribution-id' => give('PAYPAL_COMMERCE_ATTRIBUTION_ID'), |
| 102 | 'data-client-token' => $merchantDetailRepository->getClientToken(), |
| 103 | 'currency' => give_get_currency($formId), |
| 104 | ]; |
| 105 | |
| 106 | if (give_is_setting_enabled(give_get_option('paypal_commerce_accept_venmo', 'disabled'))) { |
| 107 | $data['enable-funding'] = 'venmo'; |
| 108 | } |
| 109 | |
| 110 | return $data; |
| 111 | } |
| 112 | } |
| 113 |