Advertisement
verygoodplugins

Untitled

May 3rd, 2021
1,129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.28 KB | None | 0 0
  1. <?php
  2.  
  3. if ( ! defined( 'ABSPATH' ) ) {
  4.     exit; // Exit if accessed directly
  5. }
  6.  
  7. use SimplePay\Pro\Payments\Subscription;
  8.  
  9. /**
  10.  * WP Simple Pay integration.
  11.  *
  12.  * @since 3.30.4
  13.  */
  14.  
  15. class WPF_Simple_Pay extends WPF_Integrations_Base {
  16.  
  17.     /**
  18.      * Gets things started.
  19.      *
  20.      * @since 3.30.4
  21.      */
  22.     public function init() {
  23.  
  24.         $this->name = 'Simple Pay';
  25.         $this->slug = 'simple-pay';
  26.  
  27.         add_action( 'simpay_after_customer_created', array( $this, 'customer_created' ) );
  28.  
  29.         add_filter( 'simpay_form_settings_meta_tabs_li', array( $this, 'settings_tabs' ), 10, 2 );
  30.         add_action( 'simpay_form_settings_meta_options_panel', array( $this, 'settings_options_panel' ) );
  31.         add_action( 'simpay_save_form_settings', array( $this, 'save_settings' ), 10, 2 );
  32.  
  33.         add_action( 'simpay_webhook_charge_failed', array( $this, 'charge_failed' ), 10, 2 );
  34.         add_action( 'simpay_webhook_invoice_payment_succeeded', array( $this, 'payment_succeeded' ), 10, 2 );
  35.         add_action( 'simpay_webhook_event', array( $this, 'subscription_cancelled' ) );
  36.  
  37.     }
  38.  
  39.     /**
  40.      * Apply Payment Failed tags when a charge fails in Stripe.
  41.      *
  42.      * @since 3.37.13
  43.      *
  44.      * @param \Stripe\Event  $event  Stripe webhook event.
  45.      * @param \Stripe\Charge $charge Stripe Charge.
  46.      */
  47.     public function charge_failed( $event, $charge ) {
  48.  
  49.         $settings = get_post_meta( $charge->metadata->simpay_form_id, 'wpf_settings_simple_pay', true );
  50.  
  51.         if ( empty( $settings ) || false == $settings['enable'] || empty( $settings['apply_tags_payment_failed'] ) ) {
  52.             return;
  53.         }
  54.  
  55.         $user = get_user_by( 'email', $charge->customer->email );
  56.  
  57.         if ( $user ) {
  58.  
  59.             wp_fusion()->user->apply_tags( $settings['apply_tags_payment_failed'], $user->ID );
  60.  
  61.         } else {
  62.  
  63.             $contact_id = wp_fusion()->crm->get_contact_id( $charge->customer->email );
  64.  
  65.             if ( $contact_id ) {
  66.                 wp_fusion()->crm->apply_tags( $settings['apply_tags_payment_failed'], $contact_id );
  67.             }
  68.         }
  69.  
  70.     }
  71.  
  72.  
  73.     /**
  74.      * Remove Payment Failed tags when a payment succeeds/
  75.      *
  76.      * @since 3.37.13
  77.      *
  78.      * @param \Stripe\Event   $event   Stripe webhook event.
  79.      * @param \Stripe\Invoice $invoice Stripe Invoice.
  80.      */
  81.     public function payment_succeeded( $event, $invoice ) {
  82.  
  83.         $form_id = end( $invoice->lines->data )->metadata->simpay_form_id;
  84.  
  85.         $settings = get_post_meta( $form_id, 'wpf_settings_simple_pay', true );
  86.  
  87.         if ( empty( $settings ) || false == $settings['enable'] || empty( $settings['apply_tags_payment_failed'] ) ) {
  88.             return;
  89.         }
  90.  
  91.         $user = get_user_by( 'email', $invoice->customer_email );
  92.  
  93.         if ( $user ) {
  94.  
  95.             wp_fusion()->user->remove_tags( $settings['apply_tags_payment_failed'], $user->ID );
  96.  
  97.         } else {
  98.  
  99.             $contact_id = wp_fusion()->crm->get_contact_id( $invoice->customer_email );
  100.  
  101.             if ( $contact_id ) {
  102.                 wp_fusion()->crm->remove_tags( $settings['apply_tags_payment_failed'], $contact_id );
  103.             }
  104.         }
  105.  
  106.     }
  107.  
  108.  
  109.     /**
  110.      * Apply Subscription Cancelled tags when a subscription is cancelled in
  111.      * Stripe.
  112.      *
  113.      * @since 3.37.13
  114.      *
  115.      * @param \Stripe\Event $event  Stripe webhook event.
  116.      */
  117.     public function subscription_cancelled( $event ) {
  118.  
  119.         if ( 'customer.subscription.deleted' !== $event->type ) {
  120.             return;
  121.         }
  122.  
  123.         $subscription = $event->data->object;
  124.  
  125.         // Get the form ID
  126.  
  127.         if ( ! isset( $subscription->metadata->simpay_form_id ) ) {
  128.             return;
  129.         }
  130.  
  131.         $form_id = $subscription->metadata->simpay_form_id;
  132.  
  133.         // See if WP Fusion is enabled on the form
  134.  
  135.         $settings = get_post_meta( $form_id, 'wpf_settings_simple_pay', true );
  136.  
  137.         if ( empty( $settings ) || false == $settings['enable'] || empty( $settings['apply_tags_subscription_cancelled'] ) ) {
  138.             return;
  139.         }
  140.  
  141.         $form = simpay_get_form( $form_id );
  142.  
  143.         if ( false === $form ) {
  144.             return;
  145.         }
  146.  
  147.         // Retrieve the customer data
  148.  
  149.         $subscription = Subscription\retrieve(
  150.             array(
  151.                 'id'     => $subscription->id,
  152.                 'expand' => array(
  153.                     'customer',
  154.                 ),
  155.             ),
  156.             $form->get_api_request_args()
  157.         );
  158.  
  159.         $user = get_user_by( 'email', $subscription->customer->email );
  160.  
  161.         if ( $user ) {
  162.  
  163.             wp_fusion()->user->apply_tags( $settings['apply_tags_subscription_cancelled'], $user->ID );
  164.  
  165.             if ( ! empty( $settings['remove_tags'] ) && ! empty( $settings['apply_tags'] ) ) {
  166.                 wp_fusion()->user->remove_tags( $settings['apply_tags_subscription_cancelled'], $user->ID );
  167.             }
  168.         } else {
  169.  
  170.             $contact_id = wp_fusion()->crm->get_contact_id( $charge->customer->email );
  171.  
  172.             if ( $contact_id ) {
  173.                 wp_fusion()->crm->apply_tags( $settings['apply_tags_subscription_cancelled'], $contact_id );
  174.  
  175.                 if ( ! empty( $settings['remove_tags'] ) && ! empty( $settings['apply_tags'] ) ) {
  176.                     wp_fusion()->crm->remove_tags( $settings['apply_tags_subscription_cancelled'], $contact_id );
  177.                 }
  178.             }
  179.         }
  180.  
  181.     }
  182.  
  183.  
  184.     /**
  185.      * Sync the customer to the CRM.
  186.      *
  187.      * Runs on the simpay_after_customer_created hook and creates / updates the
  188.      * customer in the connected CRM, applying any tags.
  189.      *
  190.      * @since 3.30.4
  191.      *
  192.      * @param \Stripe\Customer $customer The customer.
  193.      */
  194.     public function customer_created( $customer ) {
  195.  
  196.         $form_id = $customer->metadata->simpay_form_id;
  197.  
  198.         $settings = get_post_meta( $form_id, 'wpf_settings_simple_pay', true );
  199.  
  200.         if ( empty( $settings ) || false == $settings['enable'] ) {
  201.             return;
  202.         }
  203.  
  204.         // Build the name
  205.  
  206.         $name = explode( ' ', $customer->name );
  207.  
  208.         $first_name = $name[0];
  209.  
  210.         unset( $name[0] );
  211.  
  212.         $last_name = implode( ' ', $name );
  213.  
  214.         $update_data = array(
  215.             'first_name' => $first_name,
  216.             'last_name'  => $last_name,
  217.             'user_email' => $customer->email,
  218.         );
  219.  
  220.         if ( wpf_is_user_logged_in() ) {
  221.  
  222.             wp_fusion()->user->push_user_meta( wpf_get_current_user_id(), $update_data );
  223.  
  224.             if ( ! empty( $settings['apply_tags'] ) ) {
  225.  
  226.                 wp_fusion()->user->apply_tags( $settings['apply_tags'] );
  227.  
  228.             }
  229.         } else {
  230.  
  231.             $contact_id = $this->guest_registration( $customer->email, $update_data );
  232.  
  233.             if ( $contact_id && ! empty( $settings['apply_tags'] ) ) {
  234.  
  235.                 wpf_log( 'info', 0, 'Simple Pay guest payment applying tag(s): ', array( 'tag_array' => $settings['apply_tags'] ) );
  236.  
  237.                 wp_fusion()->crm->apply_tags( $settings['apply_tags'], $contact_id );
  238.  
  239.             }
  240.         }
  241.  
  242.     }
  243.  
  244.  
  245.     /**
  246.      * Register the settings panel.
  247.      *
  248.      * @since  3.30.4
  249.      *
  250.      * @param  array $tabs    The tabs.
  251.      * @param  int   $post_id The post ID.
  252.      * @return array The tabs.
  253.      */
  254.     public function settings_tabs( $tabs, $post_id ) {
  255.  
  256.         $tabs['wp_fusion'] = array(
  257.             'label'  => 'WP Fusion',
  258.             'target' => 'wp-fusion-settings-panel',
  259.             'icon'   => wpf_logo_svg( '20px' ),
  260.         );
  261.  
  262.         return $tabs;
  263.  
  264.     }
  265.  
  266.  
  267.     /**
  268.      * Settings panel output.
  269.      *
  270.      * @since 3.30.4
  271.      *
  272.      * @param int   $post_id The post ID.
  273.      * @return mixed The settings panel output.
  274.      */
  275.     public function settings_options_panel( $post_id ) {
  276.  
  277.         $defaults = array(
  278.             'enable'                            => false,
  279.             'apply_tags'                        => array(),
  280.             'apply_tags_payment_failed'         => array(),
  281.             'apply_tags_subscription_cancelled' => array(),
  282.             'remove_tags'                       => false,
  283.         );
  284.  
  285.         $settings = get_post_meta( $post_id, 'wpf_settings_simple_pay', true );
  286.  
  287.         $settings = wp_parse_args( $settings, $defaults );
  288.  
  289.         ?>
  290.  
  291.         <div id="wp-fusion-settings-panel" class="simpay-panel simpay-panel-hidden simpay-panel--has-help">
  292.  
  293.             <table>
  294.                 <tbody class="simpay-panel-section">
  295.  
  296.                 <tr class="simpay-panel-field">
  297.                     <th>
  298.                         <label for="wpf-enable"><?php esc_html_e( 'Enable', 'wp-fusion' ); ?></label>
  299.                     </th>
  300.                     <td>
  301.  
  302.                         <input class="checkbox" type="checkbox" id="wpf-enable" name="wpf_settings_simple_pay[enable]" value="1" <?php checked( $settings['enable'], 1 ); ?> />
  303.                         <label for="wpf-enable"><?php echo sprintf( __( 'Sync customers with %s', 'wp-fusion' ), wp_fusion()->crm->name ); ?></label>
  304.  
  305.                         <?php
  306.                         /* <br /><br />
  307.                         <p class="description"><?php _e( 'Field mapping can be configured on the Custom Form Fields tab.', 'wp-fusion' ); ?></p> */
  308.                         ?>
  309.  
  310.                     </td>
  311.                 </tr>
  312.  
  313.                 <tr class="simpay-panel-field">
  314.                     <th>
  315.                         <label for="apply_tags"><?php esc_html_e( 'Apply Tags', 'wp-fusion' ); ?></label>
  316.                     </th>
  317.                     <td>
  318.  
  319.                         <?php
  320.  
  321.                         wpf_render_tag_multiselect(
  322.                             array(
  323.                                 'setting'   => $settings['apply_tags'],
  324.                                 'meta_name' => 'wpf_settings_simple_pay',
  325.                                 'field_id'  => 'apply_tags',
  326.                             )
  327.                         );
  328.  
  329.                         ?>
  330.  
  331.                         <p class="description"><?php echo sprintf( __( 'Select tags to apply in %s when a payment is received.', 'wp-fusion' ), wp_fusion()->crm->name ); ?></p>
  332.  
  333.                     </td>
  334.                 </tr>
  335.  
  336.                 <tr class="simpay-panel-field">
  337.                     <th>
  338.                         <label for="apply_tags"><?php esc_html_e( 'Apply Tags - Payment Failed', 'wp-fusion' ); ?></label>
  339.                     </th>
  340.                     <td>
  341.  
  342.                         <?php
  343.  
  344.                         wpf_render_tag_multiselect(
  345.                             array(
  346.                                 'setting'   => $settings['apply_tags_payment_failed'],
  347.                                 'meta_name' => 'wpf_settings_simple_pay',
  348.                                 'field_id'  => 'apply_tags_payment_failed',
  349.                             )
  350.                         );
  351.  
  352.                         ?>
  353.  
  354.                         <p class="description"><?php echo sprintf( __( 'Select tags to apply in %s when a recurring payment fails.', 'wp-fusion' ), wp_fusion()->crm->name ); ?></p>
  355.  
  356.                     </td>
  357.                 </tr>
  358.  
  359.                 <tr class="simpay-panel-field">
  360.                     <th>
  361.                         <label for="apply_tags"><?php esc_html_e( 'Apply Tags - Subscription Cancelled', 'wp-fusion' ); ?></label>
  362.                     </th>
  363.                     <td>
  364.  
  365.                         <?php
  366.  
  367.                         wpf_render_tag_multiselect(
  368.                             array(
  369.                                 'setting'   => $settings['apply_tags_subscription_cancelled'],
  370.                                 'meta_name' => 'wpf_settings_simple_pay',
  371.                                 'field_id'  => 'apply_tags_subscription_cancelled',
  372.                             )
  373.                         );
  374.  
  375.                         ?>
  376.  
  377.                         <p class="description"><?php echo sprintf( __( 'Select tags to apply in %s when a subscription is cancelled.', 'wp-fusion' ), wp_fusion()->crm->name ); ?></p>
  378.  
  379.                     </td>
  380.                 </tr>
  381.  
  382.                 <tr class="simpay-panel-field">
  383.                     <th>
  384.                         <label for="wpf-remove-tags"><?php esc_html_e( 'Remove Tags', 'wp-fusion' ); ?></label>
  385.                     </th>
  386.                     <td>
  387.  
  388.                         <input class="checkbox" type="checkbox" id="wpf-remove-tags" name="wpf_settings_simple_pay[remove_tags]" value="1" <?php checked( $settings['remove_tags'], 1 ); ?> />
  389.                         <label for="wpf-remove-tags"><?php echo __( 'Remove the tags applied with the initial purchase when a subscription is cancelled.', 'wp-fusion' ); ?></label>
  390.  
  391.                         <?php
  392.                         /* <br /><br />
  393.                         <p class="description"><?php _e( 'Field mapping can be configured on the Custom Form Fields tab.', 'wp-fusion' ); ?></p> */
  394.                         ?>
  395.  
  396.                     </td>
  397.                 </tr>
  398.  
  399.                 </tbody>
  400.             </table>
  401.  
  402.             <div class="simpay-docs-link-wrap">
  403.                 <a href="http://wpfusion.com/documentation/ecommerce/wp-simple-pay/" target="_blank" rel="noopener noreferrer">Help docs for WP Fusion<span class="dashicons dashicons-editor-help"></span></a>
  404.             </div>
  405.  
  406.         </div>
  407.  
  408.         <?php
  409.  
  410.     }
  411.  
  412.  
  413.     /**
  414.      * Saves settings.
  415.      *
  416.      * @since 3.30.4
  417.      *
  418.      * @param int     $post_id The post ID.
  419.      * @param WP_Post $post    The post.
  420.      */
  421.     public function save_settings( $post_id, $post ) {
  422.  
  423.         if ( isset( $_POST['wpf_settings_simple_pay'] ) ) {
  424.             update_post_meta( $post_id, 'wpf_settings_simple_pay', $_POST['wpf_settings_simple_pay'] );
  425.         } else {
  426.             delete_post_meta( $post_id, 'wpf_settings_simple_pay' );
  427.         }
  428.  
  429.     }
  430.  
  431. }
  432.  
  433. new WPF_Simple_Pay();
  434.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement