Example #1
0
    /**
     * Output the Stripe's payment dialog.
     * Step 2 in the payment process.
     */
    public function pay_page()
    {
        $payment_id = absint(get_query_var('edu-pay'));
        if (!$payment_id) {
            return;
        }
        $user = wp_get_current_user();
        if (0 == $user->ID) {
            return;
        }
        $payment = edr_get_payment($payment_id);
        if (!$payment->ID || $user->ID != $payment->user_id) {
            // The payment must exist and it must be associated with the current user.
            return;
        }
        if ('course' == $payment->payment_type) {
            $post = get_post($payment->course_id);
        } elseif ('membership' == $payment->payment_type) {
            $post = get_post($payment->object_id);
        }
        if (!$post) {
            return;
        }
        ?>
		<p id="ib-edu-payment-processing-msg">
			<?php 
        _e('The payment is getting processed...', 'ibeducator');
        ?>
		</p>
		<script src="https://checkout.stripe.com/checkout.js"></script>
		<script>
		(function($) {
			var handler = StripeCheckout.configure({
				key: <?php 
        echo json_encode($this->get_option('publishable_key'));
        ?>
,
				image: '',
				email: <?php 
        echo json_encode($user->user_email);
        ?>
,
				token: function(token) {
					$.ajax({
						type: 'POST',
						cache: false,
						url: <?php 
        echo json_encode(ib_edu_request_url('stripe_token'));
        ?>
,
						data: {
							payment_id: <?php 
        echo intval($payment->ID);
        ?>
,
							token: token.id,
							_wpnonce: <?php 
        echo json_encode(wp_create_nonce('ib_educator_stripe_token'));
        ?>
						},
						success: function(response) {
							if (response === '1') {
								$('#ib-edu-payment-processing-msg').text(<?php 
        echo json_encode(__('Redirecting to the payment summary page...', 'ibeducator'));
        ?>
);
								var redirectTo = <?php 
        echo json_encode(ib_edu_get_endpoint_url('edu-thankyou', $payment->ID, get_permalink(ib_edu_page_id('payment'))));
        ?>
;
								document.location = redirectTo;
							}
						}
					});
				}
			});

			handler.open({
				name: <?php 
        echo json_encode(esc_html($post->post_title));
        ?>
,
				description: <?php 
        echo json_encode(ib_edu_format_price($payment->amount, false, false));
        ?>
,
				currency: <?php 
        echo json_encode(ib_edu_get_currency());
        ?>
,
				amount: <?php 
        echo absint($payment->amount * 100);
        ?>
			});

			$(window).on('popstate', function() {
				handler.close();
			});
		})(jQuery);
		</script>
		<?php 
    }
Example #2
0
    /**
     * Output the form to the step 2 (pay page) of the payment page.
     */
    public function pay_page()
    {
        $action_url = $this->get_option('test') ? $this->test_url : $this->live_url;
        $payment_id = absint(get_query_var('edu-pay'));
        if (!$payment_id) {
            return;
        }
        $user_id = get_current_user_id();
        if (!$user_id) {
            return;
        }
        $payment = edr_get_payment($payment_id);
        // The payment must exist in the database
        // and it must belong to the current user.
        if (!$payment->ID || $user_id != $payment->user_id) {
            return;
        }
        if ('course' == $payment->payment_type) {
            $post = get_post($payment->course_id);
        } elseif ('membership' == $payment->payment_type) {
            $post = get_post($payment->object_id);
        }
        if (!$post) {
            return;
        }
        $amount = $payment->amount - $payment->tax;
        $return_url = '';
        $payment_page_id = ib_edu_page_id('payment');
        if ($payment_page_id) {
            $return_url = ib_edu_get_endpoint_url('edu-thankyou', $payment->ID ? $payment->ID : '', get_permalink($payment_page_id));
        }
        echo '<form id="ib-edu-paypal-form" action="' . esc_url($action_url) . '" method="post">';
        echo '<input type="hidden" name="cmd" value="_xclick">';
        echo '<input type="hidden" name="charset" value="utf-8">';
        echo '<input type="hidden" name="business" value="' . esc_attr($this->get_option('business_email')) . '">';
        echo '<input type="hidden" name="return" value="' . esc_url($return_url) . '">';
        echo '<input type="hidden" name="notify_url" value="' . esc_url(ib_edu_request_url('paypalipn')) . '">';
        echo '<input type="hidden" name="currency_code" value="' . esc_attr(ib_edu_get_currency()) . '">';
        echo '<input type="hidden" name="item_name" value="' . esc_attr($post->post_title) . '">';
        echo '<input type="hidden" name="item_number" value="' . absint($payment->ID) . '">';
        if ($payment->tax) {
            echo '<input type="hidden" name="tax" value="' . (double) $payment->tax . '">';
        }
        echo '<input type="hidden" name="amount" value="' . (double) $amount . '">';
        echo '<div id="paypal-form-buttons"><button type="submit">' . __('Continue', 'ibeducator') . '</button></div>';
        echo '</form>';
        echo '<div id="paypal-redirect-notice" style="display: none;">' . __('Redirecting to PayPal...', 'ibeducator') . '</div>';
        echo '<script>(function() {
			function goToPayPal() {
				document.getElementById("paypal-form-buttons").style.display = "none";
				document.getElementById("paypal-redirect-notice").style.display = "block";
				document.getElementById("ib-edu-paypal-form").submit();
			}

			if ( typeof jQuery === "undefined" ) {
				setTimeout(goToPayPal, 500);
			} else {
				jQuery(document).on("ready", function() {
					goToPayPal();
				});
			}
		})();</script>';
    }