Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Epic: Update PayPal gateway to use card fields API #7853

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@
"@givewp/form-builder-library": "^1.7.1",
"@hookform/error-message": "^2.0.1",
"@hookform/resolvers": "^2.9.10",
"@paypal/paypal-js": "^5.1.4",
"@paypal/react-paypal-js": "^7.8.2",
"@paypal/paypal-js": "^8.2.0",
"@paypal/react-paypal-js": "^8.8.2",
"@picocss/pico": "^1.5.7",
"@stripe/react-stripe-js": "^2.1.0",
"@stripe/stripe-js": "^1.52.0",
Expand Down
25 changes: 17 additions & 8 deletions src/DonationForms/resources/app/hooks/useFormData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,26 @@ const amountToMinorUnit = (amount: string, currency: string) => {
/**
* Donation total calculation
*
* @unreleased Round return value
* @since 4.0.0
*/
const getAmountTotal = (totals: DonationTotals, amount: number) =>
Number(
Object.values({
...totals,
amount,
}).reduce((total: number, amount: number) => {
return total + amount;
}, 0)
normalizeAmount(
Number(
Object.values({
...totals,
amount,
}).reduce((total: number, amount: number) => {
return total + amount;
}, 0)
)
);

/**
* Subscription total calculation
* TODO: figure out which totals will be included in subscriptions
*
* @unreleased Round return value
* @since 4.0.0
*/
const getSubscriptionTotal = (totals: DonationTotals, amount: number) => {
Expand All @@ -74,9 +78,14 @@ const getSubscriptionTotal = (totals: DonationTotals, amount: number) => {
}
}

return Number(total + amount);
return normalizeAmount(Number(total + amount));
};

/**
* @unreleased
*/
const normalizeAmount = (amount: number) => Math.round(amount * 100) / 100;

/**
* @since 4.0.0
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Exception;
use Give\DonationForms\Actions\GenerateDonationFormValidationRouteUrl;
use Give\Framework\Support\Scripts\Concerns\HasScriptAssetFile;
use Give\Helpers\Form\Utils;
use Give\Helpers\Language;
use Give\PaymentGateways\PayPalCommerce\Models\MerchantDetail;
use Give\PaymentGateways\PayPalCommerce\PayPalCommerce;
Expand Down Expand Up @@ -85,25 +86,52 @@ private function getPayPalSDKOptions(int $formId): array
// Add hosted fields if payment field type is auto and connect account type supports custom payments.
$paymentFieldType = give_get_option('paypal_payment_field_type', 'auto');
$paymentComponents[] = 'buttons';
if ('auto' === $paymentFieldType && $merchantDetailModel->supportsCustomPayments) {
$paymentComponents[] = 'hosted-fields';
}

$formIsV3 = Utils::isV3Form($formId);
$venmoEnabled = give_is_setting_enabled(give_get_option('paypal_commerce_accept_venmo', 'disabled'));
$fieldsEnabled = 'auto' === $paymentFieldType && $merchantDetailModel->supportsCustomPayments;

$data = [
// data-namespace is required for multiple PayPal SDKs to load in harmony.
'data-namespace' => 'givewp/paypal-commerce',
'client-id' => $merchantDetailModel->clientId,
'components' => implode(',', $paymentComponents),
'disable-funding' => 'credit',
'intent' => 'capture',
'vault' => 'false',
'data-partner-attribution-id' => give('PAYPAL_COMMERCE_ATTRIBUTION_ID'),
'data-client-token' => $merchantDetailRepository->getClientToken(),
'currency' => give_get_currency($formId),
];

if (give_is_setting_enabled(give_get_option('paypal_commerce_accept_venmo', 'disabled'))) {
$data['enable-funding'] = 'venmo';
if ($formIsV3) {
if ($fieldsEnabled) {
$paymentComponents[] = 'card-fields';
}

$data = array_merge($data, [
'dataNamespace' => 'givewp/paypal-commerce',
'clientId' => $merchantDetailModel->clientId,
'disableFunding' => 'credit',
'dataPartnerAttributionId' => give('PAYPAL_COMMERCE_ATTRIBUTION_ID'),
'dataClientToken' => $merchantDetailRepository->getClientToken(),
'components' => implode(',', $paymentComponents),
]);

if ($venmoEnabled){
$data['enableFunding'] = 'venmo';
}
} else {
if ($fieldsEnabled) {
$paymentComponents[] = 'hosted-fields';
}

$data = array_merge($data, [
// data-namespace is required for multiple PayPal SDKs to load in harmony.
'data-namespace' => 'givewp/paypal-commerce',
'client-id' => $merchantDetailModel->clientId,
'disable-funding' => 'credit',
'data-partner-attribution-id' => give('PAYPAL_COMMERCE_ATTRIBUTION_ID'),
'data-client-token' => $merchantDetailRepository->getClientToken(),
'components' => implode(',', $paymentComponents),
]);

if ($venmoEnabled){
$data['enable-funding'] = 'venmo';
}
}

return $data;
Expand Down
Loading