Advertisement
cw17s0n

listener

Dec 17th, 2012
214
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. function pippin_stripe_event_listener() {
  4.    
  5.     if(isset($_GET['wps-listener']) && $_GET['wps-listener'] == 'stripe') {
  6.        
  7.         global $stripe_options;
  8.        
  9.         require_once(STRIPE_BASE_DIR . '/lib/Stripe.php');
  10.  
  11.         if(isset($stripe_options['test_mode']) && $stripe_options['test_mode']) {
  12.             $secret_key = $stripe_options['test_secret_key'];
  13.         } else {
  14.             $secret_key = $stripe_options['live_secret_key'];
  15.         }
  16.        
  17.         Stripe::setApiKey($secret_key);
  18.  
  19.         // retrieve the request's body and parse it as JSON
  20.         $body = @file_get_contents('php://input');
  21.        
  22.         // grab the event information
  23.         $event_json = json_decode($body);
  24.        
  25.         // this will be used to retrieve the event from Stripe
  26.         $event_id = $event_json->id;
  27.        
  28.         if(isset($event_json->id)) {
  29.            
  30.             try {
  31.                
  32.                 // to verify this is a real event, we re-retrieve the event from Stripe
  33.                 $event = Stripe_Event::retrieve($event_id);
  34.                 $invoice = $event->data->object;
  35.                
  36.                 // successful payment
  37.                 if($event->type == 'charge.succeeded') {
  38.                     // send a payment receipt email here
  39.                    
  40.                     // retrieve the payer's information
  41.                     $customer = Stripe_Customer::retrieve($invoice->customer);
  42.                     $email = $customer->email;
  43.                    
  44.                     $amount = $invoice->amount / 100; // amount comes in as amount in cents, so we need to convert to dollars
  45.                    
  46.                     $subject = __('Payment Receipt', 'pippin_stripe');
  47.                     $headers = 'From: "' . html_entity_decode(get_bloginfo('name')) . '" <' . get_bloginfo('admin_email') . '>';
  48.                     $message = "Hello " . $customer_name . "\n\n";
  49.                     $message .= "You have successfully made a payment of " . $amount . "\n\n";
  50.                     $message .= "Thank you.";
  51.  
  52.                     wp_mail($email, $subject, $message, $headers);
  53.                 }
  54.                
  55.                 // failed payment
  56.                 if($event->type == 'charge.failed') {
  57.                     // send a failed payment notice email here
  58.                    
  59.                     // retrieve the payer's information
  60.                     $customer = Stripe_Customer::retrieve($invoice->customer);
  61.                     $email = $customer->email;
  62.                                        
  63.                     $subject = __('Failed Payment', 'pippin_stripe');
  64.                     $headers = 'From: "' . html_entity_decode(get_bloginfo('name')) . '" <' . get_bloginfo('admin_email') . '>';
  65.                     $message = "Hello " . $customer_name . "\n\n";
  66.                     $message .= "We have failed to process your payment of " . $amount . "\n\n";
  67.                     $message .= "Please get in touch with support.\n\n";
  68.                     $message .= "Thank you.";
  69.  
  70.                     wp_mail($email, $subject, $message, $headers);
  71.                 }
  72.                
  73.             } catch (Exception $e) {
  74.                 // something failed, perhaps log a notice or email the site admin
  75.             }
  76.         }
  77.     }
  78. }
  79. add_action('init', 'pippin_stripe_event_listener');
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement