/** * Gets specified invoice * @param $invoice_id * @return mixed */ public function get($invoice_id) { try { $ch = Stripe_Invoice::retrieve($invoice_id); return $ch; } catch (Exception $e) { $this->error = TRUE; $this->message = $e->getMessage(); $this->code = $e->getCode(); return FALSE; } }
/** * Print an invoice */ public function printAction($id) { \Stripe::setApiKey($this->container->getParameter('avro_stripe.secret_key')); $invoice = \Stripe_Invoice::retrieve($id); if (!$invoice) { throw new NotFoundHttpException('Invoice not found'); } $user = $this->container->get('fos_user.user_manager')->findUserBy(array('stripeCustomerId' => $invoice->customer)); if (!$user) { throw new NotFoundHttpException('User not found'); } $html = $this->container->get('templating')->render('AvroStripeBundle:Invoice:print.html.twig', array('invoice' => $invoice, 'user' => $user)); return new Response($this->container->get('knp_snappy.pdf')->getOutputFromHtml($html), 200, array('Content-Type' => 'application/pdf', 'Content-Disposition' => 'attachment; filename="' . $invoice->id . '.pdf"')); }
function getOldOrderFromInvoiceEvent($event) { //pause here to give PMPro a chance to finish checkout sleep(PMPRO_STRIPE_WEBHOOK_DELAY); global $wpdb; $customer_id = $event->data->object->customer; // no customer passed? we can't cross reference if (empty($customer_id)) { return false; } // okay, add an invoice. first lookup the user_id from the subscription id passed $old_order_id = $wpdb->get_var("SELECT id FROM {$wpdb->pmpro_membership_orders} WHERE subscription_transaction_id = '" . $customer_id . "' AND gateway = 'stripe' ORDER BY timestamp DESC LIMIT 1"); // since v1.8, PMPro may store the Stripe subscription_id (sub_XXXX) instead of the Stripe customer_id (cus_XXXX) // so that last query may turn up an empty result if (empty($old_order_id)) { // let's look up the Stripe subscription_id instead // unfortunately, the subscription_id is not included in the JSON data from the Stripe event // so, we must look up the subscription_id from the invoice_id, which IS included in the JSON data from the Stripe event $invoice_id = $event->data->object->invoice; $invoice = Stripe_Invoice::retrieve($invoice_id); $subscription_id = $invoice->subscription; $old_order_id = $wpdb->get_var("SELECT id FROM {$wpdb->pmpro_membership_orders} WHERE subscription_transaction_id = '" . $subscription_id . "' AND gateway = 'stripe' ORDER BY timestamp DESC LIMIT 1"); } $old_order = new MemberOrder($old_order_id); if (!empty($old_order->id)) { return $old_order; } else { return false; } }
if ($gatewaytestmode == "on") { Stripe::setApiKey($gateway['private_test_key']); } else { Stripe::setApiKey($gateway['private_live_key']); } $body = @file_get_contents('php://input'); $event_json = json_decode($body); $event_id = $event_json->id; try { $event = Stripe_Event::retrieve($event_id); if ($event->type == 'charge.succeeded') { // Pull invoice ID from Stripe description if ($event->data->object->invoice != "") { // This is an invoice/subscription payment, get the WHMCS invoice ID $invoice_id = $event->data->object->invoice; $retrieved_invoice = Stripe_Invoice::retrieve($invoice_id)->lines->all(array('count' => 1, 'offset' => 0)); $description_invoice = $retrieved_invoice["data"][0]["plan"]["name"]; $description = $description_invoice; } else { // This is a one time payment $description = $event->data->object->description; } // Get the invoice ID from the transaction $start = strpos($description, "#") + strlen("#"); $end = strpos($description, " ", $start); $invoiceid = substr($description, $start, $end - $start); $transid = $event->data->object->id; $amount_cents = $event->data->object->amount; $amount = $amount_cents / 100; $fee_cents = floatval($event->data->object->fee); $fee = $fee_cents / 100;
/** * Refund a payment or invoice * @param object &$order Related PMPro order object. * @param string $transaction_id Payment or invoice id to void. * @return bool True or false if the refund worked. */ function refund(&$order, $transaction_id = NULL) { //default to using the payment id from the order if (empty($transaction_id) && !empty($order->payment_transaction_id)) { $transaction_id = $order->payment_transaction_id; } //need a transaction id if (empty($transaction_id)) { return false; } //if an invoice ID is passed, get the charge/payment id if (strpos($transaction_id, "in_") !== false) { $invoice = Stripe_Invoice::retrieve($transaction_id); if (!empty($invoice) && !empty($invoice->payment)) { $transaction_id = $invoice->payment; } } //get the charge $charge = Stripe_Charge::retrieve($transaction_id); //can't find the charge? if (empty($charge)) { $order->status = "error"; $order->errorcode = ""; $order->error = ""; $order->shorterror = ""; return false; } //attempt refund try { $refund = $charge->refund(); } catch (Exception $e) { //$order->status = "error"; $order->errorcode = true; $order->error = __("Error: ", "pmpro") . $e->getMessage(); $order->shorterror = $order->error; return false; } if ($refund->status == "succeeded") { $order->status = "refunded"; $order->saveOrder(); return true; } else { $order->status = "error"; $order->errorcode = true; $order->error = sprintf(__("Error: Unkown error while refunding charge #%s", "pmpro"), $transaction_id); $order->shorterror = $order->error; return false; } }
<?php require_once './config.php'; if ($_SERVER['REQUEST_METHOD'] == 'POST') { $postdata = file_get_contents("php://input"); $event = json_decode($postdata); if ($event->type == 'invoice.payment_succeeded') { $customer_id = $event->data->object->customer; $customer = Stripe_Customer::retrieve($customer_id); $invoice = Stripe_Invoice::retrieve($event->data->object->id); // This is where we'd normally e-mail the invoice, but we'll just write out the invoice to a file instead. $from = "From: Oscar Wilde"; $to = "To: " . $customer->email; $subject = "Subject: You have made a payment for another month of Wilde quotes"; $body = "You have made a new payment for \$" . $invoice->total / 100.0 . ":\n\n"; foreach ($invoice->lines->data as &$line) { if ($line->type == 'subscription') { $body .= "Subscription - " . $line->plan->name . ": " . $line->amount . "\n"; } else { if ($line->type == 'invoiceitem') { $body .= "Additional -" . $line->description . ": " . $line->amount; } } } $email_file = fopen('/public/' . $customer->id . "-" . $invoice->date, 'a'); $email = $from . "\n" . $to . "\n" . $subject . "\n" . $body; fwrite($email_file, $email); } }
public function stripe_getInvoice($invoice) { try { $inv = Stripe_Invoice::retrieve($invoice); return $inv; } catch (Exception $e) { return false; } }
/** * Get an invoice * @param string $invoice_id * @return Stripe_Invoice|false */ public function getInvoice($invoice_id = '') { try { return Stripe_Invoice::retrieve($invoice_id, $this->access_token); } catch (Exception $ex) { $this->log($ex); return false; } }
<?php require 'lib/Stripe.php'; $cus_id = $_POST['cus_id']; $sub_id = $_POST['sub_id']; $stripeKey = "sk_test_9b496fHXP5VKKsecxIzFxMld"; date_default_timezone_set('UTC'); Stripe::setApiKey($stripeKey); //$invoiceID = 'in_16jRNHGoIOM2Af5cmztSyj57'; $invoiceID = Stripe_Invoice::all(array("customer" => $cus_id))->data[0]->id; $invoice = Stripe_Invoice::retrieve($invoiceID); $customer = Stripe_Customer::retrieve($cus_id); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Invoice</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="description" content="Customer Invoice"> <meta name="author" content="5marks"> <link rel="stylesheet" href="css/bootstrap-min.css"> <style> .invoice-head td { padding: 0 8px; } .container { padding-top:30px; } .invoice-body{
public function external_hook($hook) { switch ($hook) { case 'event_ipn': require_once 'includes/plugin_paymethod_stripe/stripe-php/lib/Stripe.php'; $stripe = array("secret_key" => module_config::c('payment_method_stripe_secret_key'), "publishable_key" => module_config::c('payment_method_stripe_publishable_key')); Stripe::setApiKey($stripe['secret_key']); $body = @file_get_contents('php://input'); $event_json = json_decode($body); ob_start(); // echo "INPUT: <br>\n"; // print_r($body); // echo "<br><br>\n"; echo "UCM STRIPE DEBUG:<br><br>JSON: <br>\n"; print_r($event_json); echo "<br><br>\n"; $event_id = $event_json->id; try { $event = Stripe_Event::retrieve($event_id); // This will send receipts on succesful invoices if ($event->type == 'charge.succeeded' && $event->data->object->invoice) { $paid_amount = $event->data->object->amount / 100; // get the invoice. $invoice = Stripe_Invoice::retrieve($event->data->object->invoice); echo "INVOICE: <br>\n"; print_r($invoice); echo "<br><br>\n"; if ($invoice && $invoice->subscription && $invoice->paid) { // this payment was for a subscription! which one though? $customer = Stripe_Customer::retrieve($invoice->customer); echo "CUSTOMER: <br>\n"; print_r($customer); echo "<br><br>\n"; $subscription = $customer->subscriptions->retrieve($invoice->subscription); echo "SUBSCRIPTION: <br>\n"; print_r($subscription); echo "<br><br>\n"; // now we have the Customer and Subscription we can look through our invoice_payment_subscription table for those values. /*update_insert('invoice_payment_subscription_id',$invoice_payment_subscription_id,'invoice_payment_subscription',array( 'status' => _INVOICE_SUBSCRIPTION_ACTIVE, 'date_start' => date('Y-m-d'), // we also have to store the stripe details here so we can easily search for them later on. 'stripe_customer' => $stripe_customer->id, 'stripe_subscription' => $stripe_subscription->id, ));*/ $invoice_payment_subscription = get_single('invoice_payment_subscription', array('stripe_customer', 'stripe_subscription'), array($customer->id, $subscription->id)); if ($invoice_payment_subscription) { // FIND THE linked invoice_payment for this original invoice payment subscription, this allows us to perform the same creatE_new_invoice as paypal below: $invoice_payment_subscription_id = $invoice_payment_subscription['invoice_payment_subscription_id']; $invoice_payment = get_single('invoice_payment', 'invoice_payment_subscription_id', $invoice_payment_subscription_id); if ($invoice_payment) { $payment_id = $invoice_payment['invoice_payment_id']; $invoice_id = $invoice_payment['invoice_id']; // we have a subscription payment. woo! // this gets a bit tricky, we have to work out if the invoice has been generated for this subscription yet. // if this invoice hasn't been generated yet then we have to generate it. // pass this back to the invoice class so we can reuse this feature in the future. $data = module_invoice::create_new_invoice_for_subscription_payment($invoice_id, $payment_id, $invoice_payment_subscription_id); if ($data && $data['invoice_id'] && $data['invoice_payment_id']) { $next_time = time(); $next_time = strtotime('+' . abs((int) $invoice_payment_subscription['days']) . ' days', $next_time); $next_time = strtotime('+' . abs((int) $invoice_payment_subscription['months']) . ' months', $next_time); $next_time = strtotime('+' . abs((int) $invoice_payment_subscription['years']) . ' years', $next_time); update_insert('invoice_payment_subscription_id', $invoice_payment_subscription_id, 'invoice_payment_subscription', array('date_last_pay' => date('Y-m-d'), 'date_next' => date('Y-m-d', $next_time))); update_insert("invoice_payment_id", $data['invoice_payment_id'], "invoice_payment", array('date_paid' => date('Y-m-d'), 'amount' => $paid_amount, 'method' => 'Stripe (Subscription)', 'invoice_payment_subscription_id' => $invoice_payment_subscription_id)); module_paymethod_stripe::add_payment_data($data['invoice_payment_id'], 'log', "Payment Received via Webhook: " . var_export(array('event.type' => $event->type, 'invoice.id' => $invoice->id, 'subscription.id' => $subscription->id, 'customer.id' => $customer->id, '$invoice_payment_subscription_id' => $invoice_payment_subscription_id, '$invoice_payment_id' => $payment_id), true)); module_invoice::save_invoice($data['invoice_id'], array()); echo "Successful Subscription Payment For Invoice " . $data['invoice_id']; } else { send_error("Stripe Webhook Subscription Error (failed to generate new invoice!) " . var_export($data, true)); } } else { echo 'Failed to find matching invoice payment in db'; } } else { echo 'Failed to find matching subscription payment in db'; } } } } catch (Exception $e) { $body = $e->getJsonBody(); $err = $body['error']; $error = "Sorry: Webhook failed. <br><br>\n\n"; $error .= $err['message']; $error .= "\n\n\n" . var_export($e, true); echo $error; } $debug = ob_get_clean(); //mail('*****@*****.**','Stripe Webhook debug',$debug); if (module_config::c('stripe_payment_debug', 0)) { echo $debug; } echo "Thanks! (set stripe_payment_debug to 1 in UCM to see more data here)"; exit; break; case 'pay_subscription': $invoice_id = isset($_REQUEST['invoice_id']) ? $_REQUEST['invoice_id'] : false; $invoice_payment_id = isset($_REQUEST['invoice_payment_id']) ? $_REQUEST['invoice_payment_id'] : false; $invoice_payment_subscription_id = isset($_REQUEST['invoice_payment_subscription_id']) ? $_REQUEST['invoice_payment_subscription_id'] : false; $stripe_plan_id = isset($_REQUEST['stripe_plan_id']) ? $_REQUEST['stripe_plan_id'] : false; $user_id = isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : false; if ($invoice_id && $invoice_payment_id && $stripe_plan_id && $invoice_payment_subscription_id && $user_id && isset($_POST['stripeToken'])) { $user_data = module_user::get_user($user_id); $email = isset($_REQUEST['stripeEmail']) && strlen($_REQUEST['stripeEmail']) ? $_REQUEST['stripeEmail'] : $user_data['email']; if (!$email || !strpos($email, '@')) { die('Please ensure your user account has a valid email address before paying with stripe'); } $invoice_payment = get_single('invoice_payment', 'invoice_payment_id', $invoice_payment_id); $invoice_payment_subscription = get_single('invoice_payment_subscription', 'invoice_payment_subscription_id', $invoice_payment_subscription_id); if (!$invoice_payment || !$invoice_payment_subscription || $invoice_payment['invoice_id'] != $invoice_id || $invoice_payment['invoice_payment_subscription_id'] != $invoice_payment_subscription_id) { die('Invalid invoice payment subscription id'); } $invoice_payment_data = module_invoice::get_invoice_payment($invoice_payment_id); $invoice_data = module_invoice::get_invoice($invoice_id); if ($invoice_payment_data && $invoice_data && $invoice_id == $invoice_data['invoice_id'] && $invoice_payment_data['invoice_id'] == $invoice_data['invoice_id']) { $currency = module_config::get_currency($invoice_payment_data['currency_id']); $currency_code = $currency['code']; $description = isset($_REQUEST['description']) ? $_REQUEST['description'] : 'N/A'; $template = new module_template(); ob_start(); require_once 'includes/plugin_paymethod_stripe/stripe-php/lib/Stripe.php'; $stripe = array("secret_key" => module_config::c('payment_method_stripe_secret_key'), "publishable_key" => module_config::c('payment_method_stripe_publishable_key')); Stripe::setApiKey($stripe['secret_key']); try { // todo- search for existing customer based on email address??? // todo: check if adding new plan to existing customer work?? $stripe_customer = Stripe_Customer::create(array("card" => $_POST['stripeToken'], "email" => $email, 'metadata' => array('user_id' => $user_id))); if ($stripe_customer && $stripe_customer->id) { //} && $stripe_customer->subscriptions){ $stripe_subscription = $stripe_customer->subscriptions->create(array('plan' => $stripe_plan_id)); if ($stripe_subscription && $stripe_subscription->id) { update_insert('invoice_payment_subscription_id', $invoice_payment_subscription_id, 'invoice_payment_subscription', array('status' => _INVOICE_SUBSCRIPTION_ACTIVE, 'date_start' => date('Y-m-d'), 'stripe_customer' => $stripe_customer->id, 'stripe_subscription' => $stripe_subscription->id)); module_paymethod_stripe::add_payment_data($invoice_payment_id, 'log', "Started Stripe Subscription: " . var_export(array('customer.id' => $stripe_customer->id, 'plan.id' => $stripe_plan_id, 'subscription.id' => $stripe_subscription->id), true)); // success! // redirect to receipt page. redirect_browser(module_invoice::link_public_payment_complete($invoice_id)); } else { echo 'Failed to create subscription with stripe'; } } $error = "Something went wrong during stripe payment. Please confirm invoice payment went through: " . htmlspecialchars($description); send_error($error); echo $error; } catch (Stripe_CardError $e) { // The card has been declined $body = $e->getJsonBody(); $err = $body['error']; $error = "Sorry: Payment failed. <br><br>\n\n" . htmlspecialchars($description) . ". <br><br>\n\n"; $error .= $err['message']; echo $error; $error .= "\n\n\n" . var_export($err, true); send_error($error); } catch (Exception $e) { $body = $e->getJsonBody(); $err = $body['error']; $error = "Sorry: Payment failed. <br><br>\n\n" . htmlspecialchars($description) . ". <br><br>\n\n"; $error .= $err['message']; echo $error; $error .= "\n\n\n" . var_export($err, true); send_error($error); } $template->content = ob_get_clean(); echo $template->render('pretty_html'); exit; } } echo 'Error paying via Stripe'; exit; case 'pay': $invoice_id = isset($_REQUEST['invoice_id']) ? $_REQUEST['invoice_id'] : false; $invoice_payment_id = isset($_REQUEST['invoice_payment_id']) ? $_REQUEST['invoice_payment_id'] : false; if ($invoice_id && $invoice_payment_id && isset($_POST['stripeToken'])) { $invoice_payment_data = module_invoice::get_invoice_payment($invoice_payment_id); $invoice_data = module_invoice::get_invoice($invoice_id); if ($invoice_payment_data && $invoice_data && $invoice_id == $invoice_data['invoice_id'] && $invoice_payment_data['invoice_id'] == $invoice_data['invoice_id']) { $currency = module_config::get_currency($invoice_payment_data['currency_id']); $currency_code = $currency['code']; $description = _l('Payment for invoice %s', $invoice_data['name']); $template = new module_template(); ob_start(); include module_theme::include_ucm('includes/plugin_paymethod_stripe/pages/stripe_form.php'); $template->content = ob_get_clean(); echo $template->render('pretty_html'); exit; } } echo 'Error paying via Stripe'; exit; } }
public function invoice($data) { global $current_user; if (empty($current_user->ID)) { return; } try { $stripeapikey = $this->wlm->GetOption('stripeapikey'); Stripe::setApiKey($stripeapikey); $inv = Stripe_Invoice::retrieve($data['txn_id']); $cust = Stripe_Customer::retrieve($inv['customer']); include $this->get_view_path('invoice_details'); die; } catch (Exception $e) { } }