/** * 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]; }
/** * Getting specific events for the user (null = all) * @param user object * * @return an array with the events */ public static function getEvents($user) { $out_events = array(); // initializing variables $has_more = true; $foundLatestEvent = false; $latestEvent = Abf\Event::where('user', $user->id)->where('provider', 'stripe')->orderBy('created', 'desc')->first(); $last_obj = null; // continue request as long as there is more AND we don't already have it while ($has_more && !$foundLatestEvent) { // trying to avoid overflow $previous_last_obj = $last_obj; // telling stripe who we are if (strlen($user->stripe_key) > 2) { Stripe::setApiKey($user->stripe_key); if ($last_obj) { // we have last obj -> starting from there $returned_object = Event::all(array('limit' => 20, 'starting_after' => $last_obj)); } else { // starting from zero $returned_object = Event::all(array('limit' => 100)); } } else { Stripe::setApiKey($_ENV['STRIPE_SECRET_KEY']); if ($last_obj) { // we have last obj -> starting from there $returned_object = Event::all(array('limit' => 20, 'starting_after' => $last_obj), array('stripe_account' => $user->stripeUserId)); } else { // starting from zero $returned_object = Event::all(array('limit' => 100), array('stripe_account' => $user->stripeUserId)); } } // getting the events // https://stripe.com/docs/api/php#events // pagination.... // extractin json (this is not the best approach) $events = json_decode(strstr($returned_object, '{'), true); // getting relevant fields foreach ($events['data'] as $event) { // updating array /* created - timestamp type - string, see https://stripe.com/docs/api/php#event_types object - hash map (assoc array) */ if (isset($event['data']['object'])) { if ($latestEvent) { if ($event['id'] == $latestEvent->eventID) { $foundLatestEvent = true; } } $out_events[$event['id']] = array('created' => $event['created'], 'type' => $event['type'], 'data' => $event['data'], 'provider' => 'stripe'); $last_obj = $event['id']; } } // foreach // updating has_more $has_more = $events['has_more']; // avoiding infinite loop if ($previous_last_obj == $last_obj and $has_more) { // we should never get here // this is too bad system failure :( $has_more = false; } } // while // returning object return $out_events; }
/** * getEvents * Getting events from the last 30 days. * -------------------------------------------------- * @returns The stripe events. * @throws StripeNotConnected * -------------------------------------------------- */ public function getEvents() { /* Connecting to stripe, and making query. */ $rawData = array(); $decodedData = array(); $hasMore = TRUE; $startingAfter = null; while ($hasMore) { try { /* Collecting events with pagination. */ if ($startingAfter) { $rawData = \Stripe\Event::all(array("limit" => 100, "starting_after" => $startingAfter)); } else { $rawData = \Stripe\Event::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. $events = []; foreach ($decodedData as $event) { array_push($events, $event); } // Return. return $events; }