Plugin Directory

Changeset 2458620


Ignore:
Timestamp:
01/19/2021 12:34:41 AM (5 years ago)
Author:
servicebot
Message:

Update to version 2.0.5 from GitHub

Location:
servicebot
Files:
10 edited
1 copied

Legend:

Unmodified
Added
Removed
  • servicebot/tags/2.0.5/public/class-servicebot-public.php

    r2457925 r2458620  
    77use Stripe\Event;
    88use 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     */
    919
    1020/**
     
    114124    $email = sanitize_email( $_POST['email'] );
    115125    $name = sanitize_user( $_POST['name'] );
    116     $password = $_POST['password'];
     126    $password = $_POST['password'];
     127    $subscription_id = $_POST['subscription_id'];
    117128   
    118129    $userdata = array(
     
    120131      'user_email' => $email,
    121132      'user_pass'   =>  $password,
    122       'role' => "subscriber"
    123133    );
    124134   
    125135    $user_id = wp_insert_user( $userdata );
    126    
     136
    127137    //On success
    128138    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
    129145        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
    130152        wp_send_json( array(    'user_id' => $user_id,
    131153                                'email' => $email,
    132154                                'name' => $name,
    133155                                'password' => '*****',
    134                                 'message' => 'User created successfully.'
     156                                'message' => 'User created successfully.',
     157                                'refresh' => true
    135158                    ), 200 );
    136159    }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        }
    142195    }
    143196
     
    173226}
    174227
    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 */
     237function servicebot_create_wp_user($customer, $product_sb_tier = NULL, $subscription_id = NULL){
    176238    $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
    177241    $userdata = array(
    178         'user_login'  =>  $email,
    179         'user_email' => $email,
    180         'role' => ""
     242        'user_login'  => $email,
     243        'user_email' => $email
    181244    );
    182245    $user_id = wp_insert_user( $userdata );
     
    184247    if ( ! is_wp_error( $user_id ) ) {
    185248
     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
    186257        $new_user = get_user_by('id', $user_id);
    187258        if($product_sb_tier){
     
    189260        }
    190261
     262        // send email notification to new user
    191263        wp_new_user_notification( $user_id, null, 'both');
     264
    192265        wp_send_json( array(   
    193266            'user_id' => $user_id,
     
    198271        ), 200 );
    199272        return $new_user;
     273
    200274    }else{
    201275
     276        // if user already exists
    202277        $user = get_user_by('email', $email);
    203278        if($user){
     279            // and the subscription has product metadata sb_tier
    204280            if($product_sb_tier){
     281                // then try updating the user role according to the billflow roles settings
    205282                updateUserRole($user->get('id'), $product_sb_tier);
    206283                wp_send_json( array( 
     
    210287            }else{
    211288                wp_send_json( array( 
    212                     'info' => 'User already exists, no action',
     289                    'ok', true,
     290                    'info' => 'User already exists, no action performed.',
    213291                    'user' => get_user_by('email', $email)
    214292                ), 200 );
     
    457535                                    throw(new Exception('No customer retrieved'));
    458536                                }
    459                                 servicebot_create_wp_user($customer, $product_sb_tier);
     537                                servicebot_create_wp_user($customer, $product_sb_tier, $subscription['id']);
    460538                                break;
    461539                            } catch (Exception $e) {
  • servicebot/tags/2.0.5/public/widgets/class-servicebot-billing-page-widget.php

    r2457745 r2458620  
    125125        $subscription_id = isset( $instance['subscription_id'] ) ? apply_filters( 'widget_subscription_id', $instance['subscription_id'] ) : '';
    126126        $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'];
    128127
    129128        // Get Wordpress data
    130129        $logged_in_user = wp_get_current_user();
    131130        $logged_in_email = $logged_in_user->user_email;
    132         $login_url = wp_login_url($sb_login_redirect_url);
    133131        $admin_ajax_url = admin_url("admin-ajax.php");
    134132
     
    198196            'is_logged_in'    => $logged_in_email ? true : false,
    199197            'logged_in_email' => $logged_in_email,
    200             'login_redirect_url' => $login_url,
    201198            'admin_ajax_url'  => $admin_ajax_url,
    202199            'widget'          => 'billflow-billing-page-widget',
  • servicebot/tags/2.0.5/public/widgets/js/billflow-widget.js

    r2457745 r2458620  
    112112                    email: response.customer.email,
    113113                    name: username,
     114                    subscription_id: response && response.id,
    114115                    password: password
    115116                };
    116117
    117                 const createSubscriptionCallback = function(data) {
     118                jQuery.post(ajax_url, payload, function(data) {
    118119                    console.debug("create_subscription create_user callback", data)
    119120
     
    124125                        servicebot_wp_handle_response_create_subscription({event, response, extras});
    125126                    }
     127                    /**
     128                     * new handle response hook for 2021
     129                     */
    126130                    if(billflow_wp_handle_response_create_subscription){
    127131                        billflow_wp_handle_response_create_subscription({event, response, extras});
     
    129133
    130134                    if(login_redirect_url){
     135                        console.debug("redirect url set", login_redirect_url);
    131136                        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                })
    136147            }
    137148
  • servicebot/tags/2.0.5/public/widgets/js/servicebot-handle-response.js

    r2414033 r2458620  
    11console.log('loaded servicebot-handle-response.js')
     2
    23var servicebot_wp_handle_response_create_subscription;
    34var servicebot_wp_handle_response;
     5
     6var billflow_wp_handle_response_create_subscription;
     7var billflow_wp_handle_response;
     8
    49(function( $ ) {
    510    'use strict';
  • servicebot/tags/2.0.5/servicebot.php

    r2457926 r2458620  
    1717 * Plugin URI:        http://www.wpexplorer.com/servicebot/
    1818 * Description:       Provides NO-CODE integration between WordPress and Billflow, an UI layer on top of Stripe Billing.
    19  * Version:           2.0.4
     19 * Version:           2.0.5
    2020 * Author:            Billflow
    2121 * Author URI:        https://billflow.io
     
    3636 * Rename this for your plugin and update it as you release new versions.
    3737 */
    38 define( 'SERVICEBOT_VERSION', '2.0.4' );
     38define( 'SERVICEBOT_VERSION', '2.0.5' );
    3939
    4040/**
  • servicebot/trunk/public/class-servicebot-public.php

    r2457925 r2458620  
    77use Stripe\Event;
    88use 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     */
    919
    1020/**
     
    114124    $email = sanitize_email( $_POST['email'] );
    115125    $name = sanitize_user( $_POST['name'] );
    116     $password = $_POST['password'];
     126    $password = $_POST['password'];
     127    $subscription_id = $_POST['subscription_id'];
    117128   
    118129    $userdata = array(
     
    120131      'user_email' => $email,
    121132      'user_pass'   =>  $password,
    122       'role' => "subscriber"
    123133    );
    124134   
    125135    $user_id = wp_insert_user( $userdata );
    126    
     136
    127137    //On success
    128138    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
    129145        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
    130152        wp_send_json( array(    'user_id' => $user_id,
    131153                                'email' => $email,
    132154                                'name' => $name,
    133155                                'password' => '*****',
    134                                 'message' => 'User created successfully.'
     156                                'message' => 'User created successfully.',
     157                                'refresh' => true
    135158                    ), 200 );
    136159    }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        }
    142195    }
    143196
     
    173226}
    174227
    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 */
     237function servicebot_create_wp_user($customer, $product_sb_tier = NULL, $subscription_id = NULL){
    176238    $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
    177241    $userdata = array(
    178         'user_login'  =>  $email,
    179         'user_email' => $email,
    180         'role' => ""
     242        'user_login'  => $email,
     243        'user_email' => $email
    181244    );
    182245    $user_id = wp_insert_user( $userdata );
     
    184247    if ( ! is_wp_error( $user_id ) ) {
    185248
     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
    186257        $new_user = get_user_by('id', $user_id);
    187258        if($product_sb_tier){
     
    189260        }
    190261
     262        // send email notification to new user
    191263        wp_new_user_notification( $user_id, null, 'both');
     264
    192265        wp_send_json( array(   
    193266            'user_id' => $user_id,
     
    198271        ), 200 );
    199272        return $new_user;
     273
    200274    }else{
    201275
     276        // if user already exists
    202277        $user = get_user_by('email', $email);
    203278        if($user){
     279            // and the subscription has product metadata sb_tier
    204280            if($product_sb_tier){
     281                // then try updating the user role according to the billflow roles settings
    205282                updateUserRole($user->get('id'), $product_sb_tier);
    206283                wp_send_json( array( 
     
    210287            }else{
    211288                wp_send_json( array( 
    212                     'info' => 'User already exists, no action',
     289                    'ok', true,
     290                    'info' => 'User already exists, no action performed.',
    213291                    'user' => get_user_by('email', $email)
    214292                ), 200 );
     
    457535                                    throw(new Exception('No customer retrieved'));
    458536                                }
    459                                 servicebot_create_wp_user($customer, $product_sb_tier);
     537                                servicebot_create_wp_user($customer, $product_sb_tier, $subscription['id']);
    460538                                break;
    461539                            } catch (Exception $e) {
  • servicebot/trunk/public/widgets/class-servicebot-billing-page-widget.php

    r2457745 r2458620  
    125125        $subscription_id = isset( $instance['subscription_id'] ) ? apply_filters( 'widget_subscription_id', $instance['subscription_id'] ) : '';
    126126        $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'];
    128127
    129128        // Get Wordpress data
    130129        $logged_in_user = wp_get_current_user();
    131130        $logged_in_email = $logged_in_user->user_email;
    132         $login_url = wp_login_url($sb_login_redirect_url);
    133131        $admin_ajax_url = admin_url("admin-ajax.php");
    134132
     
    198196            'is_logged_in'    => $logged_in_email ? true : false,
    199197            'logged_in_email' => $logged_in_email,
    200             'login_redirect_url' => $login_url,
    201198            'admin_ajax_url'  => $admin_ajax_url,
    202199            'widget'          => 'billflow-billing-page-widget',
  • servicebot/trunk/public/widgets/js/billflow-widget.js

    r2457745 r2458620  
    112112                    email: response.customer.email,
    113113                    name: username,
     114                    subscription_id: response && response.id,
    114115                    password: password
    115116                };
    116117
    117                 const createSubscriptionCallback = function(data) {
     118                jQuery.post(ajax_url, payload, function(data) {
    118119                    console.debug("create_subscription create_user callback", data)
    119120
     
    124125                        servicebot_wp_handle_response_create_subscription({event, response, extras});
    125126                    }
     127                    /**
     128                     * new handle response hook for 2021
     129                     */
    126130                    if(billflow_wp_handle_response_create_subscription){
    127131                        billflow_wp_handle_response_create_subscription({event, response, extras});
     
    129133
    130134                    if(login_redirect_url){
     135                        console.debug("redirect url set", login_redirect_url);
    131136                        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                })
    136147            }
    137148
  • servicebot/trunk/public/widgets/js/servicebot-handle-response.js

    r2414033 r2458620  
    11console.log('loaded servicebot-handle-response.js')
     2
    23var servicebot_wp_handle_response_create_subscription;
    34var servicebot_wp_handle_response;
     5
     6var billflow_wp_handle_response_create_subscription;
     7var billflow_wp_handle_response;
     8
    49(function( $ ) {
    510    'use strict';
  • servicebot/trunk/servicebot.php

    r2457926 r2458620  
    1717 * Plugin URI:        http://www.wpexplorer.com/servicebot/
    1818 * Description:       Provides NO-CODE integration between WordPress and Billflow, an UI layer on top of Stripe Billing.
    19  * Version:           2.0.4
     19 * Version:           2.0.5
    2020 * Author:            Billflow
    2121 * Author URI:        https://billflow.io
     
    3636 * Rename this for your plugin and update it as you release new versions.
    3737 */
    38 define( 'SERVICEBOT_VERSION', '2.0.4' );
     38define( 'SERVICEBOT_VERSION', '2.0.5' );
    3939
    4040/**
Note: See TracChangeset for help on using the changeset viewer.