function get_customers(WP_REST_Request $data) { $this->set_api_key(); $data = $data->get_params(); try { $args = array('limit' => 10); if (isset($data['starting_after'])) { $args['starting_after'] = $data['starting_after']; $customers = \Stripe\Customer::all($args); } elseif (!isset($data['starting_after']) && !isset($data['id'])) { $customers = \Stripe\Customer::all(array('limit' => 10)); } elseif (isset($data['id'])) { $customers = \Stripe\Customer::retrieve($data['id']); } return new WP_REST_Response($customers, 200); } catch (Stripe_AuthenticationError $e) { $body = $e->getJsonBody(); $err = $body['error']; return new WP_Error($err['type'], __($err['message']), array('status' => 403)); } catch (Stripe_Error $e) { $body = $e->getJsonBody(); $err = $body['error']; return new WP_Error($err['type'], __($err['message']), array('status' => 403)); } catch (\Stripe\Error\Base $e) { $body = $e->getJsonBody(); $err = $body['error']; return new WP_Error($err['type'], __($err['message']), array('status' => 403)); } }
/** * [listCustomers Returns a list of your customers. The customers are returned sorted by creation date, * with the most recently created customers appearing first.] * * @param array $opctions ['created' The value can be a string with an integer Unix timestamp, or it can be a dictionary with * the following options: * 'gt' here the created field is after this timestamp. * 'gte' where the created field is after or equal to this timestamp. * 'lt' where the created field is before this timestamp. * 'lte' where the created field is before or equal to this timestamp. * * 'ending_before' A cursor for use in pagination. ending_before is an object ID that defines your place * in the list. For instance, if you make a list request and receive 100 objects, starting * with obj_bar, your subsequent call can include ending_before=obj_bar in order to fetch * the previous page of the list. * * 'limit' A limit on the number of objects to be returned. Limit can range between 1 and 100 items. * * 'starting_after' A cursor for use in pagination. starting_after is an object ID that defines your place * in the list. For instance, if you make a list request and receive 100 objects, ending * with obj_foo, your subsequent call can include starting_after=obj_foo in order to fetch * the next page of the list.] * * @return [type] [A associative array with a data property that contains an array of up to limit customers, * starting after customer starting_after. Each entry in the array is a separate customer * object. If no more customers are available, the resulting array will be empty. * This request should never throw an error.] */ public function listCustomers($opctions = []) { return \Stripe\Customer::all($opctions); }
/** * @param null $limit * @param null $startAfter * @return StripeCollection */ public function listUsersDetails($limit = null, $startAfter = null) { $req = []; if ($limit) { $req['limit'] = $limit; } if ($startAfter) { $req['starting_after'] = $startAfter; } return Customer::all($req); }
/** * Getting all the customers for the user * @param user object * * @return an array with the customers */ public static function getCustomers($user) { // init out array $out_customers = array(); // setting stripe key $returned_object = null; if (strlen($user->stripe_key) > 2) { // we have secret key Stripe::setApiKey($user->stripe_key); $returned_object = Customer::all(); } else { // we have permission to connect Stripe::setApiKey($_ENV['STRIPE_SECRET_KEY']); $returned_object = Customer::all(array(), array('stripe_account' => $user->stripeUserId)); } // getting the customers // extracting data $customers = json_decode(strstr($returned_object, '{'), true); // setting the data to our own format foreach ($customers['data'] as $customer) { // updating array /* livemode - valid customer subscriptions - all the subscription a user has */ $out_customers[$customer['id']] = array('zombie' => $customer['livemode'], 'email' => $customer['email'], 'subscriptions' => $customer['subscriptions']['data']); } //foreach // return with the customers return $out_customers; }
<?php // Load Stripe's PHP bindings and set your API key require_once 'vendor/autoload.php'; \Stripe\Stripe::setApiKey('sk_your_api_key'); // Retrieve the first 100 customers created in the last month $customers = \Stripe\Customer::all(array("limit" => 100)); // Iterate through the first 100 and output the customer ID and created date foreach ($customers->data as $customer) { echo $customer->id . " created " . date("m-d-y", $customer->created) . "<br>"; } // While we have more results, iterate through them while ($customers->has_more) { // Add the `starting_after` parameter to reflect the last customer ID $customers = \Stripe\Customer::all(array("limit" => 100, "starting_after" => $customer->id)); foreach ($customers->data as $customer) { echo $customer->id . " created " . date("m-d-y", $customer->created) . "<br>"; } }
/** * request method * * @param string $method * @param array $data * * @return array - containing 'status', 'message' and 'data' keys * if response was successful, keys will be 'success', 'Success' and the stripe response as associated array respectively, * if request failed, keys will be 'error', the card error message if it was card_error, boolen false otherwise, and * error data as an array respectively */ private function request($method = null, $data = null) { if (!$method) { throw new Exception(__('Request method is missing')); } if (is_null($data)) { throw new Exception(__('Request Data is not provided')); } Stripe::setApiKey($this->key); $success = null; $error = null; $message = false; $log = null; try { switch ($method) { /** * * CHARGES * */ case 'charge': $success = $this->fetch(Charge::create($data)); break; case 'retrieveCharge': $success = $this->fetch(Charge::retrieve($data['charge_id'])); if (!empty($success['refunds'])) { foreach ($success['refunds'] as &$refund) { $refund = $this->fetch($refund); } } break; case 'updateCharge': $charge = Charge::retrieve($data['charge_id']); foreach ($data['fields'] as $field => $value) { $charge->{$field} = $value; } $success = $this->fetch($charge->save()); break; case 'refundCharge': $charge = Charge::retrieve($data['charge_id']); // to prevent unknown param error unset($data['charge_id']); $success = $this->fetch($charge->refund($data)); foreach ($success['refunds']['data'] as &$refund) { $refund = $this->fetch($refund); } break; case 'captureCharge': $charge = Charge::retrieve($data['charge_id']); unset($data['charge_id']); $success = $this->fetch($charge->capture($data)); if (!empty($success['refunds']['data'])) { foreach ($success['refunds']['data'] as &$refund) { $refund = $this->fetch($refund); } } break; case 'listCharges': $charges = Charge::all(); $success = $this->fetch($charges); foreach ($success['data'] as &$charge) { $charge = $this->fetch($charge); if (isset($charge['refunds']['data']) && !empty($charge['refunds']['data'])) { foreach ($charge['refunds']['data'] as &$refund) { $refund = $this->fetch($refund); } unset($refund); } } break; /** * CUSTOMERS */ /** * CUSTOMERS */ case 'createCustomer': $customer = Customer::create($data); $success = $this->fetch($customer); if (!empty($success['cards']['data'])) { foreach ($success['cards']['data'] as &$card) { $card = $this->fetch($card); } unset($card); } if (!empty($success['subscriptions']['data'])) { foreach ($success['subscriptions']['data'] as &$subscription) { $subscription = $this->fetch($subscription); } unset($subscription); } break; case 'retrieveCustomer': $customer = Customer::retrieve($data['customer_id']); $success = $this->fetch($customer); if (!empty($success['cards']['data'])) { foreach ($success['cards']['data'] as &$card) { $card = $this->fetch($card); } unset($card); } if (!empty($success['subscriptions']['data'])) { foreach ($success['subscriptions']['data'] as &$subscription) { $subscription = $this->fetch($subscription); } unset($subscription); } break; case 'updateCustomer': $cu = Customer::retrieve($data['customer_id']); foreach ($data['fields'] as $field => $value) { $cu->{$field} = $value; } $success = $this->fetch($cu->save()); if (!empty($success['cards']['data'])) { foreach ($success['cards']['data'] as &$card) { $card = $this->fetch($card); } unset($card); } if (!empty($success['subscriptions']['data'])) { foreach ($success['subscriptions']['data'] as &$subscription) { $subscription = $this->fetch($subscription); } unset($subscription); } break; case 'deleteCustomer': $cu = Customer::retrieve($data['customer_id']); $success = $this->fetch($cu->delete()); break; case 'listCustomers': $customers = Customer::all($data['options']); $success = $this->fetch($customers); foreach ($success['data'] as &$customer) { $customer = $this->fetch($customer); if (!empty($customer['cards']['data'])) { foreach ($customer['cards']['data'] as &$card) { $card = $this->fetch($card); } unset($card); } if (!empty($customer['subscriptions']['data'])) { foreach ($customer['subscriptions']['data'] as &$subscription) { $subscription = $this->fetch($subscription); } unset($subscription); } } break; /** * CARDS * */ /** * CARDS * */ case 'createCard': $cu = Customer::retrieve($data['customer_id']); $validCardFields = ['object', 'address_zip', 'address_city', 'address_state', 'address_country', 'address_line1', 'address_line2', 'number', 'exp_month', 'exp_year', 'cvc', 'name', 'metadata']; // unset not valid keys to prevent unknown parameter stripe error unset($data['customer_id']); foreach ($data['source'] as $k => $v) { if (!in_array($k, $validCardFields)) { unset($data['source'][$k]); } } $card = $cu->sources->create($data); $success = $this->fetch($card); break; case 'retrieveCard': $cu = Customer::retrieve($data['customer_id']); $card = $cu->sources->retrieve($data['card_id']); $success = $this->fetch($card); break; case 'updateCard': $cu = Customer::retrieve($data['customer_id']); $cuCard = $cu->sources->retrieve($data['card_id']); foreach ($data['fields'] as $field => $value) { $cuCard->{$field} = $value; } $card = $cuCard->save(); $success = $this->fetch($card); break; case 'deleteCard': $cu = Customer::retrieve($data['customer_id']); $card = $cu->sources->retrieve($data['card_id'])->delete(); $success = $this->fetch($card); break; case 'listCards': $cu = Customer::retrieve($data['customer_id']); $cards = $cu->sources->all($data['options']); $success = $this->fetch($cards); foreach ($success['data'] as &$card) { $card = $this->fetch($card); } break; /** * SUBSCRIPTIONS * */ /** * SUBSCRIPTIONS * */ case 'createSubscription': $cu = Customer::retrieve($data['customer_id']); // unset customer_id to prevent unknown parameter stripe error unset($data['customer_id']); $subscription = $cu->subscriptions->create($data['subscription']); $success = $this->fetch($subscription); break; case 'retrieveSubscription': $cu = Customer::retrieve($data['customer_id']); $subscription = $cu->subscriptions->retrieve($data['subscription_id']); $success = $this->fetch($subscription); break; case 'updateSubscription': $cu = Customer::retrieve($data['customer_id']); $cuSubscription = $cu->subscriptions->retrieve($data['subscription_id']); foreach ($data['fields'] as $field => $value) { $cuSubscription->{$field} = $value; } $subscription = $cuSubscription->save(); $success = $this->fetch($subscription); break; case 'cancelSubscription': $cu = Customer::retrieve($data['customer_id']); $subscription = $cu->subscriptions->retrieve($data['subscription_id'])->cancel($data['at_period_end']); $success = $this->fetch($subscription); break; case 'listSubscriptions': $cu = Customer::retrieve($data['customer_id']); $subscriptions = $cu->subscriptions->all($data['options']); $success = $this->fetch($subscriptions); foreach ($success['data'] as &$subscription) { $subscription = $this->fetch($subscription); } break; /** * PLANS * */ /** * PLANS * */ case 'createPlan': $plan = Plan::create($data); $success = $this->fetch($plan); break; case 'retrievePlan': $plan = Plan::retrieve($data['plan_id']); $success = $this->fetch($plan); break; case 'updatePlan': $p = Plan::retrieve($data['plan_id']); foreach ($data['fields'] as $field => $value) { $p->{$field} = $value; } $plan = $p->save(); $success = $this->fetch($plan); break; case 'deletePlan': $p = Plan::retrieve($data['plan_id']); $plan = $p->delete(); $success = $this->fetch($plan); break; case 'listPlans': $plans = Plan::all($data['options']); $success = $this->fetch($plans); foreach ($success['data'] as &$plan) { $plan = $this->fetch($plan); } break; /** * COUPONS * */ /** * COUPONS * */ case 'createCoupon': $coupon = Coupon::create($data); $success = $this->fetch($coupon); break; case 'retrieveCoupon': $coupon = Coupon::retrieve($data['coupon_id']); $success = $this->fetch($coupon); break; case 'deleteCoupon': $c = Coupon::retrieve($data['coupon_id']); $coupon = $c->delete(); $success = $this->fetch($coupon); break; case 'listCoupons': $coupons = Coupon::all($data['options']); $success = $this->fetch($coupons); foreach ($success['data'] as &$coupon) { $coupon = $this->fetch($coupon); } break; /** * * EVENTS * */ /** * * EVENTS * */ case 'retrieveEvent': $event = Event::retrieve($data['event_id']); $success = $this->fetch($event); // cards if (isset($success['data']['object']['cards']['data']) && !empty($success['data']['object']['cards']['data'])) { foreach ($success['data']['object']['cards']['data'] as &$card) { $card = $this->fetch($card); } unset($refund); } break; case 'listEvents': $events = Event::all($data['options']); $success = $this->fetch($events); foreach ($success['data'] as &$event) { $event = $this->fetch($event); // refunds if (isset($event['data']['object']['refunds']) && !empty($event['data']['object']['refunds'])) { foreach ($event['data']['object']['refunds'] as &$refund) { $refund = $this->fetch($refund); } unset($refund); } // cards if (isset($event['data']['object']['cards']['data']) && !empty($event['data']['object']['cards']['data'])) { foreach ($event['data']['object']['cards']['data'] as &$card) { $card = $this->fetch($card); } unset($refund); } } break; } } catch (Card $e) { $body = $e->getJsonBody(); $error = $body['error']; $error['http_status'] = $e->getHttpStatus(); $message = $error['message']; } catch (InvalidRequest $e) { $body = $e->getJsonBody(); $error = $body['error']; $error['http_status'] = $e->getHttpStatus(); } catch (Authentication $e) { $error = $e->getJsonBody(); $error['http_status'] = $e->getHttpStatus(); } catch (ApiConnection $e) { $body = $e->getJsonBody(); $error['http_status'] = $e->getHttpStatus(); } catch (Base $e) { $body = $e->getJsonBody(); $error['http_status'] = $e->getHttpStatus(); } catch (\Exception $e) { $body = $e->getJsonBody(); $error['http_status'] = $e->getHttpStatus(); } if ($success) { // if ($this->logFile && in_array($this->logType, ['both', 'success'])) { // CakeLog::write('Success', $method, $this->logFile); // } return ['status' => 'success', 'message' => 'Success', 'response' => $success]; } $str = ''; $str .= $method . ", type:" . (!empty($error['type']) ? $error['type'] : ''); $str .= ", type:" . (!empty($error['type']) ? $error['type'] : ''); $str .= ", http_status:" . (!empty($error['http_status']) ? $error['http_status'] : ''); $str .= ", param:" . (!empty($error['param']) ? $error['param'] : ''); $str .= ", message:" . (!empty($error['message']) ? $error['message'] : ''); // if ($this->logFile && in_array($this->logType, array('both', 'error'))) { // CakeLog::write('Error', $str, $this->logFile ); // } return ['status' => 'error', 'message' => $message, 'response' => $error]; }
/** * getCustomers * Getting a list of customers. * -------------------------------------------------- * @returns The stripe customers. * @throws StripeNotConnected * -------------------------------------------------- */ public function getCustomers() { $rawData = array(); $decodedData = array(); $hasMore = TRUE; $startingAfter = null; while ($hasMore) { try { /* Collecting events with pagination. */ if ($startingAfter) { $rawData = \Stripe\Customer::all(array("limit" => 100, "starting_after" => $startingAfter)); } else { $rawData = \Stripe\Customer::all(array("limit" => 100)); } /* Adding objects to collection. */ $currentData = json_decode($this->loadJSON($rawData), TRUE); $decodedData = array_merge($decodedData, $currentData['data']); } catch (\Stripe\Error\Authentication $e) { // Access token expired. Calling handler. $this->getNewAccessToken(); } $hasMore = $currentData['has_more']; $startingAfter = end($currentData['data'])['id']; } // Getting the plans. $customers = []; foreach ($decodedData as $customer) { array_push($customers, $customer); } // Return. return $customers; }
<?php require_once dirname(__FILE__) . '/init.php'; require_once dirname(__FILE__) . '/config.php'; $increment = 100; $count = 0; $continue = true; $starting_after = NULL; while ($continue) { $customersJSON = \Stripe\Customer::all(array("limit" => $increment, "starting_after" => $starting_after)); $customers = $customersJSON->__toArray(true); $continue = $customers['has_more']; $starting_after = end($customers['data'])['id']; foreach ($customers['data'] as $customer) { if (count($customer['subscriptions']['data']) == 0) { $count++; $cu = \Stripe\Customer::retrieve($customer['id']); $cu->delete(); } } } echo $count;
public function getCustomers() { return Customer::all(); }
function listAllReccuringCustomers() { $this->setApiKey(); $customers = \Stripe\Customer::all(array("limit" => 3)); return $customers; }