function email_notification( $blog_id, $action, $email = false, $args = array() ) { global $wpdb; if ( ! $email ) { $email = get_blog_option( $blog_id, 'admin_email' ); } // used in all emails $search_replace = array( 'LEVEL' => $this->get_level_setting( $this->get_level( $blog_id ), 'name' ), 'SITEURL' => get_home_url( $blog_id ), 'SITENAME' => get_blog_option( $blog_id, 'blogname' ), 'CHECKOUTURL' => $this->checkout_url( $blog_id ) ); // send emails as html (fixes some formatting issues with currencies) $mail_headers = array( 'Content-Type: text/html' ); switch ( $action ) { case 'success': $e = array( 'msg' => $this->get_setting( 'success_msg' ), 'subject' => $this->get_setting( 'success_subject' ) ); $e = str_replace( array_keys( $search_replace ), $search_replace, $e ); wp_mail( $email, $e['subject'], nl2br( $e['msg'] ), implode( "\r\n", $mail_headers ) ); $this->log_action( $blog_id, sprintf( __( 'Signup success email sent to %s', 'psts' ), $email ) ); break; case 'receipt': // NOTE: Stripe no longer uses this //grab default payment info $result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->base_prefix}pro_sites WHERE blog_ID = %d", $blog_id ) ); if ( $result->term == 1 || $result->term == 3 || $result->term == 12 ) { $term = sprintf( __( 'Every %s Month(s)', 'psts' ), $result->term ); } else { $term = $result->term; } $level = $result->level; $payment_info = ''; // Get current plan $level_list = get_site_option( 'psts_levels' ); $level_name = $level_list[ $level ]['name']; $payment_info .= sprintf( __( 'Current Plan: %s', 'psts' ), $level_name ) . "\n\n"; if ( $result->gateway ) { $nicename = ProSites_Helper_Gateway::get_nice_name( $result->gateway ); $payment_info .= sprintf( __( 'Payment Method: %s', 'psts' ), $nicename ) . "\n"; } if ( $term ) { $payment_info .= sprintf( __( 'Payment Term: %s', 'psts' ), $term ) . "\n"; } $trialing = ProSites_Helper_Registration::is_trial( $blog_id ); $amount = $trialing ? 0.0 : $result->amount; $payment_info .= sprintf( __( 'Payment Amount: %s', 'psts' ), $this->format_currency( false, $amount ) . ' ' . $this->get_setting( 'currency' ) ) . "\n"; if( ! empty( $args ) ) { if( isset( $args['items'] ) ) { $items = $args['items']; $items_total = ProSites_Model_Receipt::get_items_total( $items ); foreach( $items as $item ) { $symbol = $item['amount'] > 0 ? '' : '-'; $payment_info .= sprintf( '%s: %s%s', $item['description'], $symbol, $this->format_currency( false, abs( $item['amount'] ) ) . ' ' . $this->get_setting( 'currency' ) ) . "\n"; } $payment_info .= '<hr />'; $items_total = $items_total > 0 ? $items_total : 0; $symbol = $items_total >= 0 ? '' : '-'; $payment_info .= sprintf( __( 'Total Paid: %s%s', 'psts' ), $symbol, $this->format_currency( false, $items_total ) . ' ' . $this->get_setting( 'currency' ) ) . "\n"; } else { /** * @todo Remove prior to release */ if( isset( $args['setup_amount'] ) ) { $payment_info .= sprintf( __( 'One-Time Setup Fee: %s', 'psts' ), $this->format_currency( false, $args['setup_amount'] ) . ' ' . $this->get_setting( 'currency' ) ) . "\n"; } else { $args['setup_amount'] = 0; } if( isset( $args['discount_amount'] ) ) { $payment_info .= sprintf( __( 'Discount: -%s', 'psts' ), $this->format_currency( false, abs( $args['discount_amount'] ) ) . ' ' . $this->get_setting( 'currency' ) ) . "\n"; } else { $args['discount_amount'] = 0; } $zero_cost_change = false; if( isset( $args['plan_change_amount'] ) ) { switch( $args['plan_change_mode'] ) { case 'upgrade': $payment_info .= sprintf( __( 'Plan Modified: %s', 'psts' ), $this->format_currency( false, abs( $args['plan_change_amount'] ) ) . ' ' . $this->get_setting( 'currency' ) ) . "\n"; break; case 'downgrade': $payment_info .= sprintf( __( 'Plan Modified: -%s', 'psts' ), $this->format_currency( false, abs( $args['plan_change_amount'] ) ) . ' ' . $this->get_setting( 'currency' ) ) . "\n"; break; } $zero_cost_change = 0 > ( $amount + $args['setup_amount'] + $args['plan_change_amount'] - $args['discount_amount'] ); } else { $args['plan_change_amount'] = 0; } $payment_info .= sprintf( '<hr />' ); if( $zero_cost_change ) { // No cost to upgrade $payment_info .= sprintf( __( 'Total Paid: %s', 'psts' ), $this->format_currency( false, 0 ) . ' ' . $this->get_setting( 'currency' ) ) . "\n"; } else { $payment_info .= sprintf( __( 'Total Paid: %s', 'psts' ), $this->format_currency( false, ( $amount + $args['setup_amount'] + $args['plan_change_amount'] - $args['discount_amount'] ) ) . ' ' . $this->get_setting( 'currency' ) ) . "\n"; } } } if ( $result->gateway == 'Trial' || ! empty( $trialing ) ) { $trial_info = "\n" . __( '*** PLEASE NOTE ***', 'psts' ) . "\n"; $trial_info .= sprintf( __( 'You will not be charged for your subscription until your trial ends on %s. If applicable, this does not apply to setup fees and other upfront costs.', 'psts' ), date_i18n( get_option( 'date_format' ), $result->expire ) ); $payment_info .= apply_filters( 'psts_trial_info', $trial_info, $blog_id ); } $search_replace['PAYMENTINFO'] = apply_filters( 'psts_payment_info', $payment_info, $blog_id ); $e = array( 'msg' => $this->get_setting( 'receipt_msg' ), 'subject' => $this->get_setting( 'receipt_subject' ) ); $e = str_replace( array_keys( $search_replace ), $search_replace, $e ); wp_mail( $email, $e['subject'], nl2br( $e['msg'] ), implode( "\r\n", $mail_headers ), $this->pdf_receipt( $e['msg'] ) ); $this->log_action( $blog_id, sprintf( __( 'Payment receipt email sent to %s', 'psts' ), $email ) ); break; case 'canceled': //get end date from expiration $end_date = date_i18n( get_blog_option( $blog_id, 'date_format' ), $this->get_expire( $blog_id ) ); $search_replace['ENDDATE'] = $end_date; $e = array( 'msg' => $this->get_setting( 'canceled_msg' ), 'subject' => $this->get_setting( 'canceled_subject' ) ); $e = str_replace( array_keys( $search_replace ), $search_replace, $e ); wp_mail( $email, $e['subject'], nl2br( $e['msg'] ), implode( "\r\n", $mail_headers ) ); $this->log_action( $blog_id, sprintf( __( 'Subscription canceled email sent to %s', 'psts' ), $email ) ); break; case 'expired': $e = array( 'msg' => $this->get_setting( 'expired_msg' ), 'subject' => $this->get_setting( 'expired_subject' ) ); $e = str_replace( array_keys( $search_replace ), $search_replace, $e ); wp_mail( $email, $e['subject'], nl2br( $e['msg'] ), implode( "\r\n", $mail_headers ) ); $this->log_action( $blog_id, sprintf( __( 'Expired email sent to %s', 'psts' ), $email ) ); break; case 'failed': $e = array( 'msg' => $this->get_setting( 'failed_msg' ), 'subject' => $this->get_setting( 'failed_subject' ) ); $e = str_replace( array_keys( $search_replace ), $search_replace, $e ); wp_mail( $email, $e['subject'], nl2br( $e['msg'] ), implode( "\r\n", $mail_headers ) ); $this->log_action( $blog_id, sprintf( __( 'Payment failed email sent to %s', 'psts' ), $email ) ); break; case 'extension': //get end date from expiration $end_date = date_i18n( get_blog_option( $blog_id, 'date_format' ), $this->get_expire( $blog_id ) ); if( ! empty( $args ) && isset( $args['indefinite'] ) ) { $end_date = __( 'Indefinitely', 'psts' ); } $search_replace['ENDDATE'] = $end_date; $e = array( 'msg' => $this->get_setting( 'extension_msg' ), 'subject' => $this->get_setting( 'extension_subject' ) ); $e = str_replace( array_keys( $search_replace ), $search_replace, $e ); wp_mail( $email, $e['subject'], nl2br( $e['msg'] ), implode( "\r\n", $mail_headers ) ); $this->log_action( $blog_id, sprintf( __( 'Manual extension email sent to %s', 'psts' ), $email ) ); break; } }
/** * Uses Stripe API json object to retrieve the subscription object. * * @param $response * * @return bool */ public static function get_subscription($response) { $object = $response->data->object; $from_invoice = 'invoice' == $object->object ? true : false; $from_sub = 'subscription' == $object->object ? true : false; if (!$from_invoice && !$from_sub) { return false; } $subscription = false; $coupon = false; $has_setup = false; $setup_fee_amount = 0; $plan_change = false; $invoice_items = false; if ($from_invoice) { $invoice_items = new ProSites_Model_Receipt(); $last_line_item = false; if (isset($object->metadata->plan_change) && 'yes' == $object->metadata->plan_change) { $plan_change = true; } foreach ($object->lines->data as $line_item) { $last_line_item = $line_item; // Get subscription if ('subscription' == $line_item->type) { $subscription = $line_item; continue; } // Get setup fee if (isset($line_item->metadata->setup_fee) && 'yes' == $line_item->metadata->setup_fee) { $has_setup = true; $setup_fee_amount = $line_item->amount / 100; $invoice_items->add_item($setup_fee_amount, $line_item->description); continue; } // Get upgrades/downgrades if ($plan_change && $line_item->proration) { $plan_name = $line_item->plan->name; $amount = $line_item->amount / 100; $invoice_items->add_item($amount, sprintf(__('Plan Adjustments: %s', 'psts'), $plan_name)); continue; } } if (!$subscription && !$last_line_item) { return false; } if (!$subscription) { if ('invoiceitem' == $line_item->type && isset($line_item->subscription) && isset($line_item->period) && isset($line_item->plan)) { $customer = Stripe_Customer::retrieve($object->customer); $subscription = $customer->subscriptions->retrieve($line_item->subscription); } else { return false; } } if (isset($object->discount) && isset($object->discount->coupon)) { $coupon = $object->discount->coupon; } // Get fields from Invoice $subscription->customer_id = $object->customer; $subscription->paid = $object->paid; $subscription->currency = $object->currency; $subscription->last_charge = $object->charge; $subscription->invoice_total = $object->total / 100; } if ($from_sub) { $subscription = $object; } $subscription->period_end = isset($subscription->period) ? $subscription->period->end : false; $subscription->period_end = empty($subscription->period_end) && isset($subscription->current_period_end) ? $subscription->current_period_end : $subscription->period_end; $subscription->period_start = isset($subscription->period) ? $subscription->period->start : false; $subscription->period_start = empty($subscription->period_start) && isset($subscription->current_period_start) ? $subscription->current_period_start : $subscription->period_start; // Get fields from subscription meta // 3.4 $parts = explode('_', $subscription->plan->id); $period = (int) array_pop($parts); $level = (int) array_pop($parts); $subscription->period = !empty($subscription->metadata->period) ? $subscription->metadata->period : $period; $subscription->level = !empty($subscription->metadata->level) ? $subscription->metadata->level : $level; $subscription->activation = isset($subscription->metadata->activation) ? $subscription->metadata->activation : false; $subscription->blog_id = isset($subscription->metadata->blog_id) ? (int) $subscription->metadata->blog_id : false; if (empty($subscription->blog_id)) { $subscription->blog_id = ProSites_Helper_ProSite::get_blog_id($subscription->activation); } $subscription->is_trial = isset($subscription->status) && 'trialing' == $subscription->status ? true : false; $subscription->trial_end = isset($subscription->trial_end) ? $subscription->trial_end : false; $subscription->trial_start = isset($subscription->trial_start) ? $subscription->trial_start : false; $subscription->subscription_amount = isset($subscription->amount) ? $subscription->amount / 100 : $subscription->plan->amount / 100; $subscription->plan_amount = $subscription->is_trial ? $subscription->plan->amount / 100 : $subscription->subscription_amount; $subscription->discount_amount = 0; if ($coupon) { $subscription->discount_amount = $coupon->amount_off / 100; if ($invoice_items) { $invoice_items->add_item($subscription->discount_amount, __('Coupon Applied', 'psts')); } } $subscription->has_discount = empty($coupon) ? false : true; $item_count = 0; if ($invoice_items) { // Get array and add to subscription $invoice_items = $invoice_items->get_items(); $item_count = count($invoice_items); } $subscription->invoice_items = $item_count > 0 ? $invoice_items : false; // Get setup fee $subscription->setup_fee = $setup_fee_amount; $subscription->has_setup_fee = $has_setup; $subscription->plan_change_amount = isset($subscription->invoice_total) ? $subscription->invoice_total - $subscription->subscription_amount : 0; if ($coupon && $plan_change) { $subscription->plan_change_amount + $subscription->discount_amount; } $subscription->plan_change = $plan_change; //inverse if ($subscription->plan_change_amount < 0) { $subscription->plan_change_mode = 'upgrade'; } else { $subscription->plan_change_mode = 'downgrade'; } return $subscription; }
public function get_total() { return ProSites_Model_Receipt::get_items_total($this->items); }