2019-10-03 16:03:29 +00:00
/ * *
* External dependencies
* /
2020-04-24 00:28:29 +00:00
import { _ _ } from '@wordpress/i18n' ;
2019-10-03 16:03:29 +00:00
import { Component , Fragment } from '@wordpress/element' ;
import { compose } from '@wordpress/compose' ;
import apiFetch from '@wordpress/api-fetch' ;
import { withDispatch } from '@wordpress/data' ;
import interpolateComponents from 'interpolate-components' ;
2020-04-24 00:28:29 +00:00
import { Button } from '@wordpress/components' ;
2019-10-03 16:03:29 +00:00
import { get } from 'lodash' ;
/ * *
* WooCommerce dependencies
* /
2020-03-15 21:45:19 +00:00
import { Form , Link , Stepper , TextControl } from '@woocommerce/components' ;
import { getAdminLink } from '@woocommerce/wc-admin-settings' ;
import { getQuery } from '@woocommerce/navigation' ;
2019-10-03 16:03:29 +00:00
import { WCS _NAMESPACE } from 'wc-api/constants' ;
2020-03-15 21:45:19 +00:00
import withSelect from 'wc-api/with-select' ;
2020-04-16 23:58:36 +00:00
import { PLUGINS _STORE _NAME } from '@woocommerce/data' ;
2020-03-15 21:45:19 +00:00
2019-10-03 16:03:29 +00:00
class Stripe extends Component {
constructor ( props ) {
super ( props ) ;
this . state = {
2020-04-24 00:28:29 +00:00
oAuthConnectFailed : false ,
2020-03-15 21:45:19 +00:00
connectURL : null ,
2020-04-24 00:28:29 +00:00
isPending : false ,
2019-10-03 16:03:29 +00:00
} ;
this . updateSettings = this . updateSettings . bind ( this ) ;
}
componentDidMount ( ) {
2020-03-15 21:45:19 +00:00
const { stripeSettings } = this . props ;
2019-10-03 16:03:29 +00:00
const query = getQuery ( ) ;
// Handle redirect back from Stripe.
2020-02-14 02:23:21 +00:00
if ( query [ 'stripe-connect' ] && query [ 'stripe-connect' ] === '1' ) {
const isStripeConnected =
stripeSettings . publishable _key && stripeSettings . secret _key ;
2019-10-03 16:03:29 +00:00
if ( isStripeConnected ) {
2020-03-15 21:45:19 +00:00
this . completeMethod ( ) ;
2019-10-03 16:03:29 +00:00
return ;
}
}
2020-03-15 21:45:19 +00:00
if ( ! this . requiresManualConfig ( ) ) {
2019-10-03 16:03:29 +00:00
this . fetchOAuthConnectURL ( ) ;
}
}
2020-03-15 21:45:19 +00:00
componentDidUpdate ( prevProps ) {
const {
2020-03-16 11:09:29 +00:00
activePlugins ,
2020-03-15 21:45:19 +00:00
createNotice ,
isOptionsRequesting ,
hasOptionsError ,
} = this . props ;
if ( prevProps . isOptionsRequesting && ! isOptionsRequesting ) {
if ( ! hasOptionsError ) {
this . completeMethod ( ) ;
} else {
createNotice (
'error' ,
_ _ (
'There was a problem saving your payment setings' ,
'woocommerce-admin'
)
) ;
}
2019-10-03 16:03:29 +00:00
}
2020-03-16 11:09:29 +00:00
if (
! prevProps . activePlugins . includes (
'woocommerce-gateway-stripe'
) &&
activePlugins . includes ( 'woocommerce-gateway-stripe' )
) {
this . fetchOAuthConnectURL ( ) ;
}
2019-10-03 16:03:29 +00:00
}
2020-03-15 21:45:19 +00:00
requiresManualConfig ( ) {
const { activePlugins , isJetpackConnected } = this . props ;
2020-04-24 00:28:29 +00:00
const { oAuthConnectFailed } = this . state ;
2020-03-15 21:45:19 +00:00
return (
! isJetpackConnected ||
! activePlugins . includes ( 'woocommerce-services' ) ||
2020-04-24 00:28:29 +00:00
oAuthConnectFailed
2020-03-15 21:45:19 +00:00
) ;
}
completeMethod ( ) {
const { createNotice , markConfigured } = this . props ;
this . setState ( { isPending : false } ) ;
createNotice (
'success' ,
_ _ ( 'Stripe connected successfully.' , 'woocommerce-admin' )
) ;
markConfigured ( 'stripe' ) ;
}
2019-10-03 16:03:29 +00:00
async fetchOAuthConnectURL ( ) {
2020-03-16 11:09:29 +00:00
const { activePlugins } = this . props ;
if ( ! activePlugins . includes ( 'woocommerce-gateway-stripe' ) ) {
return ;
}
2019-10-03 16:03:29 +00:00
try {
2020-03-15 21:45:19 +00:00
this . setState ( { isPending : true } ) ;
2019-10-03 16:03:29 +00:00
const result = await apiFetch ( {
path : WCS _NAMESPACE + '/connect/stripe/oauth/init' ,
method : 'POST' ,
data : {
2020-03-15 21:45:19 +00:00
returnUrl : getAdminLink (
'admin.php?page=wc-admin&task=payments&method=stripe&stripe-connect=1'
) ,
2019-10-03 16:03:29 +00:00
} ,
} ) ;
if ( ! result || ! result . oauthUrl ) {
this . setState ( {
2020-04-24 00:28:29 +00:00
oAuthConnectFailed : true ,
2020-03-15 21:45:19 +00:00
isPending : false ,
2019-10-03 16:03:29 +00:00
} ) ;
return ;
}
this . setState ( {
connectURL : result . oauthUrl ,
2020-03-15 21:45:19 +00:00
isPending : false ,
2019-10-03 16:03:29 +00:00
} ) ;
} catch ( error ) {
this . setState ( {
2020-04-24 00:28:29 +00:00
oAuthConnectFailed : true ,
2020-03-15 21:45:19 +00:00
isPending : false ,
2019-10-03 16:03:29 +00:00
} ) ;
}
}
2020-04-24 00:28:29 +00:00
renderConnectButton ( ) {
2020-03-15 21:45:19 +00:00
const { connectURL } = this . state ;
2019-10-03 16:03:29 +00:00
return (
2020-04-24 00:28:29 +00:00
< Button isPrimary isDefault href = { connectURL } >
{ _ _ ( 'Connect' , 'woocommerce-admin' ) }
< / B u t t o n >
2019-10-03 16:03:29 +00:00
) ;
}
2020-03-15 21:45:19 +00:00
updateSettings ( values ) {
const { updateOptions , stripeSettings } = this . props ;
2019-10-03 16:03:29 +00:00
2020-03-15 21:45:19 +00:00
updateOptions ( {
2019-10-03 16:03:29 +00:00
woocommerce _stripe _settings : {
2020-03-15 21:45:19 +00:00
... stripeSettings ,
2019-10-03 16:03:29 +00:00
publishable _key : values . publishable _key ,
secret _key : values . secret _key ,
enabled : 'yes' ,
} ,
} ) ;
}
getInitialConfigValues ( ) {
return {
publishable _key : '' ,
secret _key : '' ,
} ;
}
2020-03-15 21:45:19 +00:00
validateManualConfig ( values ) {
2019-10-03 16:03:29 +00:00
const errors = { } ;
2020-03-17 17:22:42 +00:00
if ( values . publishable _key . match ( /^pk_live_/ ) === null ) {
2020-02-14 02:23:21 +00:00
errors . publishable _key = _ _ (
2020-03-17 17:22:42 +00:00
'Please enter a valid publishable key. Valid keys start with "pk_live".' ,
2020-02-14 02:23:21 +00:00
'woocommerce-admin'
) ;
2019-10-03 16:03:29 +00:00
}
2020-03-17 17:22:42 +00:00
if ( values . secret _key . match ( /^[rs]k_live_/ ) === null ) {
2020-02-14 02:23:21 +00:00
errors . secret _key = _ _ (
2020-03-17 17:22:42 +00:00
'Please enter a valid secret key. Valid keys start with "sk_live" or "rk_live".' ,
2020-02-14 02:23:21 +00:00
'woocommerce-admin'
) ;
2019-10-03 16:03:29 +00:00
}
return errors ;
}
renderManualConfig ( ) {
2020-03-15 21:45:19 +00:00
const { isOptionsRequesting } = this . props ;
2019-10-03 16:03:29 +00:00
const stripeHelp = interpolateComponents ( {
mixedString : _ _ (
2020-03-15 21:45:19 +00:00
'Your API details can be obtained from your {{docsLink}}Stripe account{{/docsLink}}. Don’ t have a Stripe account? {{registerLink}}Create one.{{/registerLink}}' ,
2019-10-03 16:03:29 +00:00
'woocommerce-admin'
) ,
components : {
2020-03-15 21:45:19 +00:00
docsLink : (
2020-02-14 02:23:21 +00:00
< Link
2020-03-15 21:45:19 +00:00
href = "https://stripe.com/docs/keys"
target = "_blank"
type = "external"
/ >
) ,
registerLink : (
< Link
href = "https://dashboard.stripe.com/register"
2020-02-14 02:23:21 +00:00
target = "_blank"
type = "external"
/ >
) ,
2019-10-03 16:03:29 +00:00
} ,
} ) ;
return (
< Form
initialValues = { this . getInitialConfigValues ( ) }
onSubmitCallback = { this . updateSettings }
2020-03-15 21:45:19 +00:00
validate = { this . validateManualConfig }
2019-10-03 16:03:29 +00:00
>
{ ( { getInputProps , handleSubmit } ) => {
return (
< Fragment >
< TextControl
2020-02-14 02:23:21 +00:00
label = { _ _ (
'Live Publishable Key' ,
'woocommerce-admin'
) }
2019-10-03 16:03:29 +00:00
required
{ ... getInputProps ( 'publishable_key' ) }
/ >
< TextControl
2020-02-14 02:23:21 +00:00
label = { _ _ (
'Live Secret Key' ,
'woocommerce-admin'
) }
2019-10-03 16:03:29 +00:00
required
{ ... getInputProps ( 'secret_key' ) }
/ >
< Button
2020-03-15 21:45:19 +00:00
isPrimary
isBusy = { isOptionsRequesting }
onClick = { handleSubmit }
2019-10-03 16:03:29 +00:00
>
2020-03-15 21:45:19 +00:00
{ _ _ ( 'Proceed' , 'woocommerce-admin' ) }
2019-10-03 16:03:29 +00:00
< / B u t t o n >
< p > { stripeHelp } < / p >
< / F r a g m e n t >
) ;
} }
< / F o r m >
) ;
}
2020-03-15 21:45:19 +00:00
getConnectStep ( ) {
2020-04-24 00:28:29 +00:00
const { connectURL , isPending , oAuthConnectFailed } = this . state ;
2020-03-15 21:45:19 +00:00
const connectStep = {
key : 'connect' ,
label : _ _ ( 'Connect your Stripe account' , 'woocommerce-admin' ) ,
} ;
2019-10-03 16:03:29 +00:00
2020-04-24 00:28:29 +00:00
if ( isPending ) {
return connectStep ;
2019-10-03 16:03:29 +00:00
}
2020-04-24 00:28:29 +00:00
if ( ! oAuthConnectFailed && connectURL ) {
2020-03-15 21:45:19 +00:00
return {
... connectStep ,
description : _ _ (
2020-04-24 00:28:29 +00:00
'A Stripe account is required to process payments.' ,
2020-03-15 21:45:19 +00:00
'woocommerce-admin'
) ,
2020-04-24 00:28:29 +00:00
content : this . renderConnectButton ( ) ,
2020-03-15 21:45:19 +00:00
} ;
2019-10-03 16:03:29 +00:00
}
2020-03-15 21:45:19 +00:00
return {
... connectStep ,
description : _ _ (
'Connect your store to your Stripe account. Don’ t have a Stripe account? Create one.' ,
'woocommerce-admin'
) ,
content : this . renderManualConfig ( ) ,
} ;
}
render ( ) {
const { installStep , isOptionsRequesting } = this . props ;
const { isPending } = this . state ;
return (
< Stepper
isVertical
isPending = {
! installStep . isComplete || isOptionsRequesting || isPending
}
currentStep = { installStep . isComplete ? 'connect' : 'install' }
steps = { [ installStep , this . getConnectStep ( ) ] }
/ >
) ;
2019-10-03 16:03:29 +00:00
}
}
export default compose (
2020-02-14 02:23:21 +00:00
withSelect ( ( select ) => {
2020-03-15 21:45:19 +00:00
const {
getOptions ,
getOptionsError ,
isUpdateOptionsRequesting ,
} = select ( 'wc-api' ) ;
2020-04-16 23:58:36 +00:00
const { getActivePlugins , isJetpackConnected } = select (
PLUGINS _STORE _NAME
) ;
2020-04-24 00:28:29 +00:00
const options = getOptions ( [ 'woocommerce_stripe_settings' ] ) ;
2020-03-15 21:45:19 +00:00
const stripeSettings = get (
2019-10-03 16:03:29 +00:00
options ,
2020-03-15 21:45:19 +00:00
[ 'woocommerce_stripe_settings' ] ,
[ ]
) ;
const isOptionsRequesting = Boolean (
isUpdateOptionsRequesting ( [ 'woocommerce_stripe_settings' ] )
) ;
const hasOptionsError = getOptionsError ( [
'woocommerce_stripe_settings' ,
] ) ;
return {
activePlugins : getActivePlugins ( ) ,
hasOptionsError ,
isJetpackConnected : isJetpackConnected ( ) ,
isOptionsRequesting ,
stripeSettings ,
2019-10-03 16:03:29 +00:00
} ;
} ) ,
2020-02-14 02:23:21 +00:00
withDispatch ( ( dispatch ) => {
2019-10-03 16:03:29 +00:00
const { createNotice } = dispatch ( 'core/notices' ) ;
const { updateOptions } = dispatch ( 'wc-api' ) ;
return {
createNotice ,
updateOptions ,
} ;
} )
) ( Stripe ) ;