Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@rattrayalex-stripe
Copy link
Contributor

Resolves reactioncommerce/reaction-feature-requests#57
Impact: minor
Type: feature

Issue

reactioncommerce/reaction-feature-requests#57

Solution

I hardcode the name "ReactionCommerce" in a few places, which isn't great - do you have a suggestion for a place to reference that as a constant?

Otherwise, I pull version and url from package.json, which seems to be what reaction does elsewhere as well.

Breaking changes

none.

Testing

none; do you have suggestions? I could write a nock test and copy/paste it in 5 places, but that doesn't feel great. I could also add a unit test for getStripeInstanceForShop or add a case to an existing test file, such as stripeCapturePayment.test.js.

@rattrayalex-stripe rattrayalex-stripe force-pushed the fix-57-rattrayalex-stripe-set-stripe-app-info branch from 4e1df50 to 0c998ab Compare January 28, 2019 22:30
@spencern spencern requested a review from aldeed January 28, 2019 22:58
Copy link
Contributor

@aldeed aldeed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rattrayalex-stripe Thanks for the PR! As far as I understand, your goal is for everyone who builds a Reaction-based app and uses Stripe to be sending the same name value, correct? If so, hard coding the value actually makes more sense than an environment config because you want to discourage anyone changing it. In fact, I'd add a comment above the name property requesting that they do not change it.

The one other change I request is to create a single util file in which the object is built. You can add a file /imports/utils/appInfo.js:

import packageJson from "/package.json";

/**
 * This is used by Stripe plugins, but may also be useful for other code.
 * Do not change the `name`.
 */
const appInfo = {
  name: "ReactionCommerce",
  version: packageJson.version,
  url: packageJson.url
};

export default appInfo;

Then import that everywhere and do stripe.setAppInfo(appInfo);

@rattrayalex-stripe
Copy link
Contributor Author

rattrayalex-stripe commented Jan 29, 2019

Thanks so much, @aldeed !

Staring at this a bit more, I had a few thoughts:

  • It seems that the "marketplace" plugin and the "payments-stripe" plugin are fairly distinct. We should probably identify them as such (we can implement logic on our side to classify "ReactionCommerceMarketplace" as a derivative of "ReactionCommerce").
  • Thus, probably best not to share code directly, especially given the preexising pattern of similar functions duplicated across the two plugins.
  • I was also a bit nervous about using the same appInfo in non-Stripe areas, since it may lead to abuse/misuse such as additional fields getting passed in to Stripe because they were useful in other areas.
  • It should not be the path of least resistance for new usages of stripe within either plugin to be called without setAppInfo; duplicating that function call in many places made me nervous. Relatedly, Reaction may want to add other settings for each stripe instance in the future, such as stripe.setApiVersion(), stripe.setHttpAgent(), or stripe.setClientId().

Accordingly, I centralized things a bit with one getStripeInstance function per plugin. Hopefully this should make it easier in the future to keep things somewhat centralized. In preparation, I found getStripeApi to be a slightly confusing name when in parallel to getStripeInstance, and renamed it to getStripeApiKey.

Additionally, I'm considering a follow-on refactor to deduplicate/unify getStripeInstanceFromShop and getStripeApiKey, though I'm not yet 100% sure of the implications/benefits of that. Curious for your thoughts on that!

Totally happy to go with your originally suggested solution of /imports/utils/appInfo.js if preferred!

Mind taking another look @aldeed ?

Oh, and to be clear,

As far as I understand, your goal is for everyone who builds a Reaction-based app and uses Stripe to be sending the same name value, correct?

Yes that's correct πŸ™‚

@rattrayalex-stripe rattrayalex-stripe force-pushed the fix-57-rattrayalex-stripe-set-stripe-app-info branch from 94e2fab to 0839533 Compare January 29, 2019 21:01
Copy link
Contributor

@aldeed aldeed left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rattrayalex-stripe All good points.

  • The top of getStripeInstanceForShop is essentially the same as getStripeApiKey, so they could be combined, but you'd have to have it return the applicationFee as well which is something that only applies to marketplace. (Marketplace supports Stripe Connect so that sub-shops can use the primary shop's Stripe account and the primary shop can take a cut of sales. That's currently the only difference.)
  • The || stripePackage.settings.connectAuth.access_token part in getStripeApiKey in the non-Marketplace package shouldn't be necessary because as I mentioned, Connect is only used in a marketplace setup. Those plugins used to be combined so it's probably a leftover from then.
  • FYI The reason there's so much code duplicated is because the marketplace plugin is no longer supported and the plan is to move it to a separate community-owned repo in the near future.
  • Combining getStripeApiKey and getStripeInstance calls into await getStripeInstance(context, shopId) in both plugins would make sense to me because looking up the API key always comes right before getting the instance. And passing in the plugin name from payment (paymentMethod.paymentPluginName) is actually not correct since it could theoretically change and we'd always want to use the current name for lookup. Instead it should be from a constant.

Thanks for cleaning up this code while doing this.

@rattrayalex-stripe rattrayalex-stripe force-pushed the fix-57-rattrayalex-stripe-set-stripe-app-info branch from 0839533 to 101ccef Compare January 29, 2019 23:27
@rattrayalex-stripe
Copy link
Contributor Author

Gotcha, thanks @aldeed !

The context around the future of the marketplace plugin is quite helpful; thanks! I think the code duplication we have now is appropriate in that case (and a separate plugin name is appropriate).

I like the idea of combining them as you outlined, and the context around paymentMethod.paymentPluginName and || stripePackage.settings.connectAuth.access_token. I'd be happy to do that in this PR if you're confident it's completely safe - namely, that there would not be any Reaction users who have set settings.connectAuth.access_token in their stripe-payments plugin. If you're not quite sure, I can do in a follow-on, or just retain the connect fallback.

Thoughts?

@aldeed
Copy link
Contributor

aldeed commented Jan 30, 2019

@rattrayalex-stripe People not running marketplace should never have connectAuth set because it's only set in the marketplace plugin BUT in the interest of not trying to do too much in one PR, let's leave those lines as-is.

You can safely make the paymentPluginName change, though. Use the PACKAGE_NAME constant. There are actually several files with that constant defined, so you could instead define it in a single file (within each plugin) and import everywhere.

@rattrayalex-stripe
Copy link
Contributor Author

Gotcha! I think I'd rather do the plugin/package name change in the next PR as well then, if that's okay, since it'll be touching the same code.

How does the rest of this PR look to you? I see a build failure from snyk, not sure what that means?

@aldeed aldeed changed the base branch from master to develop January 31, 2019 15:50
@aldeed
Copy link
Contributor

aldeed commented Jan 31, 2019

I switched the base to develop and fixed conflicts. Tested to verify checkout with Stripe still works. This will be merged if all the checks pass again.

For future PRs be sure to branch from and direct PRs to develop unless it's a critical fix to the current release.

@rattrayalex-stripe
Copy link
Contributor Author

Awesome, thanks so much @aldeed !

Follow-up PR at #4951

@rattrayalex-stripe rattrayalex-stripe deleted the fix-57-rattrayalex-stripe-set-stripe-app-info branch January 31, 2019 19:43
@rattrayalex-stripe
Copy link
Contributor Author

By the way, your contribution guide was super helpful and clear - it looks like the use of a develop branch isn't documented at https://docs.reactioncommerce.com/docs/next/contributing-to-reaction#step-3-prepare-a-pull-request-for-review - might be nice to add it there too.

@jeffcorpuz jeffcorpuz mentioned this pull request Mar 1, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants