Skip to content

Commit

Permalink
Merge pull request #8 from lsydev/2.0.5
Browse files Browse the repository at this point in the history
2.0.5
  • Loading branch information
lsydev authored Jan 19, 2021
2 parents da7ce2c + 86e3e3c commit 834f92c
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 23 deletions.
Binary file modified .DS_Store
Binary file not shown.
108 changes: 93 additions & 15 deletions public/class-servicebot-public.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
use Stripe\Event;
use Stripe\Webhook;

/**
* User meta data created by billflow
* billflow_created
* billflow_checkout_created
* billflow_stripe_webhook_created
* billflow_stripe_subscription_id
*
* User created default role to whatever the system default is.
*/

/**
* The public-facing functionality of the plugin.
*
Expand Down Expand Up @@ -113,32 +123,75 @@ function servicebot_ajax_create_user() {

$email = sanitize_email( $_POST['email'] );
$name = sanitize_user( $_POST['name'] );
$password = $_POST['password'];
$password = $_POST['password'];
$subscription_id = $_POST['subscription_id'];

$userdata = array(
'user_login' => $name,
'user_email' => $email,
'user_pass' => $password,
'role' => "subscriber"
);

$user_id = wp_insert_user( $userdata );

//On success
if ( ! is_wp_error( $user_id ) ) {

// add user meta to note that this is created by Stripe webhook
update_user_meta($user_id, "billflow_checkout_created", TRUE);
update_user_meta($user_id, "billflow_created", TRUE);

// Send email to the new user
wp_new_user_notification( $user_id, null, 'both');

// Login the new user
wp_clear_auth_cookie();
wp_set_current_user ( $user_id ); // Set the current user detail
wp_set_auth_cookie ( $user_id ); // Set auth details in cookie

wp_send_json( array( 'user_id' => $user_id,
'email' => $email,
'name' => $name,
'password' => '*****',
'message' => 'User created successfully.'
'message' => 'User created successfully.',
'refresh' => true
), 200 );
}else{
wp_send_json_error( array( 'email' => $email,
'name' => $name,
'password' => '*****',
'error' => 'Unable to create user.',
), 500 );

// if user already exists
$existing_user = get_user_by('email', $email);
$user_id = $existing_user->get('id');
// if the user hasn't been updated with a password
if(get_user_meta( $user_id, "billflow_stripe_webhook_created", TRUE )){
// and if the user has the matching subscription
if(get_user_meta( $user_id, "billflow_stripe_subscription_id", TRUE) === $subscription_id){
// remove the meta that indicates need password update
delete_user_meta($user->get('id'), "billflow_stripe_webhook_created");
// update the password
wp_set_password($password, $user_id);
// Login the new user
wp_clear_auth_cookie();
wp_set_current_user ( $user_id ); // Set the current user detail
wp_set_auth_cookie ( $user_id ); // Set auth details in cookie

wp_send_json( array( 'email' => $email,
'user_already_exists' => true,
'password_updated' => true,
), 200 );
}else{
// the case if some one else put an existing email in.
wp_send_json_error( array( 'email' => $email,
'user_already_exists' => true,
'checkout_sid' => $subscription_id,
// 'existing_sid' => get_user_meta( $user_id, "billflow_stripe_subscription_id", TRUE),
'error' => 'User is created by webhook, but subscription does not match.',
), 591 );
}
}else{
wp_send_json_error( array( 'email' => $email,
'error' => 'Unable to create user.',
), 592 );
}
}

}
Expand Down Expand Up @@ -172,23 +225,43 @@ function updateUserRole($user_id, $product_sb_tier){
}
}

function servicebot_create_wp_user($customer, $product_sb_tier = NULL){
/**
* webhook create user handler, called by the Stripe webhook handler
*
* @param [object]$customer Stripe customer object
* @param [string]$product_sb_tier sb_tier meta data of the subscription
* @param [string]$subscription_id Stripe subscription id
*
* @return [WP_User] returns the WP user object if created
*/
function servicebot_create_wp_user($customer, $product_sb_tier = NULL, $subscription_id = NULL){
$email = sanitize_email( $customer->email );

// create wp user with the email and role should be assigned to whatever system settings's default is
$userdata = array(
'user_login' => $email,
'user_email' => $email,
'role' => ""
'user_login' => $email,
'user_email' => $email
);
$user_id = wp_insert_user( $userdata );

if ( ! is_wp_error( $user_id ) ) {

// add user meta to note that this is created by Stripe webhook
update_user_meta($user_id, "billflow_stripe_webhook_created", TRUE);
update_user_meta($user_id, "billflow_created", TRUE);
if(isset($subscription_id)){
update_user_meta($user_id, "billflow_stripe_subscription_id", $subscription_id);
}

// update user role according to the billfow plugin roles settings
$new_user = get_user_by('id', $user_id);
if($product_sb_tier){
updateUserRole($user_id, $product_sb_tier);
}

// send email notification to new user
wp_new_user_notification( $user_id, null, 'both');

wp_send_json( array(
'user_id' => $user_id,
'email' => $email,
Expand All @@ -197,19 +270,24 @@ function servicebot_create_wp_user($customer, $product_sb_tier = NULL){
'message' => 'User created successfully.'
), 200 );
return $new_user;

}else{

// if user already exists
$user = get_user_by('email', $email);
if($user){
// and the subscription has product metadata sb_tier
if($product_sb_tier){
// then try updating the user role according to the billflow roles settings
updateUserRole($user->get('id'), $product_sb_tier);
wp_send_json( array(
'info' => 'User already exists, updated user role if changed',
'user' => get_user_by('email', $email)
), 200 );
}else{
wp_send_json( array(
'info' => 'User already exists, no action',
'ok', true,
'info' => 'User already exists, no action performed.',
'user' => get_user_by('email', $email)
), 200 );
}
Expand Down Expand Up @@ -456,7 +534,7 @@ function servicebot_webhook_listener() {
if(!$customer['email']){
throw(new Exception('No customer retrieved'));
}
servicebot_create_wp_user($customer, $product_sb_tier);
servicebot_create_wp_user($customer, $product_sb_tier, $subscription['id']);
break;
} catch (Exception $e) {
$attempts++;
Expand Down
3 changes: 0 additions & 3 deletions public/widgets/class-servicebot-billing-page-widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,10 @@ public function widget( $args, $instance ) {
$customer_id = isset( $instance['customer_id'] ) ? apply_filters( 'widget_customer_id', $instance['customer_id'] ) : '';
$subscription_id = isset( $instance['subscription_id'] ) ? apply_filters( 'widget_subscription_id', $instance['subscription_id'] ) : '';
$create_user = isset( $instance['create_user'] ) ? apply_filters( 'create_user', $instance['create_user'] ) : (!!$this->global_values['create_user']);
$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'];

// Get Wordpress data
$logged_in_user = wp_get_current_user();
$logged_in_email = $logged_in_user->user_email;
$login_url = wp_login_url($sb_login_redirect_url);
$admin_ajax_url = admin_url("admin-ajax.php");

if($sb_secret && $logged_in_email){
Expand Down Expand Up @@ -197,7 +195,6 @@ public function widget( $args, $instance ) {
'create_user' => $create_user ? true : false,
'is_logged_in' => $logged_in_email ? true : false,
'logged_in_email' => $logged_in_email,
'login_redirect_url' => $login_url,
'admin_ajax_url' => $admin_ajax_url,
'widget' => 'billflow-billing-page-widget',
'embed_type' => 'billing_page',
Expand Down
17 changes: 14 additions & 3 deletions public/widgets/js/billflow-widget.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,11 @@
action: "create_user",
email: response.customer.email,
name: username,
subscription_id: response && response.id,
password: password
};

const createSubscriptionCallback = function(data) {
jQuery.post(ajax_url, payload, function(data) {
console.debug("create_subscription create_user callback", data)

/**
Expand All @@ -123,16 +124,26 @@
if(servicebot_wp_handle_response_create_subscription){
servicebot_wp_handle_response_create_subscription({event, response, extras});
}
/**
* new handle response hook for 2021
*/
if(billflow_wp_handle_response_create_subscription){
billflow_wp_handle_response_create_subscription({event, response, extras});
}

if(login_redirect_url){
console.debug("redirect url set", login_redirect_url);
window.location = login_redirect_url;
}else if(data.refresh){
window.location.reload();
}
};

jQuery.post(ajax_url, payload, createSubscriptionCallback);
}).done(function(){
// console.log("Billflow WP account creation successful")
}).fail(function(){
// alert('checkout create usr failed');
console.error("Billflow WP account creation encountered an error");
})
}

}
Expand Down
5 changes: 5 additions & 0 deletions public/widgets/js/servicebot-handle-response.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
console.log('loaded servicebot-handle-response.js')

var servicebot_wp_handle_response_create_subscription;
var servicebot_wp_handle_response;

var billflow_wp_handle_response_create_subscription;
var billflow_wp_handle_response;

(function( $ ) {
'use strict';

Expand Down
4 changes: 2 additions & 2 deletions servicebot.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* Plugin Name: Billflow
* Plugin URI: http://www.wpexplorer.com/servicebot/
* Description: Provides NO-CODE integration between WordPress and Billflow, an UI layer on top of Stripe Billing.
* Version: 2.0.4
* Version: 2.0.5
* Author: Billflow
* Author URI: https://billflow.io
* License: GPL-2.0+
Expand All @@ -35,7 +35,7 @@
* Start at version 1.0.0 and use SemVer - https://semver.org
* Rename this for your plugin and update it as you release new versions.
*/
define( 'SERVICEBOT_VERSION', '2.0.4' );
define( 'SERVICEBOT_VERSION', '2.0.5' );

/**
* The code that runs during plugin activation.
Expand Down

0 comments on commit 834f92c

Please sign in to comment.