-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(payments-plugin): Add multi currency support for braintree plugin #3239
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
Open
kkerti
wants to merge
8
commits into
vendure-ecommerce:minor
Choose a base branch
from
kkerti:feat/braintree-plugin-multi-currency
base: minor
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+325
−9
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c859cd1
feat(payments-plugin): Add multi-currency support to braintree payments
kkerti 097e150
feat(payments-plugin): Added readme and dev server script
kkerti 8c68d8f
Merge remote-tracking branch 'origin/master' into feat/braintree-plug…
kkerti f3b1fe6
Merge commit '07c174c391f031f8c0526d700c1a6135820e4a56' into feat/bra…
kkerti 7746af4
feat(payments-plugin): Fix typo
kkerti c2049ce
Merge branch 'minor' into feat/braintree-plugin-multi-currency
dlhck acdbd2f
fix(payments-plugin): Remove console.log
kkerti 2eb2629
Merge branch 'minor' into feat/braintree-plugin-multi-currency
dlhck File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| import { AdminUiPlugin } from '@vendure/admin-ui-plugin'; | ||
| import { | ||
| ChannelService, | ||
| configureDefaultOrderProcess, | ||
| DefaultLogger, | ||
| LanguageCode, | ||
| Logger, | ||
| LogLevel, | ||
| mergeConfig, | ||
| OrderService, | ||
| RequestContext, | ||
| } from '@vendure/core'; | ||
| import { | ||
| createTestEnvironment, | ||
| registerInitializer, | ||
| SimpleGraphQLClient, | ||
| SqljsInitializer, | ||
| testConfig, | ||
| } from '@vendure/testing'; | ||
| import gql from 'graphql-tag'; | ||
| import path from 'path'; | ||
|
|
||
| import { initialData } from '../../../e2e-common/e2e-initial-data'; | ||
| import { BraintreePlugin } from '../src/braintree'; | ||
| import { braintreePaymentMethodHandler } from '../src/braintree/braintree.handler'; | ||
|
|
||
| /* eslint-disable */ | ||
| import { CREATE_PAYMENT_METHOD } from './graphql/admin-queries'; | ||
| import { | ||
| CreatePaymentMethodMutation, | ||
| CreatePaymentMethodMutationVariables, | ||
| } from './graphql/generated-admin-types'; | ||
| import { | ||
| AddItemToOrderMutation, | ||
| AddItemToOrderMutationVariables, | ||
| AddPaymentToOrderMutation, | ||
| AddPaymentToOrderMutationVariables, | ||
| } from './graphql/generated-shop-types'; | ||
| import { ADD_ITEM_TO_ORDER, ADD_PAYMENT } from './graphql/shop-queries'; | ||
| import { GENERATE_BRAINTREE_CLIENT_TOKEN, proceedToArrangingPayment, setShipping } from './payment-helpers'; | ||
| import braintree, { Environment, Test } from 'braintree'; | ||
| import { BraintreeTestPlugin } from './fixtures/braintree-checkout-test.plugin'; | ||
|
|
||
| export let clientToken: string; | ||
| export let exposedShopClient: SimpleGraphQLClient; | ||
|
|
||
| /** | ||
| * The actual starting of the dev server | ||
| */ | ||
| (async () => { | ||
| require('dotenv').config(); | ||
|
|
||
| registerInitializer('sqljs', new SqljsInitializer(path.join(__dirname, '__data__'))); | ||
| const config = mergeConfig(testConfig, { | ||
| authOptions: { | ||
| tokenMethod: ['bearer', 'cookie'], | ||
| cookieOptions: { | ||
| secret: 'cookie-secret', | ||
| }, | ||
| }, | ||
| plugins: [ | ||
| ...testConfig.plugins, | ||
| AdminUiPlugin.init({ | ||
| route: 'admin', | ||
| port: 5001, | ||
| }), | ||
| BraintreePlugin.init({ | ||
| storeCustomersInBraintree: false, | ||
| environment: Environment.Sandbox, | ||
| merchantAccountIds: { | ||
| USD: process.env.BRAINTREE_MERCHANT_ACCOUNT_ID_USD, | ||
| EUR: process.env.BRAINTREE_MERCHANT_ACCOUNT_ID_EUR, | ||
| }, | ||
| }), | ||
| BraintreeTestPlugin, | ||
| ], | ||
| logger: new DefaultLogger({ level: LogLevel.Debug }), | ||
| }); | ||
| const { server, shopClient, adminClient } = createTestEnvironment(config as any); | ||
| exposedShopClient = shopClient; | ||
| await server.init({ | ||
| initialData, | ||
| productsCsvPath: path.join(__dirname, 'fixtures/e2e-products-minimal.csv'), | ||
| customerCount: 1, | ||
| }); | ||
| // Create method | ||
| await adminClient.asSuperAdmin(); | ||
| await adminClient.query<CreatePaymentMethodMutation, CreatePaymentMethodMutationVariables>( | ||
| CREATE_PAYMENT_METHOD, | ||
| { | ||
| input: { | ||
| code: 'braintree-payment-method', | ||
| enabled: true, | ||
| translations: [ | ||
| { | ||
| name: 'Braintree', | ||
| description: 'This is a Braintree test payment method', | ||
| languageCode: LanguageCode.en, | ||
| }, | ||
| ], | ||
| handler: { | ||
| code: braintreePaymentMethodHandler.code, | ||
| arguments: [ | ||
| { name: 'privateKey', value: process.env.BRAINTREE_PRIVATE_KEY! }, | ||
| { name: 'publicKey', value: process.env.BRAINTREE_PUBLIC_KEY! }, | ||
| { name: 'merchantId', value: process.env.BRAINTREE_MERCHANT_ID! }, | ||
| ], | ||
| }, | ||
| }, | ||
| }, | ||
| ); | ||
| // Prepare order for payment | ||
| await shopClient.asUserWithCredentials('[email protected]', 'test'); | ||
| await shopClient.query<AddItemToOrderMutation, AddItemToOrderMutationVariables>(ADD_ITEM_TO_ORDER, { | ||
| productVariantId: 'T_1', | ||
| quantity: 1, | ||
| }); | ||
| const ctx = new RequestContext({ | ||
| apiType: 'admin', | ||
| isAuthorized: true, | ||
| authorizedAsOwnerOnly: false, | ||
| channel: await server.app.get(ChannelService).getDefaultChannel(), | ||
| }); | ||
| await server.app.get(OrderService).addSurchargeToOrder(ctx, 1, { | ||
| description: 'Negative test surcharge', | ||
| listPrice: -20000, | ||
| }); | ||
| await setShipping(shopClient); | ||
| const { generateBraintreeClientToken } = await shopClient.query(GENERATE_BRAINTREE_CLIENT_TOKEN); | ||
| clientToken = generateBraintreeClientToken; | ||
| Logger.info('http://localhost:3050/checkout', 'Braintree DevServer'); | ||
| })(); | ||
114 changes: 114 additions & 0 deletions
114
packages/payments-plugin/e2e/fixtures/braintree-checkout-test.plugin.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| /* eslint-disable */ | ||
| import { Body, Controller, Get, Post, Res } from '@nestjs/common'; | ||
| import { PluginCommonModule, VendurePlugin } from '@vendure/core'; | ||
| import { Response } from 'express'; | ||
|
|
||
| import { clientToken, exposedShopClient } from '../braintree-dev-server'; | ||
| import { | ||
| AddPaymentToOrderMutation, | ||
| AddPaymentToOrderMutationVariables, | ||
| } from '../graphql/generated-shop-types'; | ||
| import { ADD_PAYMENT } from '../graphql/shop-queries'; | ||
| import { proceedToArrangingPayment } from '../payment-helpers'; | ||
| /** | ||
| * This test controller returns the Braintree drop-in checkout page | ||
| * with the client secret generated by the dev-server | ||
| */ | ||
| @Controller() | ||
| export class BraintreeTestCheckoutController { | ||
| @Get('checkout') | ||
| async client(@Res() res: Response): Promise<void> { | ||
| res.send(` | ||
| <head> | ||
| <title>Checkout</title> | ||
| <script src="https://js.braintreegateway.com/web/dropin/1.33.3/js/dropin.min.js"></script> | ||
| </head> | ||
| <html> | ||
|
|
||
| <div id="dropin-container"></div> | ||
| <button id="submit-button">Purchase</button> | ||
| <div id="result"/> | ||
|
|
||
| <script> | ||
| var submitButton = document.querySelector('#submit-button'); | ||
| braintree.dropin.create({ | ||
| authorization: "${clientToken}", | ||
| container: '#dropin-container', | ||
| dataCollector: true, | ||
| paypal: { | ||
| flow: 'checkout', | ||
| amount: 100, | ||
| currency: 'GBP', | ||
| }, | ||
| }, function (err, dropinInstance) { | ||
|
|
||
| submitButton.addEventListener('click', function () { | ||
| dropinInstance.requestPaymentMethod(async function (err, payload) { | ||
| sendPayloadToServer(payload) | ||
| }); | ||
| }); | ||
|
|
||
| if (dropinInstance.isPaymentMethodRequestable()) { | ||
| // This will be true if you generated the client token | ||
| // with a customer ID and there is a saved payment method | ||
| // available to tokenize with that customer. | ||
| submitButton.removeAttribute('disabled'); | ||
| } | ||
|
|
||
| dropinInstance.on('paymentMethodRequestable', function (event) { | ||
| console.log(event.type); // The type of Payment Method, e.g 'CreditCard', 'PayPalAccount'. | ||
| console.log(event.paymentMethodIsSelected); // true if a customer has selected a payment method when paymentMethodRequestable fires | ||
| submitButton.removeAttribute('disabled'); | ||
| }); | ||
|
|
||
| dropinInstance.on('noPaymentMethodRequestable', function () { | ||
| submitButton.setAttribute('disabled', true); | ||
| }); | ||
| }); | ||
|
|
||
| async function sendPayloadToServer(payload) { | ||
| const response = await fetch('checkout', { | ||
| method: 'POST', | ||
| headers: { | ||
| 'Content-Type': 'application/json', | ||
| 'Accept': 'application/json', | ||
| 'Credentials': 'include', | ||
| }, | ||
| body: JSON.stringify(payload) | ||
| }) | ||
| .then(res => res.json()) | ||
| .catch(err => console.error(err)) | ||
|
|
||
| document.querySelector('#result').innerHTML = JSON.stringify(response) | ||
| console.log(response) | ||
|
|
||
| } | ||
| </script> | ||
|
|
||
| </html> | ||
| `); | ||
| } | ||
| @Post('checkout') | ||
| async test(@Body() body: Request, @Res() res: Response): Promise<void> { | ||
| await proceedToArrangingPayment(exposedShopClient); | ||
| const { addPaymentToOrder } = await exposedShopClient.query< | ||
| AddPaymentToOrderMutation, | ||
| AddPaymentToOrderMutationVariables | ||
| >(ADD_PAYMENT, { | ||
| input: { | ||
| method: 'braintree-payment-method', | ||
| metadata: body, | ||
| }, | ||
| }); | ||
| res.send(addPaymentToOrder); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Test plugin for serving the Stripe intent checkout page | ||
| */ | ||
| @VendurePlugin({ | ||
| imports: [PluginCommonModule], | ||
| controllers: [BraintreeTestCheckoutController], | ||
| }) | ||
| export class BraintreeTestPlugin {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add validation for required environment variables.
The required Braintree credentials are used without validation. Consider adding checks to ensure they're properly configured.
🤖 Prompt for AI Agents