Skip to content

Commit

Permalink
Add postId to Stripe connection URL state param
Browse files Browse the repository at this point in the history
  • Loading branch information
sirreal committed Sep 3, 2019
1 parent 308b9c4 commit 4a016a1
Showing 1 changed file with 50 additions and 5 deletions.
55 changes: 50 additions & 5 deletions extensions/blocks/recurring-payments/edit.jsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
/**
* External dependencies
*/

import classnames from 'classnames';
import SubmitButton from '../../shared/submit-button';
import apiFetch from '@wordpress/api-fetch';
import { __, sprintf } from '@wordpress/i18n';
import { trimEnd } from 'lodash';
import formatCurrency, { getCurrencyDefaults } from '@automattic/format-currency';
import { addQueryArgs, getQueryArg, isURL } from '@wordpress/url';
import { compose } from '@wordpress/compose';
import { withSelect } from '@wordpress/data';

import {
Button,
Expand Down Expand Up @@ -91,7 +93,14 @@ class MembershipsButtonEdit extends Component {
const connected = result.connected_account_id
? API_STATE_CONNECTED
: API_STATE_NOTCONNECTED;
this.setState( { connected, connectURL, products, shouldUpgrade, upgradeURL, siteSlug } );
this.setState( {
connected,
connectURL,
products,
shouldUpgrade,
upgradeURL,
siteSlug,
} );
},
result => {
const connectURL = null;
Expand Down Expand Up @@ -313,9 +322,36 @@ class MembershipsButtonEdit extends Component {
);
};

getConnectUrl() {
const { postId } = this.props;
const { connectURL } = this.state;

if ( ! postId || ! isURL( connectURL ) ) {
return null;
}

const state = getQueryArg( connectURL, 'state' );
let decodedState;

try {
decodedState = JSON.parse( atob( state ) );
} catch ( err ) {
if ( process.env.NODE_ENV !== 'production' ) {
console.error( err ); // eslint-disable-line no-console
}
return connectURL;
}

decodedState.from_editor_post_id = postId;

return addQueryArgs( connectURL, { state: btoa( JSON.stringify( decodedState ) ) } );
}

render = () => {
const { className, notices } = this.props;
const { connected, connectURL, products } = this.state;
const { connected, products } = this.state;

const stripeConnectUrl = this.getConnectUrl();

const inspectorControls = (
<InspectorControls>
Expand Down Expand Up @@ -400,7 +436,13 @@ class MembershipsButtonEdit extends Component {
'jetpack'
) }
</p>
<Button isDefault isLarge href={ connectURL } target="_blank">
<Button
isDefault
isLarge
disabled={ ! stripeConnectUrl }
href={ stripeConnectUrl }
target="_blank"
>
{ __( 'Connect to Stripe or set up an account', 'jetpack' ) }
</Button>
<br />
Expand Down Expand Up @@ -458,4 +500,7 @@ class MembershipsButtonEdit extends Component {
};
}

export default withNotices( MembershipsButtonEdit );
export default compose( [
withSelect( select => ( { postId: select( 'core/editor' ).getCurrentPostId() } ) ),
withNotices,
] )( MembershipsButtonEdit );

0 comments on commit 4a016a1

Please sign in to comment.