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

feat(donate): handle woocommerce-memberships #1096

Merged
merged 2 commits into from
Apr 18, 2022
Merged
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
31 changes: 27 additions & 4 deletions src/blocks/donate/class-wp-rest-newspack-donate-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,40 @@ public function api_process_donation( $request ) {
}
}

$frequency = $request->get_param( 'frequency' );
$full_name = $request->get_param( 'full_name' );

$user_id = self::$current_user_id;

if ( 0 === $user_id ) {
$email_address = $request->get_param( 'email' );

if ( class_exists( 'Newspack\WooCommerce_Connection' ) && method_exists( 'Newspack\WooCommerce_Connection', 'set_up_membership' ) ) {
// Handle woocommerce-memberships integration, if there's no user logged in.
$user_id = \Newspack\WooCommerce_Connection::set_up_membership(
$email_address,
$full_name,
$frequency
);
if ( is_wp_error( $user_id ) ) {
return [ 'error' => wp_strip_all_tags( $user_id->get_error_message() ) ];
}
}
} else {
$email_address = get_userdata( $user_id )->user_email;
}

$response = \Newspack\Stripe_Connection::handle_donation(
[
'frequency' => $request->get_param( 'frequency' ),
'frequency' => $frequency,
'token_data' => $request->get_param( 'tokenData' ),
'email_address' => $request->get_param( 'email' ),
'full_name' => $request->get_param( 'full_name' ),
'email_address' => $email_address,
'full_name' => $full_name,
'amount' => $request->get_param( 'amount' ),
'client_metadata' => [
'clientId' => $request->get_param( 'clientId' ),
'newsletterOptIn' => $request->get_param( 'newsletter_opt_in' ),
'userId' => self::$current_user_id,
'userId' => $user_id,
],
'payment_metadata' => $payment_metadata,
'payment_method_id' => $request->get_param( 'payment_method_id' ),
Expand Down
27 changes: 25 additions & 2 deletions src/blocks/donate/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ function newspack_blocks_render_block_donate_footer( $attributes ) {
$client_id = Newspack_Popups_Segmentation::NEWSPACK_SEGMENTATION_CID_NAME;
}

$current_user = wp_get_current_user();
$user_email = '';
$user_display_name = '';
if ( 0 !== $current_user->ID ) {
$user_email = $current_user->user_email;
$user_display_name = $current_user->display_name;
}

ob_start();

?>
Expand All @@ -45,8 +53,23 @@ function newspack_blocks_render_block_donate_footer( $attributes ) {
<div class="stripe-payment__element stripe-payment__card"></div>
</div>
<div class="stripe-payment__row stripe-payment__row--flex">
<input required placeholder="<?php echo esc_html__( 'Email', 'newspack-blocks' ); ?>" type="email" name="email" value="">
<input required placeholder="<?php echo esc_html__( 'Full Name', 'newspack-blocks' ); ?>" type="text" name="full_name" value="">
<input
required
placeholder="<?php echo esc_html__( 'Email', 'newspack-blocks' ); ?>"
type="email"
name="email"
value="<?php echo esc_attr( $user_email ); ?>"
<?php if ( '' !== $user_email ) : ?>
readonly
<?php endif; ?>
>
<input
required
placeholder="<?php echo esc_html__( 'Full Name', 'newspack-blocks' ); ?>"
type="text"
name="full_name"
value="<?php echo esc_attr( $user_display_name ); ?>"
>
</div>
</div>
<?php if ( $is_rendering_fee_checkbox ) : ?>
Expand Down
4 changes: 4 additions & 0 deletions src/blocks/donate/view.scss
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
input[type='radio'] {
display: none;
}
input[readonly] {
background-color: $color__background-screen;
color: #666;
}

.freq-label,
.tier-label {
Expand Down