function leaky_paywall_pay_with_stripe($level, $level_id) { $results = ''; $settings = get_leaky_paywall_settings(); $currency = apply_filters('leaky_paywall_stripe_currency', $settings['leaky_paywall_currency']); if (in_array(strtoupper($currency), array('BIF', 'DJF', 'JPY', 'KRW', 'PYG', 'VND', 'XAF', 'XPF', 'CLP', 'GNF', 'KMF', 'MGA', 'RWF', 'VUV', 'XOF'))) { //Zero-Decimal Currencies //https://support.stripe.com/questions/which-zero-decimal-currencies-does-stripe-support $stripe_price = number_format($level['price'], '0', '', ''); } else { $stripe_price = number_format($level['price'], '2', '', ''); //no decimals } $publishable_key = 'on' === $settings['test_mode'] ? $settings['test_publishable_key'] : $settings['live_publishable_key']; if (!empty($level['recurring']) && 'on' === $level['recurring']) { try { $stripe_plan = false; $time = time(); if (!empty($level['plan_id'])) { //We need to verify that the plan_id matches the level details, otherwise we need to update it try { $stripe_plan = Stripe_Plan::retrieve($level['plan_id']); } catch (Exception $e) { $stripe_plan = false; } } if (!is_object($stripe_plan) || ($stripe_price != $stripe_plan->amount || $level['interval'] != $stripe_plan->interval || $level['interval_count'] != $stripe_plan->interval_count)) { $args = array('amount' => esc_js($stripe_price), 'interval' => esc_js($level['interval']), 'interval_count' => esc_js($level['interval_count']), 'name' => esc_js($level['label']) . ' ' . $time, 'currency' => esc_js($currency), 'id' => sanitize_title_with_dashes($level['label']) . '-' . $time); $stripe_plan = Stripe_Plan::create($args); $settings['levels'][$level_id]['plan_id'] = $stripe_plan->id; update_leaky_paywall_settings($settings); } $results .= '<form action="' . esc_url(add_query_arg('issuem-leaky-paywall-stripe-return', '1', get_page_link($settings['page_for_subscription']))) . '" method="post"> <input type="hidden" name="custom" value="' . esc_js($level_id) . '" /> <script src="https://checkout.stripe.com/v2/checkout.js" class="stripe-button" data-key="' . esc_js($publishable_key) . '" data-plan="' . esc_js($stripe_plan->id) . '" data-currency="' . esc_js($currency) . '" data-description="' . esc_js($level['label']) . '"> </script> ' . apply_filters('leaky_paywall_pay_with_stripe_recurring_payment_form_after_script', '') . ' </form>'; } catch (Exception $e) { $results = '<h1>' . sprintf(__('Error processing request: %s', 'issuem-leaky-paywall'), $e->getMessage()) . '</h1>'; } } else { $results .= '<form action="' . esc_url(add_query_arg('issuem-leaky-paywall-stripe-return', '1', get_page_link($settings['page_for_subscription']))) . '" method="post"> <input type="hidden" name="custom" value="' . esc_js($level_id) . '" /> <script src="https://checkout.stripe.com/v2/checkout.js" class="stripe-button" data-key="' . esc_js($publishable_key) . '" data-amount="' . esc_js($stripe_price) . '" data-currency="' . esc_js($currency) . '" data-description="' . esc_js($level['label']) . '"> </script> ' . apply_filters('leaky_paywall_pay_with_stripe_non_recurring_payment_form_after_script', '') . ' </form>'; } return '<div class="leaky-paywall-stripe-button leaky-paywall-payment-button">' . $results . '</div>'; }
/** * Gets the stripe plan associated with the level, and creates one if it doesn't exist * * @since 4.0.0 */ function leaky_paywall_get_stripe_plan($level, $level_id, $plan_args) { $settings = get_leaky_paywall_settings(); $stripe_plan = false; $time = time(); Stripe::setApiKey($plan_args['secret_key']); if (!empty($level['plan_id'])) { //We need to verify that the plan_id matches the level details, otherwise we need to update it try { $stripe_plan = Stripe_Plan::retrieve($level['plan_id']); } catch (Exception $e) { $stripe_plan = false; } } if (!is_object($stripe_plan) || ($plan_args['stripe_price'] != $stripe_plan->amount || $level['interval'] != $stripe_plan->interval || $level['interval_count'] != $stripe_plan->interval_count)) { $args = array('amount' => esc_js($plan_args['stripe_price']), 'interval' => esc_js($level['interval']), 'interval_count' => esc_js($level['interval_count']), 'name' => esc_js($level['label']) . ' ' . $time, 'currency' => esc_js($plan_args['currency']), 'id' => sanitize_title_with_dashes($level['label']) . '-' . $time); $stripe_plan = Stripe_Plan::create($args); $settings['levels'][$level_id]['plan_id'] = $stripe_plan->id; update_leaky_paywall_settings($settings); } return $stripe_plan; }