Plugin Directory

Changeset 2228151


Ignore:
Timestamp:
01/16/2020 01:16:15 AM (6 years ago)
Author:
servicebot
Message:

adds Stripe webhook handler capabilities for creating wp user on create of stripe customer

Location:
servicebot/trunk
Files:
161 added
5 edited

Legend:

Unmodified
Added
Removed
  • servicebot/trunk/README.txt

    r2196047 r2228151  
    2323 * Customers can download Stripe invoices
    2424 * Subscription management portal can be used with Stripe Checkout
     25 * Automatically create your Stripe customers as WordPress users with Stripe Webhooks
    2526
    2627== Installation ==
  • servicebot/trunk/admin/class-servicebot-admin.php

    r2196039 r2228151  
    6363       
    6464        //add_submenu_page( '$parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function );
    65         // add_submenu_page( $this->plugin_name, 'Plugin Name Settings', 'Settings', 'administrator', $this->plugin_name.'-settings', array( $this, 'displayPluginAdminSettings' ));
     65        add_submenu_page( $this->plugin_name, 'Servicebot Stripe Webhooks', 'Stripe Webhooks', 'administrator', $this->plugin_name.'-stripe-webhooks', array( $this, 'displayPluginAdminStripeWebhooks' ));
    6666    }
    6767
     
    7474        }
    7575        require_once 'partials/'.$this->plugin_name.'-admin-display.php';
     76    }
     77
     78    public function displayPluginAdminStripeWebhooks() {
     79        // set this var to be used in the settings-display view
     80        $active_tab = isset( $_GET[ 'tab' ] ) ? $_GET[ 'tab' ] : 'general';
     81        if(isset($_GET['error_message'])){
     82            add_action('admin_notices', array($this,'servicebotSettingsMessages'));
     83            do_action( 'admin_notices', $_GET['error_message'] );
     84        }
     85        require_once 'partials/'.$this->plugin_name.'-admin-stripe-webhooks.php';
    7686    }
    7787
     
    103113            'servicebot_general_section',
    104114            // Title to be displayed on the administration page
    105             'Servicebot Embeddables Global Settings', 
     115            'Embeddable Global Settings', 
    106116            // Callback used to render the description of the section
    107117            array( $this, 'servicebot_display_general_account' ),   
     
    294304        );
    295305
     306        // Stripe webhooks section
     307        add_settings_section(
     308            // ID used to identify this section and with which to register options
     309            'servicebot_stripe_webhooks_section',
     310            // Title to be displayed on the administration page
     311            '', 
     312            // Callback used to render the description of the section
     313            array( $this, 'servicebot_display_stripe_webhooks' ),   
     314            // Page on which to add this section of options
     315            'servicebot_stripe_webhooks_settings'                   
     316        );
     317
     318        unset($args);
     319        $args = array (
     320            'type'      => 'input',
     321            'subtype'   => 'text',
     322            'id'        => 'servicebot_servicebot_stripe_test_secret_key_setting',
     323            'name'      => 'servicebot_servicebot_stripe_test_secret_key_setting',
     324            'required'  => 'false',
     325            'get_options_list' => '',
     326            'value_type' => 'normal',
     327            'wp_data'   => 'option'
     328        );
     329        add_settings_field(
     330            'servicebot_servicebot_stripe_test_secret_key_setting',
     331            'Stripe Test Secret Key',
     332            array( $this, 'servicebot_render_settings_field' ),
     333            'servicebot_stripe_webhooks_settings',
     334            'servicebot_stripe_webhooks_section',
     335            $args
     336        );
     337        register_setting(
     338            'servicebot_stripe_webhooks_settings',
     339            'servicebot_servicebot_stripe_test_secret_key_setting'
     340        );
     341
     342        unset($args);
     343        $args = array (
     344            'type'      => 'input',
     345            'subtype'   => 'text',
     346            'id'        => 'servicebot_servicebot_stripe_live_secret_key_setting',
     347            'name'      => 'servicebot_servicebot_stripe_live_secret_key_setting',
     348            'required'  => 'false',
     349            'get_options_list' => '',
     350            'value_type' => 'normal',
     351            'wp_data'   => 'option'
     352        );
     353        add_settings_field(
     354            'servicebot_servicebot_stripe_live_secret_key_setting',
     355            'Stripe Live Secret Key',
     356            array( $this, 'servicebot_render_settings_field' ),
     357            'servicebot_stripe_webhooks_settings',
     358            'servicebot_stripe_webhooks_section',
     359            $args
     360        );
     361        register_setting(
     362            'servicebot_stripe_webhooks_settings',
     363            'servicebot_servicebot_stripe_live_secret_key_setting'
     364        );
     365
     366        unset($args);
     367        $args = array (
     368            'type'      => 'input',
     369            'subtype'   => 'text',
     370            'id'        => 'servicebot_servicebot_stripe_test_signing_secret_setting',
     371            'name'      => 'servicebot_servicebot_stripe_test_signing_secret_setting',
     372            'required'  => 'false',
     373            'get_options_list' => '',
     374            'value_type' => 'normal',
     375            'wp_data'   => 'option'
     376        );
     377        add_settings_field(
     378            'servicebot_servicebot_stripe_test_signing_secret_setting',
     379            'Stripe Test Signing Secret',
     380            array( $this, 'servicebot_render_settings_field' ),
     381            'servicebot_stripe_webhooks_settings',
     382            'servicebot_stripe_webhooks_section',
     383            $args
     384        );
     385        register_setting(
     386            'servicebot_stripe_webhooks_settings',
     387            'servicebot_servicebot_stripe_test_signing_secret_setting'
     388        );
     389
     390        unset($args);
     391        $args = array (
     392            'type'      => 'input',
     393            'subtype'   => 'text',
     394            'id'        => 'servicebot_servicebot_stripe_live_signing_secret_setting',
     395            'name'      => 'servicebot_servicebot_stripe_live_signing_secret_setting',
     396            'required'  => 'false',
     397            'get_options_list' => '',
     398            'value_type' => 'normal',
     399            'wp_data'   => 'option'
     400        );
     401        add_settings_field(
     402            'servicebot_servicebot_stripe_live_signing_secret_setting',
     403            'Stripe Live Signing Secret',
     404            array( $this, 'servicebot_render_settings_field' ),
     405            'servicebot_stripe_webhooks_settings',
     406            'servicebot_stripe_webhooks_section',
     407            $args
     408        );
     409        register_setting(
     410            'servicebot_stripe_webhooks_settings',
     411            'servicebot_servicebot_stripe_live_signing_secret_setting'
     412        );
     413
    296414    }
    297415
    298416    public function servicebot_display_general_account() {
     417        echo '<p></p>';
     418    }
     419
     420    public function servicebot_display_stripe_webhooks() {
    299421        echo '<p></p>';
    300422    }
  • servicebot/trunk/admin/partials/servicebot-admin-display.php

    r2195968 r2228151  
    1616<!-- This file should primarily consist of HTML with a little bit of PHP. -->
    1717<div class="wrap">
    18                 <div id="icon-themes" class="icon32"></div> 
    19                 <h2>Servicebot Settings</h2> 
    20                  <!--NEED THE settings_errors below so that the errors/success messages are shown after submission - wasn't working once we started using add_menu_page and stopped using add_options_page so needed this-->
    21                 <?php settings_errors(); ?> 
    22                 <form method="POST" action="options.php"> 
    23                     <?php
    24                         settings_fields( 'servicebot_general_settings' );
    25                         do_settings_sections( 'servicebot_general_settings' );
    26                     ?>             
    27                     <?php submit_button(); ?> 
    28                 </form>
     18    <div id="icon-themes" class="icon32"></div> 
     19    <h2>Servicebot Settings</h2> 
     20        <!--NEED THE settings_errors below so that the errors/success messages are shown after submission - wasn't working once we started using add_menu_page and stopped using add_options_page so needed this-->
     21    <?php settings_errors(); ?> 
     22    <form method="POST" action="options.php"> 
     23        <section>
     24        <?php
     25            settings_fields( 'servicebot_general_settings' );
     26            do_settings_sections( 'servicebot_general_settings' );
     27        ?>
     28        </section>
     29        <?php submit_button(); ?> 
     30    </form>
    2931</div>
  • servicebot/trunk/public/class-servicebot-public.php

    r2196039 r2228151  
    11<?php
     2
     3require_once 'stripe/init.php';
     4use Stripe\Stripe;
     5use Stripe\Event;
     6use Stripe\Webhook;
    27
    38/**
     
    121126    //On success
    122127    if ( ! is_wp_error( $user_id ) ) {
     128        wp_new_user_notification( $user_id, null, 'both');
    123129        wp_send_json( array(    'user_id' => $user_id,
    124130                                'email' => $email,
     
    127133                                'message' => 'User created successfully.'
    128134                    ), 200 );
    129         wp_new_user_notification( $user_id, null, 'both');
    130135    }else{
    131136        wp_send_json_error( array(  'email' => $email,
     
    136141    }
    137142
    138   }
    139   add_action( 'wp_ajax_create_user', 'servicebot_ajax_create_user' );
    140   add_action( 'wp_ajax_nopriv_create_user', 'servicebot_ajax_create_user' );
     143}
     144add_action( 'wp_ajax_create_user', 'servicebot_ajax_create_user' );
     145add_action( 'wp_ajax_nopriv_create_user', 'servicebot_ajax_create_user' );
     146
     147
     148
     149function servicebot_webhook_listener() {
     150
     151    register_rest_route( 'servicebot/v1', '/stripe-hooks', array(
     152        'methods'  => 'POST'
     153      ) );
     154
     155    if ( $_SERVER['REQUEST_URI'] === '/servicebot/v1/stripe-hooks'){
     156
     157        $live_mode = get_option('servicebot_servicebot_live_mode_global_setting') == 1 ? true : false;
     158        if(!$live_mode){
     159            $stripe_sign_secret = get_option('servicebot_servicebot_stripe_test_signing_secret_setting');
     160            $stripe_secret_key = get_option('servicebot_servicebot_stripe_test_secret_key_setting');
     161        }else{
     162            $stripe_sign_secret = get_option('servicebot_servicebot_stripe_live_signing_secret_setting');
     163            $stripe_secret_key = get_option('servicebot_servicebot_stripe_live_secret_key_setting');
     164        }
     165
     166        Stripe::setApiKey($stripe_sign_secret);
     167        $endpoint_secret = $stripe_sign_secret;
     168       
     169        $payload = @file_get_contents( 'php://input' );
     170        $sig_header = $_SERVER['HTTP_STRIPE_SIGNATURE'];
     171        $event = null;
     172       
     173        try {
     174            $event = Webhook::constructEvent(
     175                $payload, $sig_header, $endpoint_secret
     176            );
     177        } catch(\UnexpectedValueException $e) {
     178            // Invalid payload
     179            http_response_code(481);
     180            exit();
     181        } catch(\Stripe\Exception\SignatureVerificationException $e) {
     182            // Invalid signature
     183            http_response_code(482);
     184            exit();
     185        }
     186
     187        // Handle the event
     188        switch ($event->type) {
     189            case 'customer.created':
     190                $customer = $event->data->object;
     191                $email = sanitize_email( $customer->email );
     192                $userdata = array(
     193                    'user_login'  =>  $email,
     194                    'user_email' => $email,
     195                    'role' => 'subscriber'
     196                );
     197                $user_id = wp_insert_user( $userdata );
     198
     199                if ( ! is_wp_error( $user_id ) ) {
     200                    wp_new_user_notification( $user_id, null, 'both');
     201                    wp_send_json( array(   
     202                            'user_id' => $user_id,
     203                            'email' => $email,
     204                            'password' => '*****',
     205                            'message' => 'User created successfully.'
     206                    ), 200 );
     207                }else{
     208                    wp_send_json_error( array( 
     209                            'email' => $email,
     210                            'name' => $name,
     211                            'password' => '*****',
     212                            'error' => 'Unable to create user.',
     213                    ), 500 );
     214                }
     215
     216                break;
     217            case 'payment_method.attached':
     218                $paymentMethod = $event->data->object; // contains a \Stripe\PaymentMethod
     219                // Then define and call a method to handle the successful attachment of a PaymentMethod.
     220                // handlePaymentMethodAttached($paymentMethod);
     221                break;
     222            // ... handle other event types
     223            default:
     224                // Unexpected event type
     225                http_response_code(489);
     226                exit();
     227        }
     228
     229        http_response_code(200);
     230    }
     231}
     232
     233add_action( 'init', 'servicebot_webhook_listener' );
  • servicebot/trunk/servicebot.php

    r2196039 r2228151  
    1717 * Plugin URI:        http://www.wpexplorer.com/servicebot/
    1818 * Description:       This is a short description of what the plugin does. It's displayed in the WordPress admin area.
    19  * Version:           1.0.0
     19 * Version:           1.1.0
    2020 * Author:            Servicebot
    2121 * Author URI:        https://servicebot.io
     
    3636 * Rename this for your plugin and update it as you release new versions.
    3737 */
    38 define( 'SERVICEBOT_VERSION', '1.0.0' );
     38define( 'SERVICEBOT_VERSION', '1.1.0' );
    3939
    4040/**
Note: See TracChangeset for help on using the changeset viewer.