Changeset 6866
- Timestamp:
- 03/12/2018 10:21:07 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpressfoundation.org/public_html/content/plugins/wpf-stripe/wpf-stripe.php
r6865 r6866 11 11 namespace WordPress_Foundation\Stripe; 12 12 use Exception; 13 use Stripe\Stripe, Stripe\Customer, Stripe\Charge; 13 14 14 15 defined( 'WPINC' ) || die(); … … 94 95 </div> 95 96 97 <p>Or make a one-time donation in the amount of your choosing:</p> 98 99 <div class="one-time-donation"> 100 <form action="" method="POST"> 101 <label>$ 102 <input type="number" class="amount" name="amount" min="1" /> 103 </label> 104 105 <script 106 src="https://checkout.stripe.com/checkout.js" 107 class="stripe-button" 108 data-key="<?php echo esc_html( STRIPE_PUBLIC_KEY ); ?>" 109 data-image="<?php echo esc_url( $image ); ?>" 110 data-name="WordPress Foundation" 111 data-description="One-Time Donation" 112 data-label="Give once" 113 data-zip-code="true" 114 data-billing-address="true"> 115 </script> 116 </form> 117 </div> 118 96 119 <?php 97 120 … … 100 123 add_shortcode( 'wpfstripe', __NAMESPACE__ . '\render_donate_shortcode' ); 101 124 102 add_action( 'init', __NAMESPACE__ . '\check_subscribe' );103 function check_subscribe() {104 if ( !empty( $_POST ) && isset( $_POST['stripeToken'] ) ) {105 process_payments();106 }107 }108 109 125 function process_payments() { 110 global $wpf_success_url, $wpf_fail_url; 111 112 // no token, nothing to do 113 if ( empty( $_POST['stripeToken'] ) ) { 126 if ( ! isset( $_POST['stripeEmail'], $_POST['stripeToken'] ) ) { 114 127 return; 115 128 } 116 129 117 require_once('stripe-php/init.php'); 118 119 \Stripe\Stripe::setApiKey( STRIPE_SECRET_KEY ); 120 121 try 122 { 123 $customer = \Stripe\Customer::create(array( 124 'email' => $_POST['stripeEmail'], 125 'source' => $_POST['stripeToken'], 126 'plan' => $_POST['wpf_plan'], 127 )); 128 129 wp_redirect( $wpf_success_url ); 130 require_once( __DIR__ . '/stripe-php/init.php' ); 131 Stripe::setApiKey( STRIPE_SECRET_KEY ); 132 133 try { 134 if ( isset( $_POST['wpf_plan'] ) ) { 135 $params = array( 136 'source' => $_POST['stripeToken'], 137 'email' => $_POST['stripeEmail'], 138 'plan' => $_POST['wpf_plan'], // Subscribe the new Customer to the plan at the same time. 139 ); 140 141 Customer::create( $params ); 142 } elseif ( isset( $_POST['amount'] ) ) { 143 $params = array( 144 'source' => $_POST['stripeToken'], 145 'receipt_email' => $_POST['stripeEmail'], 146 'amount' => $_POST['amount'] * 100, // Convert dollars to pennies. 147 'currency' => 'USD', 148 'description' => 'WordPress Foundation donation', 149 ); 150 151 Charge::create( $params ); 152 } else { 153 wp_die( 'Unsupported form action' ); 154 } 155 156 $redirect_url = home_url( 'successful-donation/' ); 157 } catch ( Exception $exception ) { 158 log( $exception->getMessage(), $params ); 159 $redirect_url = home_url( 'unsuccessful-donation/'); 160 } finally { 161 wp_safe_redirect( $redirect_url ); 130 162 exit; 131 163 } 132 catch(Exception $e) 133 { 134 wp_redirect( $wpf_fail_url ); 135 exit; 136 } 137 } 164 } 165 add_action( 'init', __NAMESPACE__ . '\process_payments' ); 138 166 139 167 /** … … 152 180 .stripe-button-el { 153 181 text-transform: none; 154 float: left; 155 margin: 8px; 156 } 182 } 183 184 .subscription-plans .stripe-button-el { 185 margin: 0 12px 12px 0; 186 } 187 188 @media screen and ( min-width: 415px ) { 189 .subscription-plans { 190 display: flex; 191 flex-wrap: wrap; 192 justify-content: space-around; 193 } 194 } 195 196 .one-time-donation { 197 margin-top: .5em; 198 } 199 200 .one-time-donation form { 201 margin-top: .5em; 202 text-align: center; 203 } 204 205 .amount { 206 position: relative; 207 top: 3px; 208 width: 5em; 209 margin-right: .5em; 210 padding: 1px 5px; 211 text-align: center; 212 } 157 213 </style> 158 214 … … 160 216 } 161 217 add_action( 'wp_head', __NAMESPACE__ . '\custom_styles' ); 218 219 /** 220 * Log an error message. 221 * 222 * @param string $error_message 223 * @param array $data 224 */ 225 function log( $error_message, $data ) { 226 trigger_error( sprintf( 227 '%s error: %s. Data: %s', 228 __FUNCTION__, 229 $error_message, 230 wp_json_encode( $data ) 231 ) ); 232 }
Note: See TracChangeset
for help on using the changeset viewer.