public function do_charge() { if (wp_verify_nonce($_POST['wp-simple-pay-pro-nonce'], 'charge_card')) { global $sc_options; $query_args = array(); // Set redirect $redirect = $_POST['sc-redirect']; $fail_redirect = $_POST['sc-redirect-fail']; $failed = null; $message = ''; // Get the credit card details submitted by the form $token = $_POST['stripeToken']; $amount = $_POST['sc-amount']; $description = $_POST['sc-description']; $store_name = $_POST['sc-name']; $currency = $_POST['sc-currency']; $details_placement = $_POST['sc-details-placement']; $charge = null; $sub = isset($_POST['sc_sub_id']); $interval = isset($_POST['sc_sub_interval']) ? $_POST['sc_sub_interval'] : 'month'; $interval_count = isset($_POST['sc_sub_interval_count']) ? $_POST['sc_sub_interval_count'] : 1; $statement_description = isset($_POST['sc_sub_statement_description']) ? $_POST['sc_sub_statement_description'] : ''; $setup_fee = isset($_POST['sc_sub_setup_fee']) ? $_POST['sc_sub_setup_fee'] : 0; $coupon = isset($_POST['sc_coup_coupon_code']) ? $_POST['sc_coup_coupon_code'] : ''; $test_mode = isset($_POST['sc_test_mode']) ? $_POST['sc_test_mode'] : 'false'; if ($sub) { $sub = !empty($_POST['sc_sub_id']) ? $_POST['sc_sub_id'] : 'custom'; } Stripe_Checkout_Functions::set_key($test_mode); $meta = array(); if (!empty($setup_fee)) { $meta['Setup Fee'] = Stripe_Checkout_Misc::to_formatted_amount($setup_fee, $currency); } $meta = apply_filters('sc_meta_values', $meta); try { if ($sub == 'custom') { $timestamp = time(); $plan_id = $_POST['stripeEmail'] . '_' . $amount . '_' . $timestamp; $name = __('Subscription:', 'sc_sub') . ' ' . Stripe_Checkout_Misc::to_formatted_amount($amount, $currency) . ' ' . strtoupper($currency) . '/' . $interval; // Create a plan $plan_args = array('amount' => $amount, 'interval' => $interval, 'name' => $name, 'currency' => $currency, 'id' => $plan_id, 'interval_count' => $interval_count); if (!empty($statement_description)) { $plan_args['statement_descriptor'] = $statement_description; } $new_plan = \Stripe\Plan::create($plan_args); // Create a customer and charge $new_customer = \Stripe\Customer::create(array('email' => $_POST['stripeEmail'], 'card' => $token, 'plan' => $plan_id, 'metadata' => $meta, 'account_balance' => $setup_fee)); } else { // Create new customer $cust_args = array('email' => $_POST['stripeEmail'], 'card' => $token, 'plan' => $sub, 'metadata' => $meta, 'account_balance' => $setup_fee); if (!empty($coupon)) { $cust_args['coupon'] = $coupon; } $new_customer = \Stripe\Customer::create($cust_args); // Set currency based on sub $plan = \Stripe\Plan::retrieve($sub); //echo $subscription . '<Br>'; $currency = strtoupper($plan->currency); } // We want to add the meta data and description to the actual charge so that users can still view the meta sent with a subscription + custom fields // the same way that they would normally view it without subscriptions installed. // We need the steps below to do this // First we get the latest invoice based on the customer ID $invoice = \Stripe\Invoice::all(array('customer' => $new_customer->id, 'limit' => 1)); // If this is a trial we need to skip this part since a charge is not made $trial = $invoice->data[0]->lines->data[0]->plan->trial_period_days; if (empty($trial) || !empty($setup_fee)) { // Now that we have the invoice object we can get the charge ID $inv_charge = $invoice->data[0]->charge; // Finally, with the charge ID we can update the specific charge and inject our meta data sent from Stripe Custom Fields $ch = \Stripe\Charge::retrieve($inv_charge); $charge = $ch; if (!empty($meta)) { $ch->metadata = $meta; } if (!empty($description)) { $ch->description = $description; } $ch->save(); $query_args = array('charge' => $ch->id, 'store_name' => urlencode($store_name)); $failed = false; } else { $sub_id = $invoice->data[0]->subscription; if (!empty($description)) { $customer = \Stripe\Customer::retrieve($new_customer->id); $subscription = $customer->subscriptions->retrieve($sub_id); $subscription->metadata = array('product' => $description); $subscription->save(); } $query_args = array('cust_id' => $new_customer->id, 'sub_id' => $sub_id, 'store_name' => urlencode($store_name)); $failed = false; } } catch (Exception $e) { // Something else happened, completely unrelated to Stripe $redirect = $fail_redirect; $failed = true; $e = $e->getJsonBody(); $query_args = array('sub' => true, 'error_code' => $e['error']['type'], 'charge_failed' => true); } unset($_POST['stripeToken']); do_action('sc_redirect_before'); if ($test_mode == 'true') { $query_args['test_mode'] = 'true'; } if ('below' == $details_placement) { $query_args['details_placement'] = $details_placement; } if (!empty($trial) && empty($setup_fee)) { $query_args['trial'] = 1; } wp_redirect(esc_url_raw(add_query_arg(apply_filters('sc_redirect_args', $query_args, $charge), apply_filters('sc_redirect', $redirect, $failed)))); exit; } }
<td><?php echo $subscription->status; ?> </td> <td><a href="<?php echo $cancel_url; ?> " class="btn btn-danger btn-xs confirm">Cancel Subscription</a></td> </tr> <?php } ?> </table> <?php } $invoices = \Stripe\Invoice::all(array("limit" => 300, "customer" => $user->stripe_cust)); ?> <h2>Invoices</h2> <?php echo "<table class='table table-striped table-bordered table-hover'>"; echo "<tr>"; echo "<th>Invoice Date</th>"; echo "<th>Invoice Id</th>"; echo "<th>Total</th>"; echo "<th>Status</th>"; echo "<th>Details</th>"; echo "</tr>"; foreach ($invoices['data'] as $invoice) { $class = $invoice->paid ? "success" : "warning"; echo "<tr class='{$class}'>";
/** * Gets all invoices for a customer. * * @return array */ public function invoices() { $this->info(); if (!$this->stripe_customer) { return array(); } $invoices = Stripe_Invoice::all(array('customer' => $this->id, 'limit' => 100)); $invoices_array = array(); foreach ($invoices->data as $invoice) { $invoices_array[] = $this->invoice($invoice); } return $invoices_array; }
public function invoicesAction(Request $request) { try { $customer_id = $request->query->get('customer_id'); $limit = $request->query->get('limit') ? $request->query->get('limit') : 10; $starting_after = $request->query->get('starting_after') ? $request->query->get('starting_after') : -1; $ending_before = $request->query->get('ending_before') ? $request->query->get('ending_before') : -1; $date = $request->query->get('date') ? $request->query->get('date') : -1; $request_array = array("customer" => $customer_id, "limit" => $limit); if ($starting_after != -1) { $request_array["starting_after"] = $starting_after; } if ($ending_before != -1) { $request_array["ending_before"] = $ending_before; } if ($date != -1) { $date_params = array("gt" => $date); $request_array["date"] = $date_params; } \Stripe\Stripe::setApiKey("sk_test_DxL4C2nUSAVBILG7FtnY4mYs"); $response = \Stripe\Invoice::all($request_array); $invoices = array(); $i = 0; foreach ($response['data'] as $invoice) { $invoice_item = array("date" => $invoice['date'], "end" => $invoice['lines']['data'][0]['period']['end'], "name" => $invoice['lines']['data'][0]['plan']['name'], "id" => $invoice['id'], "interval" => $invoice['lines']['data'][0]['plan']['interval'], "total" => $invoice['total']); $invoices[$i] = $invoice_item; $i++; } $invoices = json_encode($invoices); } catch (\Stripe\Error\ApiConnection $e) { // Network communication with Stripe failed $error = array("error" => "Connection Error"); return new Response(json_encode($error), 422); } catch (\Stripe\Error\Authentication $e) { // Authentication with Stripe's API failed $error = array("error" => "Invalid API Key"); return new Response(json_encode($error), 422); } catch (\Stripe\Error\InvalidRequest $e) { // Invalid parameters were supplied to Stripe's API $error = array("error" => "Invalid Request", "array" => $request_array); return new Response(json_encode($error), 422); } catch (\Stripe\Error\Base $e) { // Display a very generic error to the user, and maybe send $error = array("error" => $e); return new Response(json_encode($error), 422); } catch (Exception $e) { // Something else happened, completely unrelated to Stripe $error = array("error" => $e); return new Response(json_encode($error), 422); } return new Response($invoices, 200); }
* @author Shane Barron <*****@*****.**> * @author Aaustin Barron <*****@*****.**> * @copyright 2015 SocialApparatus * @license http://opensource.org/licenses/MIT MIT * @version 1 * @link http://socialapparatus.com */ namespace SocialApparatus; denyDirect(); gateKeeper(); $user = getLoggedInUser(); $stripe_cust = $user->stripe_cust; if ($stripe_cust) { \Stripe\Stripe::setApiKey(EcommercePlugin::secretKey()); $invoices = \Stripe\Invoice::all(array("customer" => $stripe_cust)); echo "<table class='table table-striped table-bordered table-hover'>"; echo "<tr>"; echo "<th>Invoice Date</th>"; echo "<th>Invoice Id</th>"; echo "<th>Total</th>"; echo "<th>Status</th>"; echo "<th>Details</th>"; echo "</tr>"; foreach ($invoices['data'] as $invoice) { $class = $invoice->paid ? "success" : "warning"; echo "<tr class='{$class}'>"; echo "<td>"; echo date("m/d/Y", $invoice->date); echo "</td>"; echo "<td>";
public function getInvoices($customerId) { $invoices = Invoice::all(array('customer' => $customerId))->data; return array_map(function ($invoice) { return new Invoice($invoice->date, $invoice->total, $invoice->paid); }, $invoices); }
/** * * @SWG\Api( * path="/billing_history", * description="API for user actions", * @SWG\Operation( * method="GET", * type="array[Invoice]", * summary="Retrieve a user's billing history", * ) * ) */ public function billing_history_get() { $this->validate_user(); $this->load->model(array('Plan', 'Subscription')); $this->load->helper('notification'); \Stripe\Stripe::setApiKey($this->config->item('stripe_private_key')); $subscription = $this->Subscription->load_by_user_id(get_user_id()); if ($subscription) { try { $invoices = \Stripe\Invoice::all(array("customer" => $subscription->stripe_customer_id, "limit" => 12)); //array_print($invoices); } catch (Exception $e) { log_message('error', 'Exception while retrieving billing history: ' . $e->getMessage()); json_error('We experienced an error while retrieving your billing history: ' . $e->getMessage()); return; } $this->response(decorate_invoices($invoices->data)); } else { $this->response(array()); } }
public function get_invoice_test($stripe_customer_id) { try { \Stripe\Stripe::setApiKey($this->config->item("stripe_secret_key")); $reply = \Stripe\Invoice::all(array("customer" => $stripe_customer_id)); $data = $reply->data; $response = $data; $total_invoices = count($response); for ($r = 0; $r < $total_invoices; $r++) { $stripe_invoice_id = $response[$r]->id; $invoice_amount = $response[$r]->amount_due; $invoice_date = $response[$r]->date; //$closed = $response[$r]->closed; $lines = $response[$r]->lines; $data = $lines->data; $next_payment_attempt = $response[$r]->next_payment_attempt; $paid = $response[$r]->paid; $receipt_number = $lines->receipt_number; echo "<br/>Invoice Amount = " . $invoice_amount; $total_invoice_items = count($data); for ($s = 0; $s < $total_invoice_items; $s++) { $subscription_id = $data[$s]->id; $amount = $data[$s]->amount; $quantity = $data[$s]->quantity; echo "<br/>Invoice Item Amount = " . $amount; } } $return['message'] = 'true'; $return['response'] = 'Invoice added successfully'; } catch (\Stripe\Error\Card $e) { // Since it's a decline, \Stripe\Error\Card will be caught $body = $e->getJsonBody(); $err = $body['error']; $return['message'] = 'false'; $return['response'] = $err['message']; /*echo('Status is:' . $e->getHttpStatus() . "\n"); echo('Type is:' . $err['type'] . "\n"); echo('Code is:' . $err['code'] . "\n"); // param is '' in this case echo('Param is:' . $err['param'] . "\n"); echo('Message is:' . $err['message'] . "\n");*/ } catch (\Stripe\Error\RateLimit $e) { // Too many requests made to the API too quickly $return['message'] = 'false'; $return['response'] = $e->getMessage(); } catch (\Stripe\Error\InvalidRequest $e) { // Invalid parameters were supplied to Stripe's API $return['message'] = 'false'; $return['response'] = $e->getMessage(); } catch (\Stripe\Error\Authentication $e) { // Authentication with Stripe's API failed // (maybe you changed API keys recently) $return['message'] = 'false'; $return['response'] = $e->getMessage(); } catch (\Stripe\Error\ApiConnection $e) { // Network communication with Stripe failed $return['message'] = 'false'; $return['response'] = $e->getMessage(); } catch (\Stripe\Error\Base $e) { // Display a very generic error to the user, and maybe send $return['message'] = 'false'; $return['response'] = $e->getMessage(); // yourself an email } catch (Exception $e) { // Something else happened, completely unrelated to Stripe $return['message'] = 'false'; $return['response'] = $e->getMessage(); } return $return; }