示例#1
0
/**
 * Add the Stripe subscribe popup button to the subscribe cards. 
 *
 * @since 4.0.0
 */
function leaky_paywall_stripe_checkout_button($level, $level_id)
{
    $results = '';
    $settings = get_leaky_paywall_settings();
    $currency = apply_filters('leaky_paywall_stripe_currency', $settings['leaky_paywall_currency']);
    // @todo: make this a function so we can use it on the credit card form too
    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'];
    $secret_key = 'on' === $settings['test_mode'] ? $settings['test_secret_key'] : $settings['live_secret_key'];
    if (!empty($level['recurring']) && 'on' === $level['recurring']) {
        try {
            $plan_args = array('stripe_price' => $stripe_price, 'currency' => $currency, 'secret_key' => $secret_key);
            $stripe_plan = leaky_paywall_get_stripe_plan($level, $level_id, $plan_args);
            $results .= '<form action="' . esc_url(add_query_arg('leaky-paywall-confirm', 'stripe_checkout', 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-label="' . apply_filters('leaky_paywall_stripe_button_label', 'Subscribe') . '" 
								  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('leaky-paywall-confirm', 'stripe_checkout', 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-label="' . apply_filters('leaky_paywall_stripe_button_label', 'Subscribe') . '" 
							  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>';
}
    /**
     * Add credit card fields
     *
     * @since 4.0.0
     */
    public function fields()
    {
        $level_id = esc_html($_GET['level_id']);
        $level = get_leaky_paywall_subscription_level($level_id);
        if ($level['price'] == 0) {
            return;
        }
        $settings = get_leaky_paywall_settings();
        $plan_args = array('stripe_price' => number_format($level['price'], 2, '', ''), 'currency' => $settings['leaky_paywall_currency'], 'secret_key' => $this->secret_key);
        if ($level['subscription_length_type'] == 'limited') {
            $stripe_plan = leaky_paywall_get_stripe_plan($level, $level_id, $plan_args);
        }
        ob_start();
        ?>

			<input type="hidden" name="plan_id" value="<?php 
        echo $stripe_plan->id;
        ?>
"/>

			<script type="text/javascript">

			var leaky_paywall_script_options;
			var leaky_paywall_processing;
			var leaky_paywall_stripe_processing = false;

			  // This identifies your website in the createToken call below
			  Stripe.setPublishableKey('<?php 
        echo $this->publishable_key;
        ?>
');

			  function stripeResponseHandler(status, response) {

			  	if (response.error) {
			  		// re-enable th submit button
			  		jQuery('#leaky-paywall-payment-form #leaky-paywall-submit').attr("disabled", false );

			  		// jQuery('#leaky-paywall-registration-form').unblock();
			  		jQuery('#leaky-paywall-submit').before('<div class="leaky-paywall-message error"><p class="leaky-paywall-error"><span>' + response.error.message + '</span></p></div>' );

			  		leaky_paywall_stripe_processing = false;
			  		leaky_paywall_processing = false;

			  	} else {

			  		var form$ = jQuery('#leaky-paywall-payment-form');
			  		var token = response['id'];

			  		form$.append('<input type="hidden" name="stripeToken" value="' + token + '" />');

			  		form$.get(0).submit();

			  	}
			  }

			  jQuery(document).ready(function($) {

			  	$('#leaky-paywall-payment-form').on('submit', function(e) {
 
			  		if ( ! leaky_paywall_stripe_processing ) {

			  			leaky_paywall_stripe_processing = true;

			  			// get the price
			  			$('input[name="stripe_price"]').val();

			  			// disabl the submit button to prevent repeated clicks
			  			$('#leaky-paywall-payment-form #leaky-paywall-submit').attr('disabled', 'disabled' );

			  			// create Stripe token
			  			Stripe.createToken({
			  				number: $('.card-num').val(),
			  				name: $('.card-name').val(),
			  				cvc: $('.cvc').val(),
			  				exp_month: $('.exp-month').val(),
			  				exp_year: $('.exp-year').val(),
			  				address_zip: $('.card-zip').val(),
			  			}, stripeResponseHandler);

			  			return false;
			  		}
			  	});
			  });


			</script>

		<?php 
        leaky_paywall_card_form();
        return ob_get_clean();
    }