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