· 9 years ago · Dec 01, 2016, 09:58 AM
1<?php
2 function pippin_stripe_payment_form($atts, $content = null) {
3
4 extract( shortcode_atts( array(
5 'amount' => '',
6 'myplan' =>''
7 ), $atts ) );
8
9 global $stripe_options;
10
11 if(isset($_GET['payment']) && $_GET['payment'] == 'paid') {
12 echo '<p class="success">' . __('Thank you for your payment.', 'pippin_stripe') . '</p>';
13 } else { ?>
14<h2><?php _e('Submit a payment of $10', 'pippin_stripe'); ?></h2>
15<form action="" method="POST" id="stripe-payment-form">
16 <div class="sk-form-row">
17 <label><?php _e('Email', 'pippin_stripe'); ?></label>
18 <input type="text" size="20" class="email" name="email"/>
19 </div>
20 <div class="sk-form-row">
21 <label><?php _e('Card Number', 'pippin_stripe'); ?></label>
22 <input type="text" size="20" autocomplete="off" class="card-number"/>
23 </div>
24 <div class="sk-form-row">
25 <label><?php _e('CVC', 'pippin_stripe'); ?></label>
26 <input type="text" size="4" autocomplete="off" class="card-cvc"/>
27 </div>
28 <div class="sk-form-row">
29 <label><?php _e('Expiration (MM/YYYY)', 'pippin_stripe'); ?></label>
30 <input type="text" size="2" class="card-expiry-month"/>
31 <span> / </span>
32 <input type="text" size="4" class="card-expiry-year"/>
33 </div>
34 <?php if(isset($stripe_options['recurring'])) { ?>
35 <div class="sk-form-row">
36 <label><?php _e('Payment Type:', 'pippin_stripe'); ?></label>
37 <input type="radio" name="recurring" class="stripe-recurring" value="no" checked="checked"/><span><?php _e('One time payment', 'pippin_stripe'); ?></span>
38 <input type="radio" name="recurring" class="stripe-recurring" value="yes"/><span><?php _e('Recurring monthly payment', 'pippin_stripe'); ?></span>
39 </div>
40 <div class="sk-form-row" id="stripe-plans" style="display:none;">
41 <label><?php _e('Choose Your Plan', 'pippin_stripe'); ?></label>
42 <select name="plan_id" id="stripe_plan_id">
43 <?php
44 $plans = pippin_get_stripe_plans();
45 if($plans) {
46 foreach($plans as $id => $plan) {
47 echo '<option value="' . $id . '">' . $plan . '</option>';
48 }
49 }
50 ?>
51 </select>
52 </div>
53 <div class="form-row">
54 <label><?php _e('Discount Code', 'pippin_stripe'); ?></label>
55 <input type="text" size="20" class="discount" name="discount"/>
56 </div>
57 <?php } ?>
58 <input type="hidden" name="action" value="stripe"/>
59 <input type="hidden" name="redirect" value="<?php echo get_permalink(); ?>"/>
60 <input type="hidden" name="amount" value="<?php echo base64_encode($amount); ?>"/>
61 <input type="hidden" name="stripe_nonce" value="<?php echo wp_create_nonce('stripe-nonce'); ?>"/>
62 <button type="submit" id="stripe-submit"><?php _e('Submit Payment', 'pippin_stripe'); ?></button>
63</form>
64 <div class="payment-errors"></div>
65 <?php
66 }
67}
68add_shortcode('payment_form', 'pippin_stripe_payment_form');
69
70<?php
71
72function pippin_get_stripe_plans() {
73
74global $stripe_options;
75
76// load the stripe libraries
77require_once (STRIPE_BASE_DIR . '/stripe-php/init.php');
78
79// check if we are using test mode
80if(isset($stripe_options['test_mode']) && $stripe_options['test_mode']) {
81 $secret_key = $stripe_options['test_secret_key'];
82} else {
83$secret_key = $stripe_options['live_secret_key'];
84}
85
86StripeStripe::setApiKey($secret_key);
87// retrieve all plans from stripe
88$plans_data = StripePlan::all();
89// setup a blank array
90$plans = array();
91if($plans_data) {
92foreach($plans_data['data'] as $plan) {
93 // store the plan ID as the array key and the plan name as the value
94 $plans[$plan['id']] = $plan['name'];
95}
96}
97return $plans;
98}