function get_coupons(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'];
             $coupons = \Stripe\Coupon::all($args);
         } elseif (!isset($data['starting_after']) && !isset($data['id'])) {
             $coupons = \Stripe\Coupon::all(array('limit' => 10));
         } elseif (isset($data['id'])) {
             $coupons = \Stripe\Coupon::retrieve($data['id']);
         }
         return new WP_REST_Response($coupons, 200);
     } catch (\Stripe\Error\RateLimit $e) {
         $body = $e->getJsonBody();
         $err = $body['error'];
         return new WP_Error($err['type'], __($err['message']), array('status' => $e->getHttpStatus()));
     } catch (\Stripe\Error\InvalidRequest $e) {
         $body = $e->getJsonBody();
         $err = $body['error'];
         return new WP_Error($err['type'], __($err['message']), array('status' => $e->getHttpStatus()));
     } catch (\Stripe\Error\Authentication $e) {
         $body = $e->getJsonBody();
         $err = $body['error'];
         return new WP_Error($err['type'], __($err['message']), array('status' => $e->getHttpStatus()));
     } catch (\Stripe\Error\ApiConnection $e) {
         $body = $e->getJsonBody();
         $err = $body['error'];
         return new WP_Error($err['type'], __($err['message']), array('status' => $e->getHttpStatus()));
     } catch (\Stripe\Error\Base $e) {
         $body = $e->getJsonBody();
         $err = $body['error'];
         return new WP_Error($err['type'], __($err['message']), array('status' => $e->getHttpStatus()));
     } catch (Exception $e) {
         $body = $e->getJsonBody();
         $err = $body['error'];
     }
 }
Esempio n. 2
0
 public function getCoupons($limit, $after = null, $before = null)
 {
     return \Stripe\Coupon::all(['limit' => $limit, 'starting_after' => $after, 'ending_before' => $before]);
 }
 /**
  * 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];
 }