/** * @param array $process_data * @param $blog_id * @param $domain * * @return bool */ public static function process_checkout_form($process_data = array(), $blog_id, $domain) { global $psts, $current_user, $current_site; $site_name = $current_site->site_name; $img_base = $psts->plugin_url . 'images/'; $session_keys = array('new_blog_details', 'upgraded_blog_details', 'COUPON_CODE', 'activation_key'); foreach ($session_keys as $key) { $process_data[$key] = isset($process_data[$key]) ? $process_data[$key] : ProSites_Helper_Session::session($key); } if (!empty($domain)) { $site_name = !empty($_POST['blogname']) ? $_POST['blogname'] : !empty($_POST['signup_email']) ? $_POST['signup_email'] : ''; } //Process Checkout if (isset($_POST['cc_stripe_checkout']) && 1 == (int) $_POST['cc_stripe_checkout']) { //check for level, if empty don't go ahead and return if (empty($_POST['level']) || empty($_POST['period'])) { $psts->errors->add('general', __('Please choose your desired level and payment plan.', 'psts')); return false; } else { if (!isset($_POST['stripeToken']) && empty($_POST['wp_password'])) { $psts->errors->add('general', __('There was an error processing your Credit Card with Stripe. Please try again.', 'psts')); } } // TAX Object $tax_object = ProSites_Helper_Session::session('tax_object'); if (empty($tax_object) || empty($tax_object->evidence)) { $tax_object = ProSites_Helper_Tax::get_tax_object(); ProSites_Helper_Session::session('tax_object', $tax_object); } $evidence_string = ProSites_Helper_Tax::get_evidence_string($tax_object); $error = ''; $success = ''; $plan = self::get_plan_id($_POST['level'], $_POST['period']); $customer_id = ''; $current_plan_level = 0; $activation_key = isset($_POST['activation']) ? $_POST['activation'] : ''; $email = !empty($_POST['user_email']) ? $_POST['user_email'] : (!empty($_POST['signup_email']) ? $_POST['signup_email'] : (!empty($_POST['blog_email']) ? $_POST['blog_email'] : '')); $blog_id = !empty($blog_id) ? $blog_id : isset($_REQUEST['bid']) ? (int) $_REQUEST['bid'] : 0; //If there is a blog id, fetch existing customer details (Upgrade) if (!empty($blog_id)) { $customer_id = self::get_customer_data($blog_id)->customer_id; $email = isset($current_user->user_email) ? $current_user->user_email : get_blog_option($blog_id, 'admin_email'); //Get current plan for the user if ($current_plan = self::get_current_plan($blog_id)) { list($current_plan_level, $current_plan_period) = explode('_', $current_plan); } } else { //New Signup if (empty($email) && isset($process_data['new_blog_details']) && isset($process_data['new_blog_details']['user_email'])) { $email = $process_data['new_blog_details']['user_email']; } $customer_id = self::get_customer_data(false, false, $email)->customer_id; } if (!self::plan_exists($plan)) { $psts->errors->add('general', sprintf(__('Stripe plan %1$s does not exist.', 'psts'), $plan)); return; } //Create s Stripe profile for the customer with all the available details, //If customer already exists, retrieve customer from stripe try { if (!$customer_id) { try { $customer_args = array('email' => $email, 'description' => sprintf(__('%s user', 'psts'), $site_name), 'card' => $_POST['stripeToken'], 'metadata' => array('domain' => $domain)); $user = get_user_by('email', $email); if ($user) { $blog_string = ''; $customer_args['metadata']['user'] = $user->user_login; $customer_args['description'] = sprintf(__('%s user - %s ', 'psts'), $site_name, $user->first_name . ' ' . $user->last_name); $user_blogs = get_blogs_of_user($user->ID); foreach ($user_blogs as $user_blog) { $blog_string .= $user_blog->blogname . ', '; } $customer_args['metadata']['blogs'] = $blog_string; } if (!$domain) { unset($customer_args['metadata']['domain']); } $c = Stripe_Customer::create($customer_args); } catch (Exception $e) { $psts->errors->add('general', __('The Stripe customer could not be created. Please try again.', 'psts')); return; } //Update the stripe customer id, this is temporary, will be overridden by subscription or charge id self::set_customer_data($blog_id, $c->id, 'ak_' . $activation_key); $customer_id = $c->id; $new = true; } else { // Get a customer if they exist try { $c = Stripe_Customer::retrieve($customer_id); } catch (Exception $e) { $psts->errors->add('general', __('The Stripe customer could not be retrieved. Please try again.', 'psts')); return; } $c->description = sprintf(__('%s user', 'psts'), $site_name); $c->email = $email; $user = get_user_by('email', $email); if ($user) { $blog_string = ''; $c->metadata->user = $user->user_login; $c->description = sprintf(__('%s user - %s ', 'psts'), $site_name, $user->first_name . ' ' . $user->last_name); $user_blogs = get_blogs_of_user($user->ID); foreach ($user_blogs as $user_blog) { $blog_string .= $user_blog->blogname . ', '; } $c->metadata->blogs = $blog_string; } $c->save(); $new = false; //validate wp password (if applicable) if (!empty($_POST['wp_password']) && !wp_check_password($_POST['wp_password'], $current_user->data->user_pass, $current_user->ID)) { $psts->errors->add('general', __('The password you entered is incorrect.', 'psts')); return; } } //prepare vars $currency = self::currency(); $amount_off = false; $paymentAmount = $initAmount = $psts->get_level_setting($_POST['level'], 'price_' . $_POST['period']); $trial_days = $psts->get_setting('trial_days', 0); $cp_code = false; $setup_fee = (double) $psts->get_setting('setup_fee', 0); $has_coupon = isset($process_data['COUPON_CODE']) && ProSites_Helper_Coupons::check_coupon($process_data['COUPON_CODE'], $blog_id, $_POST['level'], $_POST['period'], $domain) ? true : false; $has_setup_fee = $psts->has_setup_fee($blog_id, $_POST['level']); $recurring = $psts->get_setting('recurring_subscriptions', 1); if ($has_setup_fee) { $initAmount = $setup_fee + $paymentAmount; } if ($has_coupon || $has_setup_fee) { $lifetime = 'once'; if ($has_coupon) { //apply coupon $adjusted_values = ProSites_Helper_Coupons::get_adjusted_level_amounts($process_data['COUPON_CODE']); $coupon_obj = ProSites_Helper_Coupons::get_coupon($process_data['COUPON_CODE']); $lifetime = isset($coupon_obj['lifetime']) && 'indefinite' == $coupon_obj['lifetime'] ? 'forever' : 'once'; // $coupon_value = $psts->coupon_value( $process_data['COUPON_CODE'], $paymentAmount ); $coupon_value = $adjusted_values[$_POST['level']]['price_' . $_POST['period']]; // $amount_off = $paymentAmount - $coupon_value['new_total']; $amount_off = $paymentAmount - $coupon_value; $initAmount -= $amount_off; $initAmount = 0 > $initAmount ? 0 : $initAmount; // avoid negative $cpn = false; //Create a stripe coupon if it doesn't exists already try { $cpn = Stripe_Coupon::create(array('amount_off' => $amount_off * 100, 'duration' => $lifetime, 'currency' => $currency, 'max_redemptions' => 1)); } catch (Exception $e) { $psts->errors->add('general', __('Temporary Stripe coupon could not be generated correctly. Please try again.', 'psts')); return; } $cp_code = $cpn->id; } //Check if it's a recurring subscription if ($recurring) { $recurringAmmount = 'forever' == $lifetime && $has_coupon ? $coupon_value : $paymentAmount; if ($_POST['period'] == 1) { $desc = $site_name . ' ' . $psts->get_level_setting($_POST['level'], 'name') . ': ' . sprintf(__('%1$s for the first month, then %2$s each month', 'psts'), $psts->format_currency($currency, $initAmount), $psts->format_currency($currency, $recurringAmmount)); } else { $desc = $site_name . ' ' . $psts->get_level_setting($_POST['level'], 'name') . ': ' . sprintf(__('%1$s for the first %2$s month period, then %3$s every %4$s months', 'psts'), $psts->format_currency($currency, $initAmount), $_POST['period'], $psts->format_currency($currency, $recurringAmmount), $_POST['period']); } } else { if (!empty($blog_id)) { $initAmount = $psts->calc_upgrade_cost($blog_id, $_POST['level'], $_POST['period'], $initAmount); } if ($_POST['period'] == 1) { $desc = $site_name . ' ' . $psts->get_level_setting($_POST['level'], 'name') . ': ' . sprintf(__('%1$s for 1 month', 'psts'), $psts->format_currency($currency, $initAmount)); } else { $desc = $site_name . ' ' . $psts->get_level_setting($_POST['level'], 'name') . ': ' . sprintf(__('%1$s for %2$s months', 'psts'), $psts->format_currency($currency, $initAmount), $_POST['period']); } } } elseif ($recurring) { if ($_POST['period'] == 1) { $desc = $site_name . ' ' . $psts->get_level_setting($_POST['level'], 'name') . ': ' . sprintf(__('%1$s %2$s each month', 'psts'), $psts->format_currency($currency, $paymentAmount), $currency); } else { $desc = $site_name . ' ' . $psts->get_level_setting($_POST['level'], 'name') . ': ' . sprintf(__('%1$s %2$s every %3$s months', 'psts'), $psts->format_currency($currency, $paymentAmount), $currency, $_POST['period']); } } else { //New Signups if (!empty($blog_id)) { $paymentAmount = $psts->calc_upgrade_cost($blog_id, $_POST['level'], $_POST['period'], $paymentAmount); } if ($_POST['period'] == 1) { $desc = $site_name . ' ' . $psts->get_level_setting($_POST['level'], 'name') . ': ' . sprintf(__('%1$s for 1 month', 'psts'), $psts->format_currency($currency, $paymentAmount)); } else { $desc = $site_name . ' ' . $psts->get_level_setting($_POST['level'], 'name') . ': ' . sprintf(__('%1$s for %2$s months', 'psts'), $psts->format_currency($currency, $paymentAmount), $_POST['period']); } } // Override the Stripe description $desc = apply_filters('psts_stripe_checkout_desc', $desc, $_POST['period'], $_POST['level'], $paymentAmount, $initAmount, $blog_id, $domain); // Time to process invoices with Stripe if ($recurring) { // Recurring subscription // Assign plan to customer $args = array("plan" => $plan, "prorate" => true); // Apply tax? if ($tax_object->apply_tax) { $args['tax_percent'] = $tax_object->tax_rate * 100; } // If there is a coupon, add its reference if ($cp_code) { $args["coupon"] = $cp_code; } // If this is a trial before the subscription starts if ($psts->is_trial_allowed($blog_id)) { if (isset($process_data['new_blog_details']) || !$psts->is_existing($blog_id)) { //customer is new - add trial days $args['trial_end'] = strtotime('+ ' . $trial_days . ' days'); } elseif (is_pro_trial($blog_id) && $psts->get_expire($blog_id) > time()) { //customer's trial is still valid - carry over existing expiration date $args['trial_end'] = $psts->get_expire($blog_id); } } // Meta data for `pay before blog` creation $args['metadata'] = array('domain' => !empty($domain) ? $domain : '', 'period' => $_POST['period'], 'level' => $_POST['level']); if ($tax_object->apply_tax) { $args['metadata']['tax_evidence'] = $evidence_string; } if (!$domain) { unset($args['metadata']['domain']); } // new blog if (isset($_POST['activation'])) { $args['metadata']['activation'] = $_POST['activation']; } // Create Stripe Invoice for the setup fee if ($has_setup_fee) { try { $customer_args = array('customer' => $customer_id, 'amount' => $setup_fee * 100, 'currency' => $currency, 'description' => __('One-time setup fee', 'psts'), 'metadata' => array('domain' => !empty($domain) ? $domain : '', 'period' => $_POST['period'], 'level' => $_POST['level'], 'setup_fee' => 'yes')); if ($tax_object->apply_tax) { $customer_args['metadata']['tax_evidence'] = $evidence_string; } if (!$domain) { unset($customer_args['metadata']['domain']); } // new blog if (isset($_POST['activation'])) { $customer_args['metadata']['activation'] = $_POST['activation']; } Stripe_InvoiceItem::create($customer_args); } catch (Exception $e) { wp_mail(get_blog_option($blog_id, 'admin_email'), __('Error charging setup fee. Attention required!', 'psts'), sprintf(__('An error occurred while charging a setup fee of %1$s to Stripe customer %2$s. You will need to manually process this amount.', 'psts'), $psts->format_currency($currency, $setup_fee), $customer_id)); } } // Create/update subscription try { $result = false; $sub = false; // Brand new blog... if (empty($blog_id)) { $result = $c->subscriptions->create($args); } else { $customer_data = self::get_customer_data($blog_id); try { $sub = $c->subscriptions->retrieve($customer_data->subscription_id); $sub_id = $sub->id; $prev_plan = $sub->plan->id; $sub->plan = isset($args['plan']) ? $args['plan'] : $sub->plan; $changed_plan = $sub->plan; $sub->prorate = isset($args['prorate']) ? $args['prorate'] : $sub->prorate; if (isset($args['coupon'])) { $sub->coupon = $args['coupon']; } if (isset($args['trial_end'])) { $sub->trial_end = $args['trial_end']; } $sub->metadata->period = $args['metadata']['period']; $sub->metadata->level = $args['metadata']['level']; if (isset($args['metadata']['activation'])) { $sub->metadata->activation = $args['metadata']['activation']; } else { $activation_key = ProSites_Helper_ProSite::get_activation_key($blog_id); if (!empty($activation_key)) { $sub->metadata->activation = $activation_key; } } $sub->metadata->blog_id = $blog_id; if (isset($args['metadata']['domain'])) { $sub->metadata->domain = $args['metadata']['domain']; } // Apply tax? if ($tax_object->apply_tax) { $sub->tax_percent = $tax_object->tax_rate * 100; $sub->metadata->tax_evidence = $evidence_string; } $sub->save(); // As per Stripe API, to charge immediately, apply an invoice now if ($prev_plan != $changed_plan) { $customer_args = array('customer' => $customer_id, 'subscription' => $sub_id, 'metadata' => array('plan_change' => 'yes')); $invoice = Stripe_Invoice::create($customer_args); $invoice = $invoice->pay(); $plan_parts = explode('_', $changed_plan); $new_period = array_pop($plan_parts); $new_level = array_pop($plan_parts); $plan_parts = explode('_', $prev_plan); $prev_period = array_pop($plan_parts); $prev_level = array_pop($plan_parts); $updated = array('render' => true, 'blog_id' => $blog_id, 'level' => $new_level, 'period' => $new_period, 'prev_level' => $prev_level, 'prev_period' => $prev_period); ProSites_Helper_Session::session('plan_updated', $updated); } } catch (Exception $e) { // Fall through... } // Additional blog if (empty($sub)) { $args['metadata']['blog_id'] = $blog_id; if (!isset($args['metadata']['activation'])) { $activation_key = ProSites_Helper_ProSite::get_activation_key($blog_id); if (!empty($activation_key)) { $args['metadata']['activation'] = $activation_key; } } $result = $c->subscriptions->create($args); } // This one is now deprecated // $result = $c->updateSubscription( $args ); } // Capture success as soon as we can! if (!empty($result)) { $sub_id = $result->id; $plan = $result->plan; $plan_parts = explode('_', $plan->id); $period = array_pop($plan_parts); $level = array_pop($plan_parts); $trial = isset($plan->status) && 'trialing' == $plan->status ? true : false; $expire = $trial ? $plan->trial_end : $result->current_period_end; $result = ProSites_Helper_Registration::activate_blog($activation_key, $trial, $period, $level, $expire); $blog_id = $result['blog_id']; if (isset($process_data['new_blog_details'])) { ProSites_Helper_Session::session(array('new_blog_details', 'blog_id'), $blog_id); ProSites_Helper_Session::session(array('new_blog_details', 'payment_success'), true); } else { ProSites_Helper_Session::session('upgrade_blog_details', array()); ProSites_Helper_Session::session(array('upgrade_blog_details', 'blog_id'), $blog_id); ProSites_Helper_Session::session(array('upgrade_blog_details', 'level'), $level); ProSites_Helper_Session::session(array('upgrade_blog_details', 'period'), $period); ProSites_Helper_Session::session(array('upgrade_blog_details', 'payment_success'), true); } self::set_customer_data($blog_id, $customer_id, $sub_id); } if ($current_plan = self::get_current_plan($blog_id)) { list($current_plan_level, $current_plan_period) = explode('_', $current_plan); } //Extend the Blog Subscription $old_expire = $psts->get_expire($blog_id); $new_expire = $old_expire && $old_expire > time() ? $old_expire : false; $psts->extend($blog_id, $_POST['period'], self::get_slug(), $_POST['level'], $initAmount, $new_expire, false); //$psts->email_notification( $blog_id, 'receipt' ); if (isset($current_plan_level)) { if ($current_plan_level > $_POST['level']) { $psts->record_stat($blog_id, 'upgrade'); } else { $psts->record_stat($blog_id, 'modify'); } } else { $psts->record_stat($blog_id, 'signup'); } // Update the sub with the new blog id (old subscriptions will update later). if (!empty($blog_id)) { $sub = $c->subscriptions->retrieve($sub_id); $sub->metadata->blog_id = $blog_id; $sub->save(); } } catch (Exception $e) { $body = $e->getJsonBody(); $error = $body['error']; $psts->errors->add('general', $error['message']); return; } } else { // Not a subscription, this is a one of payment, charged for 1 term try { if (!empty($blog_id)) { $initAmount = $psts->calc_upgrade_cost($blog_id, $_POST['level'], $_POST['period'], $initAmount); //If activation key is empty $activation_key = !empty($activation_key) ? $activation_key : ProSites_Helper_ProSite::get_activation_key($blog_id); } if ($tax_object->apply_tax) { $amount = $initAmount + $initAmount * $tax_object->tax_rate; $desc += sprintf(__('(includes tax of %s%% [%s])', 'psts'), $tax_object->tax_rate * 100, $tax_object->country); } else { $amount = $initAmount; } $customer_args = array('customer' => $customer_id, 'amount' => $amount * 100, 'currency' => $currency, 'description' => $desc, 'metadata' => array('domain' => !empty($domain) ? $domain : '', 'period' => $_POST['period'], 'level' => $_POST['level'])); if ($tax_object->apply_tax) { $customer_args['metadata']['tax_evidence'] = $evidence_string; } if (!$domain) { unset($customer_args['metadata']['domain']); } // new blog if (isset($_POST['activation'])) { $customer_args['metadata']['activation'] = $_POST['activation']; } /** * 1 off charge of not trialing, but if trialing, just send a zero-dollar invoice */ if (empty($trial_days) || 0 == $customer_args['amount']) { $result = Stripe_Charge::create($customer_args); } else { $result = Stripe_InvoiceItem::create($customer_args); } // Capture success as soon as we can! if ($result) { $period = (int) $_POST['period']; $level = (int) $_POST['level']; $signup_details = ProSites_Helper_Registration::activate_blog($activation_key, false, $period, $level); $blog_id = $signup_details['blog_id']; if (isset($process_data['new_blog_details'])) { ProSites_Helper_Session::session(array('new_blog_details', 'blog_id'), $blog_id); ProSites_Helper_Session::session(array('new_blog_details', 'payment_success'), true); } self::set_customer_data($blog_id, $customer_id, $result->id); } if ($current_plan = self::get_current_plan($blog_id)) { list($current_plan_level, $current_plan_period) = explode('_', $current_plan); } $old_expire = $psts->get_expire($blog_id); $new_expire = $old_expire && $old_expire > time() ? $old_expire : false; $psts->extend($blog_id, $_POST['period'], self::get_slug(), $_POST['level'], $initAmount, $new_expire, false); //$psts->email_notification( $blog_id, 'receipt' ); if (isset($current_plan_level)) { if ($current_plan_level > $_POST['level']) { $psts->record_stat($blog_id, 'upgrade'); } else { $psts->record_stat($blog_id, 'modify'); } } else { $psts->record_stat($blog_id, 'signup'); } } catch (Stripe_CardError $e) { $body = $e->getJsonBody(); $err = $body['error']; $psts->errors->add('general', $e['message']); } catch (Exception $e) { $psts->errors->add('general', __('An unknown error occurred while processing your payment. Please try again.', 'psts')); } } //delete the temporary coupon code if ($cp_code) { try { $cpn = Stripe_Coupon::retrieve($cp_code); $cpn->delete(); } catch (Exception $e) { wp_mail(get_blog_option($blog_id, 'admin_email'), __('Error deleting temporary Stripe coupon code. Attention required!.', 'psts'), sprintf(__('An error occurred when attempting to delete temporary Stripe coupon code %1$s. You will need to manually delete this coupon via your Stripe account.', 'psts'), $cp_code)); } $psts->use_coupon($process_data['COUPON_CODE'], $blog_id, $domain); } if ($new || $psts->is_blog_canceled($blog_id)) { // Added for affiliate system link if ($recurring) { $psts->log_action($blog_id, sprintf(__('User creating new subscription via CC: Subscription created (%1$s) - Customer ID: %2$s', 'psts'), $desc, $customer_id), $domain); } else { $psts->log_action($blog_id, sprintf(__('User completed new payment via CC: Site created/extended (%1$s) - Customer ID: %2$s', 'psts'), $desc, $customer_id), $domain); } do_action('supporter_payment_processed', $blog_id, $paymentAmount, $_POST['period'], $_POST['level']); } else { $psts->log_action($blog_id, sprintf(__('User modifying subscription via CC: Plan changed to (%1$s) - %2$s', 'psts'), $desc, $customer_id), $domain); } //display GA ecommerce in footer $psts->create_ga_ecommerce($blog_id, $_POST['period'], $initAmount, $_POST['level'], $site_name, $domain); if (!empty($blog_id)) { update_blog_option($blog_id, 'psts_stripe_canceled', 0); /* some times there is a lag receiving webhooks from Stripe. we want to be able to check for that and display an appropriate message to the customer (e.g. there are changes pending to your account) */ update_blog_option($blog_id, 'psts_stripe_waiting', 1); } else { if (isset($process_data['activation_key'])) { //Update signup meta $key = $process_data['activation_key']; $signup_meta = ''; $signup_meta = $psts->get_signup_meta($key); $signup_meta['psts_stripe_canceled'] = 0; $signup_meta['psts_stripe_waiting'] = 1; $psts->update_signup_meta($signup_meta, $key); } } update_blog_option($blog_id, 'psts_stripe_waiting', 1); if (empty(self::$complete_message)) { // Message is redundant now, but still used as a flag. self::$complete_message = __('Your payment was successfully recorded! You should be receiving an email receipt shortly.', 'psts'); } } catch (Exception $e) { $psts->errors->add('general', $e->getMessage()); } } }
/** * Verify that a coupon with a given ID exists, or create a new one if it does * not. */ protected static function retrieveOrCreateCoupon($id) { self::authorizeFromEnv(); try { $coupon = Stripe_Coupon::retrieve($id); } catch (Stripe_InvalidRequestError $exception) { $coupon = Stripe_Coupon::create(array('id' => $id, 'duration' => 'forever', 'percent_off' => 25)); } }
public function check_coupon($data = array()) { $stripeapikey = $this->wlm->GetOption('stripeapikey'); Stripe::setApiKey($stripeapikey); try { $coupon = Stripe_Coupon::retrieve($data['coupon']); echo json_encode(true); } catch (Exception $e) { echo json_encode(false); } die; }
/** * Delete a coupon */ public function deleteAction($id) { \Stripe::setApiKey($this->container->getParameter('avro_stripe.secret_key')); try { $coupon = \Stripe_Coupon::retrieve($id); $coupon->delete(); $this->container->get('session')->setFlash('success', 'coupon.deleted.flash'); } catch (\Exception $e) { $this->container->get('session')->setFlash('error', 'coupon.notDeleted.flash'); } return new RedirectResponse($this->container->get('router')->generate('avro_stripe_coupon_list')); }
public function testSave() { self::authorizeFromEnv(); $id = 'test_coupon-' . self::randomString(); $c = Stripe_Coupon::create(array('percent_off' => 25, 'duration' => 'repeating', 'duration_in_months' => 5, 'id' => $id)); $this->assertEqual($id, $c->id); // @codingStandardsIgnoreStart $this->assertEqual(25, $c->percent_off); // @codingStandardsIgnoreEnd $c->metadata['foo'] = 'bar'; $c->save(); $stripeCoupon = Stripe_Coupon::retrieve($id); $this->assertEqual($c->metadata, $stripeCoupon->metadata); }
public static function coupon() { require_once 'lib/Stripe.php'; Stripe::setApiKey(STRIPE_SECRET); // Verify required data if (empty($_REQUEST['coupon'])) { self::fail('Please enter a coupon code.'); } try { $coupon = Stripe_Coupon::retrieve($_REQUEST['coupon']); } catch (Exception $e) { self::fail('Invalid coupon id'); } echo json_encode(array('percent_off' => $coupon->percent_off, 'amount_off' => $coupon->amount_off)); exit; }
function getCouponInfo($coupon = null) { $resp['code'] = ''; $resp['message'] = ''; try { $resp = Stripe_Coupon::retrieve($coupon); return $resp; $resp['code'] = 'OK'; $resp['message'] = $resp->percent_off; return $resp; } catch (Stripe_CardError $e) { $body = $e->getJsonBody(); $resp = $body['error']; return $resp; } catch (Stripe_InvalidRequestError $e) { // Invalid parameters were supplied to Stripe's API $body = $e->getJsonBody(); $resp = $body['error']; return $resp; } catch (Stripe_AuthenticationError $e) { // Authentication with Stripe's API failed // (maybe you changed API keys recently) $body = $e->getJsonBody(); $resp = $body['error']; return $resp; } catch (Stripe_ApiConnectionError $e) { // Network communication with Stripe failed $body = $e->getJsonBody(); $resp = $body['error']; return $resp; } catch (Stripe_Error $e) { // Display a very generic error to the user, and maybe send // yourself an email $body = $e->getJsonBody(); $resp = $body['error']; return $resp; } catch (Exception $e) { // Something else happened, completely unrelated to Stripe $body = $e->getJsonBody(); $resp = $body['error']; return $resp; } // if nothing is returned above, return null. should never happen. return null; }
public function getCouponById($coupon_id) { $coupon = \Stripe_Coupon::retrieve($coupon_id); if ($coupon->valid) { return $coupon; } else { throw new \Stripe_Error('Coupon is expired or invalid.'); } }
public function deleteCoupon($stripeId) { $response = array(); try { $c = \Stripe_Coupon::retrieve($stripeId); $c->delete(); return true; } catch (\Exception $e) { $this->errors[] = 'Failed to delete coupon - ' . $e->getMessage(); return false; } return false; }
function process_checkout($blog_id) { global $current_site, $current_user, $psts, $wpdb; //add scripts add_action('wp_head', array(&$this, 'checkout_js')); $stripe_secret_key = $psts->get_setting('stripe_secret_key'); $stripe_publishable_key = $psts->get_setting('stripe_publishable_key'); wp_enqueue_script('js-stripe', 'https://js.stripe.com/v1/', array('jquery')); wp_enqueue_script('stripe-token', $psts->plugin_url . 'gateways/gateway-stripe-files/stripe_token.js', array('js-stripe', 'jquery')); wp_localize_script('stripe-token', 'stripe', array('publisher_key' => $stripe_publishable_key, 'name' => __('Please enter the full Cardholder Name.', 'psts'), 'number' => __('Please enter a valid Credit Card Number.', 'psts'), 'expiration' => __('Please choose a valid expiration date.', 'psts'), 'cvv2' => __('Please enter a valid card security code. This is the 3 digits on the signature panel, or 4 digits on the front of Amex cards.', 'psts'))); if (isset($_POST['cc_checkout']) && empty($_POST['coupon_code'])) { //check for level if (empty($_POST['level']) || empty($_POST['period'])) { $psts->errors->add('general', __('Please choose your desired level and payment plan.', 'psts')); return; } else { if (!isset($_POST['stripeToken'])) { $psts->errors->add('general', __('The Stripe Token was not generated correctly. Please try again.', 'psts')); return; } } $error = ''; $success = ''; $plan = $_POST['level'] . '_' . $_POST['period']; $customer_id = $this->get_customer_id($blog_id); $email = isset($current_user->user_email) ? $current_user->user_email : get_blog_option($blog_id, 'admin_email'); try { if (!$customer_id) { $c = Stripe_Customer::create(array("email" => $email, "description" => sprintf(__('%s Pro Site - BlogID: %d', 'psts'), $current_site->site_name, $blog_id))); $this->set_customer_id($blog_id, $c->id); $customer_id = $c->id; $new = true; } else { $c = Stripe_Customer::retrieve($customer_id); $c->description = sprintf(__('%s Pro Site - BlogID: %d', 'psts'), $current_site->site_name, $blog_id); $c->email = $email; $c->save(); $new = false; } //prepare vars $discountAmt = false; if ($_POST['period'] == 1) { $paymentAmount = $psts->get_level_setting($_POST['level'], 'price_1'); if (isset($_SESSION['COUPON_CODE']) && $psts->check_coupon($_SESSION['COUPON_CODE'], $blog_id, $_POST['level'])) { $coupon_value = $psts->coupon_value($_SESSION['COUPON_CODE'], $paymentAmount); $discountAmt = $coupon_value['new_total']; $desc = $current_site->site_name . ' ' . $psts->get_level_setting($_POST['level'], 'name') . ': ' . sprintf(__('%1$s for the first month, then %2$s each month', 'psts'), $psts->format_currency('USD', $discountAmt), $psts->format_currency('USD', $paymentAmount)); } else { $desc = $current_site->site_name . ' ' . $psts->get_level_setting($_POST['level'], 'name') . ': ' . sprintf(__('%1$s %2$s each month', 'psts'), $psts->format_currency('USD', $paymentAmount), 'USD'); } } else { if ($_POST['period'] == 12) { $paymentAmount = $psts->get_level_setting($_POST['level'], 'price_12'); if (isset($_SESSION['COUPON_CODE']) && $psts->check_coupon($_SESSION['COUPON_CODE'], $blog_id, $_POST['level'])) { $coupon_value = $psts->coupon_value($_SESSION['COUPON_CODE'], $paymentAmount); $discountAmt = $coupon_value['new_total']; $desc = $current_site->site_name . ' ' . $psts->get_level_setting($_POST['level'], 'name') . ': ' . sprintf(__('%1$s for the first 12 month period, then %2$s every 12 months', 'psts'), $psts->format_currency('USD', $discountAmt), $psts->format_currency('USD', $paymentAmount)); } else { $desc = $current_site->site_name . ' ' . $psts->get_level_setting($_POST['level'], 'name') . ': ' . sprintf(__('%1$s %2$s every 12 months', 'psts'), $psts->format_currency('USD', $paymentAmount), 'USD'); } } } $desc = apply_filters('psts_pypl_checkout_desc', $desc, $_POST['period'], $_POST['level'], $paymentAmount, $discountAmt, $blog_id); //create temporary stripe coupon if ($discountAmt) { $pct = $discountAmt <= 0 ? 100 : 100 - round($discountAmt / $paymentAmount * 100); //get whole number percent $cpn = Stripe_Coupon::create(array("percent_off" => $pct, "duration" => "once", "max_redemptions" => 1)); $cp_code = $cpn->id; $initAmount = $discountAmt; } else { $cp_code = false; $initAmount = $paymentAmount; } //assign the new plan to the customer $args = array("plan" => $plan, "prorate" => true, "card" => $_POST['stripeToken']); //add coupon if set if ($cp_code) { $args["coupon"] = $cp_code; } //add trial days for new signups with expiration in the future (manually extended or from another gateway) if (is_pro_site($blog_id) && !is_pro_trial($blog_id) && ($new || get_blog_option($blog_id, 'psts_stripe_canceled'))) { $args["trial_end"] = $psts->get_expire($blog_id); } $c->updateSubscription($args); //delete the temporary coupon code if ($cp_code) { $cpn = Stripe_Coupon::retrieve($cp_code); $cpn->delete(); $psts->use_coupon($_SESSION['COUPON_CODE'], $blog_id); } if ($new || get_blog_option($blog_id, 'psts_stripe_canceled')) { $psts->log_action($blog_id, sprintf(__('User creating new subscription via CC: Subscription created (%1$s) - Customer ID: %2$s', 'psts'), $desc, $customer_id)); //$psts->extend($blog_id, $_POST['period'], 'Stripe', $_POST['level'], $paymentAmount); //let the IPN handle that $psts->record_stat($blog_id, 'signup'); $psts->email_notification($blog_id, 'success'); // Added for affiliate system link do_action('supporter_payment_processed', $blog_id, $paymentAmount, $_POST['period'], $_POST['level']); } else { $psts->log_action($blog_id, sprintf(__('User modifying subscription via CC: Plan changed to (%1$s) - %2$s', 'psts'), $desc, $customer_id)); } //display GA ecommerce in footer $psts->create_ga_ecommerce($blog_id, $_POST['period'], $initAmount, $_POST['level']); update_blog_option($blog_id, 'psts_stripe_canceled', 0); if (empty($this->complete_message)) { $this->complete_message = __('Your subscription was successful! You should be receiving an email receipt shortly.', 'psts'); } } catch (Exception $e) { $psts->errors->add('general', $e->getMessage()); } } }
public function stripe_deleteCoupon($couponid) { try { $coupon = Stripe_Coupon::retrieve($couponid); $coupon->delete(); return true; } catch (Exception $e) { return $e->getMessage(); } }