Changeset 2458620
- Timestamp:
- 01/19/2021 12:34:41 AM (5 years ago)
- Location:
- servicebot
- Files:
-
- 10 edited
- 1 copied
-
tags/2.0.5 (copied) (copied from servicebot/trunk)
-
tags/2.0.5/public/class-servicebot-public.php (modified) (9 diffs)
-
tags/2.0.5/public/widgets/class-servicebot-billing-page-widget.php (modified) (2 diffs)
-
tags/2.0.5/public/widgets/js/billflow-widget.js (modified) (3 diffs)
-
tags/2.0.5/public/widgets/js/servicebot-handle-response.js (modified) (1 diff)
-
tags/2.0.5/servicebot.php (modified) (2 diffs)
-
trunk/public/class-servicebot-public.php (modified) (9 diffs)
-
trunk/public/widgets/class-servicebot-billing-page-widget.php (modified) (2 diffs)
-
trunk/public/widgets/js/billflow-widget.js (modified) (3 diffs)
-
trunk/public/widgets/js/servicebot-handle-response.js (modified) (1 diff)
-
trunk/servicebot.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
servicebot/tags/2.0.5/public/class-servicebot-public.php
r2457925 r2458620 7 7 use Stripe\Event; 8 8 use Stripe\Webhook; 9 10 /** 11 * User meta data created by billflow 12 * billflow_created 13 * billflow_checkout_created 14 * billflow_stripe_webhook_created 15 * billflow_stripe_subscription_id 16 * 17 * User created default role to whatever the system default is. 18 */ 9 19 10 20 /** … … 114 124 $email = sanitize_email( $_POST['email'] ); 115 125 $name = sanitize_user( $_POST['name'] ); 116 $password = $_POST['password']; 126 $password = $_POST['password']; 127 $subscription_id = $_POST['subscription_id']; 117 128 118 129 $userdata = array( … … 120 131 'user_email' => $email, 121 132 'user_pass' => $password, 122 'role' => "subscriber"123 133 ); 124 134 125 135 $user_id = wp_insert_user( $userdata ); 126 136 127 137 //On success 128 138 if ( ! is_wp_error( $user_id ) ) { 139 140 // add user meta to note that this is created by Stripe webhook 141 update_user_meta($user_id, "billflow_checkout_created", TRUE); 142 update_user_meta($user_id, "billflow_created", TRUE); 143 144 // Send email to the new user 129 145 wp_new_user_notification( $user_id, null, 'both'); 146 147 // Login the new user 148 wp_clear_auth_cookie(); 149 wp_set_current_user ( $user_id ); // Set the current user detail 150 wp_set_auth_cookie ( $user_id ); // Set auth details in cookie 151 130 152 wp_send_json( array( 'user_id' => $user_id, 131 153 'email' => $email, 132 154 'name' => $name, 133 155 'password' => '*****', 134 'message' => 'User created successfully.' 156 'message' => 'User created successfully.', 157 'refresh' => true 135 158 ), 200 ); 136 159 }else{ 137 wp_send_json_error( array( 'email' => $email, 138 'name' => $name, 139 'password' => '*****', 140 'error' => 'Unable to create user.', 141 ), 500 ); 160 161 // if user already exists 162 $existing_user = get_user_by('email', $email); 163 $user_id = $existing_user->get('id'); 164 // if the user hasn't been updated with a password 165 if(get_user_meta( $user_id, "billflow_stripe_webhook_created", TRUE )){ 166 // and if the user has the matching subscription 167 if(get_user_meta( $user_id, "billflow_stripe_subscription_id", TRUE) === $subscription_id){ 168 // remove the meta that indicates need password update 169 delete_user_meta($user->get('id'), "billflow_stripe_webhook_created"); 170 // update the password 171 wp_set_password($password, $user_id); 172 // Login the new user 173 wp_clear_auth_cookie(); 174 wp_set_current_user ( $user_id ); // Set the current user detail 175 wp_set_auth_cookie ( $user_id ); // Set auth details in cookie 176 177 wp_send_json( array( 'email' => $email, 178 'user_already_exists' => true, 179 'password_updated' => true, 180 ), 200 ); 181 }else{ 182 // the case if some one else put an existing email in. 183 wp_send_json_error( array( 'email' => $email, 184 'user_already_exists' => true, 185 'checkout_sid' => $subscription_id, 186 // 'existing_sid' => get_user_meta( $user_id, "billflow_stripe_subscription_id", TRUE), 187 'error' => 'User is created by webhook, but subscription does not match.', 188 ), 591 ); 189 } 190 }else{ 191 wp_send_json_error( array( 'email' => $email, 192 'error' => 'Unable to create user.', 193 ), 592 ); 194 } 142 195 } 143 196 … … 173 226 } 174 227 175 function servicebot_create_wp_user($customer, $product_sb_tier = NULL){ 228 /** 229 * webhook create user handler, called by the Stripe webhook handler 230 * 231 * @param [object]$customer Stripe customer object 232 * @param [string]$product_sb_tier sb_tier meta data of the subscription 233 * @param [string]$subscription_id Stripe subscription id 234 * 235 * @return [WP_User] returns the WP user object if created 236 */ 237 function servicebot_create_wp_user($customer, $product_sb_tier = NULL, $subscription_id = NULL){ 176 238 $email = sanitize_email( $customer->email ); 239 240 // create wp user with the email and role should be assigned to whatever system settings's default is 177 241 $userdata = array( 178 'user_login' => $email, 179 'user_email' => $email, 180 'role' => "" 242 'user_login' => $email, 243 'user_email' => $email 181 244 ); 182 245 $user_id = wp_insert_user( $userdata ); … … 184 247 if ( ! is_wp_error( $user_id ) ) { 185 248 249 // add user meta to note that this is created by Stripe webhook 250 update_user_meta($user_id, "billflow_stripe_webhook_created", TRUE); 251 update_user_meta($user_id, "billflow_created", TRUE); 252 if(isset($subscription_id)){ 253 update_user_meta($user_id, "billflow_stripe_subscription_id", $subscription_id); 254 } 255 256 // update user role according to the billfow plugin roles settings 186 257 $new_user = get_user_by('id', $user_id); 187 258 if($product_sb_tier){ … … 189 260 } 190 261 262 // send email notification to new user 191 263 wp_new_user_notification( $user_id, null, 'both'); 264 192 265 wp_send_json( array( 193 266 'user_id' => $user_id, … … 198 271 ), 200 ); 199 272 return $new_user; 273 200 274 }else{ 201 275 276 // if user already exists 202 277 $user = get_user_by('email', $email); 203 278 if($user){ 279 // and the subscription has product metadata sb_tier 204 280 if($product_sb_tier){ 281 // then try updating the user role according to the billflow roles settings 205 282 updateUserRole($user->get('id'), $product_sb_tier); 206 283 wp_send_json( array( … … 210 287 }else{ 211 288 wp_send_json( array( 212 'info' => 'User already exists, no action', 289 'ok', true, 290 'info' => 'User already exists, no action performed.', 213 291 'user' => get_user_by('email', $email) 214 292 ), 200 ); … … 457 535 throw(new Exception('No customer retrieved')); 458 536 } 459 servicebot_create_wp_user($customer, $product_sb_tier );537 servicebot_create_wp_user($customer, $product_sb_tier, $subscription['id']); 460 538 break; 461 539 } catch (Exception $e) { -
servicebot/tags/2.0.5/public/widgets/class-servicebot-billing-page-widget.php
r2457745 r2458620 125 125 $subscription_id = isset( $instance['subscription_id'] ) ? apply_filters( 'widget_subscription_id', $instance['subscription_id'] ) : ''; 126 126 $create_user = isset( $instance['create_user'] ) ? apply_filters( 'create_user', $instance['create_user'] ) : (!!$this->global_values['create_user']); 127 $sb_login_redirect_url = isset( $instance['sb_login_redirect_url'] ) ? apply_filters( 'sb_login_redirect_url', $instance['sb_login_redirect_url'] ) : $this->global_values['login_redirect_url'];128 127 129 128 // Get Wordpress data 130 129 $logged_in_user = wp_get_current_user(); 131 130 $logged_in_email = $logged_in_user->user_email; 132 $login_url = wp_login_url($sb_login_redirect_url);133 131 $admin_ajax_url = admin_url("admin-ajax.php"); 134 132 … … 198 196 'is_logged_in' => $logged_in_email ? true : false, 199 197 'logged_in_email' => $logged_in_email, 200 'login_redirect_url' => $login_url,201 198 'admin_ajax_url' => $admin_ajax_url, 202 199 'widget' => 'billflow-billing-page-widget', -
servicebot/tags/2.0.5/public/widgets/js/billflow-widget.js
r2457745 r2458620 112 112 email: response.customer.email, 113 113 name: username, 114 subscription_id: response && response.id, 114 115 password: password 115 116 }; 116 117 117 const createSubscriptionCallback =function(data) {118 jQuery.post(ajax_url, payload, function(data) { 118 119 console.debug("create_subscription create_user callback", data) 119 120 … … 124 125 servicebot_wp_handle_response_create_subscription({event, response, extras}); 125 126 } 127 /** 128 * new handle response hook for 2021 129 */ 126 130 if(billflow_wp_handle_response_create_subscription){ 127 131 billflow_wp_handle_response_create_subscription({event, response, extras}); … … 129 133 130 134 if(login_redirect_url){ 135 console.debug("redirect url set", login_redirect_url); 131 136 window.location = login_redirect_url; 132 } 133 }; 134 135 jQuery.post(ajax_url, payload, createSubscriptionCallback); 137 }else if(data.refresh){ 138 window.location.reload(); 139 } 140 141 }).done(function(){ 142 // console.log("Billflow WP account creation successful") 143 }).fail(function(){ 144 // alert('checkout create usr failed'); 145 console.error("Billflow WP account creation encountered an error"); 146 }) 136 147 } 137 148 -
servicebot/tags/2.0.5/public/widgets/js/servicebot-handle-response.js
r2414033 r2458620 1 1 console.log('loaded servicebot-handle-response.js') 2 2 3 var servicebot_wp_handle_response_create_subscription; 3 4 var servicebot_wp_handle_response; 5 6 var billflow_wp_handle_response_create_subscription; 7 var billflow_wp_handle_response; 8 4 9 (function( $ ) { 5 10 'use strict'; -
servicebot/tags/2.0.5/servicebot.php
r2457926 r2458620 17 17 * Plugin URI: http://www.wpexplorer.com/servicebot/ 18 18 * Description: Provides NO-CODE integration between WordPress and Billflow, an UI layer on top of Stripe Billing. 19 * Version: 2.0. 419 * Version: 2.0.5 20 20 * Author: Billflow 21 21 * Author URI: https://billflow.io … … 36 36 * Rename this for your plugin and update it as you release new versions. 37 37 */ 38 define( 'SERVICEBOT_VERSION', '2.0. 4' );38 define( 'SERVICEBOT_VERSION', '2.0.5' ); 39 39 40 40 /** -
servicebot/trunk/public/class-servicebot-public.php
r2457925 r2458620 7 7 use Stripe\Event; 8 8 use Stripe\Webhook; 9 10 /** 11 * User meta data created by billflow 12 * billflow_created 13 * billflow_checkout_created 14 * billflow_stripe_webhook_created 15 * billflow_stripe_subscription_id 16 * 17 * User created default role to whatever the system default is. 18 */ 9 19 10 20 /** … … 114 124 $email = sanitize_email( $_POST['email'] ); 115 125 $name = sanitize_user( $_POST['name'] ); 116 $password = $_POST['password']; 126 $password = $_POST['password']; 127 $subscription_id = $_POST['subscription_id']; 117 128 118 129 $userdata = array( … … 120 131 'user_email' => $email, 121 132 'user_pass' => $password, 122 'role' => "subscriber"123 133 ); 124 134 125 135 $user_id = wp_insert_user( $userdata ); 126 136 127 137 //On success 128 138 if ( ! is_wp_error( $user_id ) ) { 139 140 // add user meta to note that this is created by Stripe webhook 141 update_user_meta($user_id, "billflow_checkout_created", TRUE); 142 update_user_meta($user_id, "billflow_created", TRUE); 143 144 // Send email to the new user 129 145 wp_new_user_notification( $user_id, null, 'both'); 146 147 // Login the new user 148 wp_clear_auth_cookie(); 149 wp_set_current_user ( $user_id ); // Set the current user detail 150 wp_set_auth_cookie ( $user_id ); // Set auth details in cookie 151 130 152 wp_send_json( array( 'user_id' => $user_id, 131 153 'email' => $email, 132 154 'name' => $name, 133 155 'password' => '*****', 134 'message' => 'User created successfully.' 156 'message' => 'User created successfully.', 157 'refresh' => true 135 158 ), 200 ); 136 159 }else{ 137 wp_send_json_error( array( 'email' => $email, 138 'name' => $name, 139 'password' => '*****', 140 'error' => 'Unable to create user.', 141 ), 500 ); 160 161 // if user already exists 162 $existing_user = get_user_by('email', $email); 163 $user_id = $existing_user->get('id'); 164 // if the user hasn't been updated with a password 165 if(get_user_meta( $user_id, "billflow_stripe_webhook_created", TRUE )){ 166 // and if the user has the matching subscription 167 if(get_user_meta( $user_id, "billflow_stripe_subscription_id", TRUE) === $subscription_id){ 168 // remove the meta that indicates need password update 169 delete_user_meta($user->get('id'), "billflow_stripe_webhook_created"); 170 // update the password 171 wp_set_password($password, $user_id); 172 // Login the new user 173 wp_clear_auth_cookie(); 174 wp_set_current_user ( $user_id ); // Set the current user detail 175 wp_set_auth_cookie ( $user_id ); // Set auth details in cookie 176 177 wp_send_json( array( 'email' => $email, 178 'user_already_exists' => true, 179 'password_updated' => true, 180 ), 200 ); 181 }else{ 182 // the case if some one else put an existing email in. 183 wp_send_json_error( array( 'email' => $email, 184 'user_already_exists' => true, 185 'checkout_sid' => $subscription_id, 186 // 'existing_sid' => get_user_meta( $user_id, "billflow_stripe_subscription_id", TRUE), 187 'error' => 'User is created by webhook, but subscription does not match.', 188 ), 591 ); 189 } 190 }else{ 191 wp_send_json_error( array( 'email' => $email, 192 'error' => 'Unable to create user.', 193 ), 592 ); 194 } 142 195 } 143 196 … … 173 226 } 174 227 175 function servicebot_create_wp_user($customer, $product_sb_tier = NULL){ 228 /** 229 * webhook create user handler, called by the Stripe webhook handler 230 * 231 * @param [object]$customer Stripe customer object 232 * @param [string]$product_sb_tier sb_tier meta data of the subscription 233 * @param [string]$subscription_id Stripe subscription id 234 * 235 * @return [WP_User] returns the WP user object if created 236 */ 237 function servicebot_create_wp_user($customer, $product_sb_tier = NULL, $subscription_id = NULL){ 176 238 $email = sanitize_email( $customer->email ); 239 240 // create wp user with the email and role should be assigned to whatever system settings's default is 177 241 $userdata = array( 178 'user_login' => $email, 179 'user_email' => $email, 180 'role' => "" 242 'user_login' => $email, 243 'user_email' => $email 181 244 ); 182 245 $user_id = wp_insert_user( $userdata ); … … 184 247 if ( ! is_wp_error( $user_id ) ) { 185 248 249 // add user meta to note that this is created by Stripe webhook 250 update_user_meta($user_id, "billflow_stripe_webhook_created", TRUE); 251 update_user_meta($user_id, "billflow_created", TRUE); 252 if(isset($subscription_id)){ 253 update_user_meta($user_id, "billflow_stripe_subscription_id", $subscription_id); 254 } 255 256 // update user role according to the billfow plugin roles settings 186 257 $new_user = get_user_by('id', $user_id); 187 258 if($product_sb_tier){ … … 189 260 } 190 261 262 // send email notification to new user 191 263 wp_new_user_notification( $user_id, null, 'both'); 264 192 265 wp_send_json( array( 193 266 'user_id' => $user_id, … … 198 271 ), 200 ); 199 272 return $new_user; 273 200 274 }else{ 201 275 276 // if user already exists 202 277 $user = get_user_by('email', $email); 203 278 if($user){ 279 // and the subscription has product metadata sb_tier 204 280 if($product_sb_tier){ 281 // then try updating the user role according to the billflow roles settings 205 282 updateUserRole($user->get('id'), $product_sb_tier); 206 283 wp_send_json( array( … … 210 287 }else{ 211 288 wp_send_json( array( 212 'info' => 'User already exists, no action', 289 'ok', true, 290 'info' => 'User already exists, no action performed.', 213 291 'user' => get_user_by('email', $email) 214 292 ), 200 ); … … 457 535 throw(new Exception('No customer retrieved')); 458 536 } 459 servicebot_create_wp_user($customer, $product_sb_tier );537 servicebot_create_wp_user($customer, $product_sb_tier, $subscription['id']); 460 538 break; 461 539 } catch (Exception $e) { -
servicebot/trunk/public/widgets/class-servicebot-billing-page-widget.php
r2457745 r2458620 125 125 $subscription_id = isset( $instance['subscription_id'] ) ? apply_filters( 'widget_subscription_id', $instance['subscription_id'] ) : ''; 126 126 $create_user = isset( $instance['create_user'] ) ? apply_filters( 'create_user', $instance['create_user'] ) : (!!$this->global_values['create_user']); 127 $sb_login_redirect_url = isset( $instance['sb_login_redirect_url'] ) ? apply_filters( 'sb_login_redirect_url', $instance['sb_login_redirect_url'] ) : $this->global_values['login_redirect_url'];128 127 129 128 // Get Wordpress data 130 129 $logged_in_user = wp_get_current_user(); 131 130 $logged_in_email = $logged_in_user->user_email; 132 $login_url = wp_login_url($sb_login_redirect_url);133 131 $admin_ajax_url = admin_url("admin-ajax.php"); 134 132 … … 198 196 'is_logged_in' => $logged_in_email ? true : false, 199 197 'logged_in_email' => $logged_in_email, 200 'login_redirect_url' => $login_url,201 198 'admin_ajax_url' => $admin_ajax_url, 202 199 'widget' => 'billflow-billing-page-widget', -
servicebot/trunk/public/widgets/js/billflow-widget.js
r2457745 r2458620 112 112 email: response.customer.email, 113 113 name: username, 114 subscription_id: response && response.id, 114 115 password: password 115 116 }; 116 117 117 const createSubscriptionCallback =function(data) {118 jQuery.post(ajax_url, payload, function(data) { 118 119 console.debug("create_subscription create_user callback", data) 119 120 … … 124 125 servicebot_wp_handle_response_create_subscription({event, response, extras}); 125 126 } 127 /** 128 * new handle response hook for 2021 129 */ 126 130 if(billflow_wp_handle_response_create_subscription){ 127 131 billflow_wp_handle_response_create_subscription({event, response, extras}); … … 129 133 130 134 if(login_redirect_url){ 135 console.debug("redirect url set", login_redirect_url); 131 136 window.location = login_redirect_url; 132 } 133 }; 134 135 jQuery.post(ajax_url, payload, createSubscriptionCallback); 137 }else if(data.refresh){ 138 window.location.reload(); 139 } 140 141 }).done(function(){ 142 // console.log("Billflow WP account creation successful") 143 }).fail(function(){ 144 // alert('checkout create usr failed'); 145 console.error("Billflow WP account creation encountered an error"); 146 }) 136 147 } 137 148 -
servicebot/trunk/public/widgets/js/servicebot-handle-response.js
r2414033 r2458620 1 1 console.log('loaded servicebot-handle-response.js') 2 2 3 var servicebot_wp_handle_response_create_subscription; 3 4 var servicebot_wp_handle_response; 5 6 var billflow_wp_handle_response_create_subscription; 7 var billflow_wp_handle_response; 8 4 9 (function( $ ) { 5 10 'use strict'; -
servicebot/trunk/servicebot.php
r2457926 r2458620 17 17 * Plugin URI: http://www.wpexplorer.com/servicebot/ 18 18 * Description: Provides NO-CODE integration between WordPress and Billflow, an UI layer on top of Stripe Billing. 19 * Version: 2.0. 419 * Version: 2.0.5 20 20 * Author: Billflow 21 21 * Author URI: https://billflow.io … … 36 36 * Rename this for your plugin and update it as you release new versions. 37 37 */ 38 define( 'SERVICEBOT_VERSION', '2.0. 4' );38 define( 'SERVICEBOT_VERSION', '2.0.5' ); 39 39 40 40 /**
Note: See TracChangeset
for help on using the changeset viewer.