function get_plans(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']; $plans = \Stripe\Plan::all($args); } elseif (!isset($data['starting_after']) && !isset($data['id'])) { $plans = \Stripe\Plan::all(array('limit' => 10)); } elseif (isset($data['id'])) { $plans = \Stripe\Plan::retrieve($data['id']); } return new WP_REST_Response($plans, 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)); } }
/** * @param null $limit * @param null $startingAfter * @return array|\Stripe\Collection */ public function all($limit = null, $startingAfter = null) { $data = []; if ($limit) { $data['limit'] = $limit; } if ($startingAfter) { $data['starting_after'] = $startingAfter; } $plans = \Stripe\Plan::all($data); return $plans; }
private static function getStripePlans() { Stripe::setApiKey(Config::get('services.stripe.secret')); $plans = StripePlan::all(); if (!$plans) { return false; } self::$items = []; foreach ($plans->data as $plan) { $item = $plan->__toArray(); if ($item['amount'] > 0) { $item['amount'] = $item['amount'] / 100; } self::$items[] = $item; } }
public function construct_form() { \Stripe\Stripe::setApiKey($this->stripe_private_key); $stripe_account = \Stripe\Account::retrieve(); $default_currency_symbol = \Symfony\Component\Intl\Intl::getCurrencyBundle()->getCurrencySymbol(strtoupper($stripe_account->default_currency)); $plans = \Stripe\Plan::all(array("limit" => 10)); if (empty($this->formTemplate) || !file_exists($this->formTemplate)) { $this->set_form_template(''); } $wp_nonce_field = wp_nonce_field($this->nonce_id, '_wpnonce', true, false); $donate['defaults']['currency'] = $stripe_account->default_currency; $currencies = $stripe_account->currencies_supported; $index = array_search($stripe_account->default_currency, $currencies); array_splice($currencies, $index, 1); array_unshift($currencies, $stripe_account->default_currency); ob_start(); include $this->formTemplate; $return = ob_get_contents(); ob_end_clean(); return $return; }
function musof_stripe_checkout() { global $ultimatemember; $user_id = get_current_user_id(); $theme = get_stylesheet_directory_uri(); //@TODO reload cache $role = $ultimatemember->user->profile['role']; $ajax_url = admin_url('/admin-ajax.php'); echo '<div class="um-account-heading uimob340-hide uimob500-hide"><i class="um-faicon-star"></i>Pro Membership</div>'; if ($role != 'pro-member') { $plans_data = array(); require dirname(__FILE__) . '/stripe-php/init.php'; \Stripe\Stripe::setApiKey("sk_test_P7kUSLtvnEa1JLzaJyk7GGIX"); $plans = \Stripe\Plan::all(array("limit" => 3)); $plans = $plans->__toArray(true); foreach ($plans['data'] as $plan) { $plans_data[$plan['id']] = $plan['amount']; } echo <<<FREE \t\t<div class="um-field um-field-user_pro" data-key="user_pro"> \t\t<script src="https://checkout.stripe.com/checkout.js"></script> \t\t<p>Upgrade to pro membership:</p> \t\t<a class="button" id="pro-yearly">Pay Annually</a> \t\t<a class="button" id="pro-monthly">Pay Monthly</a> \t\t<style> \t#musof_payment_processing, #musof_payment_processing img { \t\tmargin: auto; \t\tbottom: 0; \t\ttop: 0; \t\tleft: 0; \t\tright: 0; \t\tbackground-color: rgba(255,255,255,0.8); \t\tposition: fixed; \t} \t</style> \t<div id="musof_payment_processing" style="display:none;"> \t<img src="https://cdnjs.cloudflare.com/ajax/libs/file-uploader/3.7.0/processing.gif"> \t</div> \t \t<script> \t(function(\$) { \t\tvar name = 'Muso Net Ltd', \t\tcurrency = "usd", \t\tplan = 'pro-monthly', \t\turl = window.location.origin + window.location.pathname; \t\tvar handler = StripeCheckout.configure({ \t\t\tkey: 'pk_test_PxOWZ6rBgc7dmckuYUx24bnO', \t\t\timage: '{$theme}/images/muso-square.png', \t\t\tlocale: 'auto', \t\t\ttoken: function(token) { \t\t\t\tquery = {}; \t\t\t\tquery.action = 'musof_pro'; \t\t\t\tquery.description = description; \t\t\t\tquery.currency = currency; \t\t\t\tquery.amount = amount; \t\t\t\tquery.plan = plan; \t\t\t\tquery.tokenData = token; \t\t\t\tquery.token = token.id; \t\t\t\tjQuery.post( '{$ajax_url}', query, function ( response ) { \t\t\t\t\tconsole.log( response ); \t\t\t\t\tif ( 'Payment successful' == response ) { \t\t\t\t\t\tconsole.log( 'Success!!!' ); \t\t\t\t\t\twindow.location = url + '?updated=payment-successful'; \t\t\t\t\t} else { \t\t\t\t\t\tconsole.log( 'Error!' ); \t\t\t\t\t\tresponse = JSON.parse( response ); \t\t\t\t\t\tconsole.log( response ); \t\t\t\t\t} \t\t\t\t} ); } }); \$('#pro-yearly').on('click', function(e) { \tdescription = 'yearly'; \tamount = {$plans_data['pro-yearly']}; \tplan = 'pro-yearly'; \t\t // Open Checkout with further options \thandler.open({ \t\tname: name, \t\tdescription: description, \t\tcurrency: currency, \t\tamount: amount \t}); e.preventDefault(); }); \$('#pro-monthly').on('click', function(e) { // Open Checkout with further options \tdescription = 'monthly'; \tamount = {$plans_data['pro-monthly']}; \tplan = 'pro-monthly'; \thandler.open({ \t\tname: name, \t\tdescription: description, \t\tcurrency: currency, \t\tamount: amount \t}); e.preventDefault(); }); \t\t// Close Checkout on page navigation \$(window).on('popstate', function() { \tconsole.log( 'popstate' ); \t\$('#musof_payment_processing').show(); \thandler.close(); }); })(jQuery); </script> </div> FREE; } else { $payment_log = get_user_meta($user_id, 'musof_payment_log', true); echo <<<PRO \t<p id='member-status'>You are a Pro Memeber \t<a href='?cancel-membership={$user_id}'>Cancel Membership</a> \t</p> \t \t<style> \tmember-status \t\t#tbl-header { \tfont-size: 15px !important; \tline-height: 22px !important; \tfont-weight: 600; } \t\t#pay-history-tbl th { font-size: 12px !important; line-height: 22px !important; font-weight: 600; padding-left: 20px; padding-top: 20px; }\t </style> PRO; if (!empty($_GET['cancel-membership'])) { musof_membership_cancel(); } echo '</table>'; } /* $payment_log = get_user_meta($user_id, 'musof_payment_log', true); $payment_log = $payment_log ? $payment_log : array(); foreach ($payment_log as $time => $value) { print_r($value); } * */ }
/** * 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]; }
/** * Return all created plans. * * @return array */ public function getPlans() { $plans = Plan::all(); $formatted = []; foreach ($plans->data as $plan) { $formatted[] = ['interval' => $plan['interval'], 'name' => $plan['name'], 'amount' => $plan['amount'] / 100, 'currency' => $plan['currency'], 'id' => $plan['id'], 'created' => $plan['created']]; } usort($formatted, function ($a1, $a2) { if ($a1['created'] == $a2['created']) { return 0; } return $a1['created'] < $a2['created'] ? -1 : 1; }); return $formatted; }
private function getAvailablePlans() { try { \Stripe\Stripe::setApiKey(AppConfig::getValue('stripe_secret_api_key')); return \Stripe\Plan::all(); } catch (\Stripe\Error\Card $exception) { $this->logStripeException($exception, 'N/A'); } catch (\Stripe\Error\InvalidRequest $exception) { $this->logStripeException($exception, 'N/A'); } catch (\Stripe\Error\Authentication $exception) { $this->logStripeException($exception, 'N/A'); } catch (\Stripe\Error\ApiConnection $exception) { $this->logStripeException($exception, 'N/A'); } catch (\Stripe\Error\Base $exception) { $this->logStripeException($exception, 'N/A'); } }
/** * updatePlans * -------------------------------------------------- * Updating the current stripe Plans. * @returns The stripe plans. * @throws StripeNotConnected * -------------------------------------------------- */ public function updatePlans() { // Connecting to stripe, and making query. try { $decodedData = json_decode($this->loadJSON(\Stripe\Plan::all()), TRUE); } catch (\Stripe\Error\Authentication $e) { // Access token expired. Calling handler. $this->getNewAccessToken(); } // Getting the plans. $plans = []; foreach ($decodedData['data'] as $plan) { $new_plan = new StripePlan(array('plan_id' => $plan['id'], 'name' => $plan['name'], 'currency' => $plan['currency'], 'amount' => $plan['amount'], 'interval' => $plan['interval'], 'interval_count' => $plan['interval_count'])); $new_plan->user()->associate($this->user); array_push($plans, $new_plan); } // Delete old, save new. foreach (StripePlan::where('user_id', $this->user->id)->get() as $stripePlan) { StripeSubscription::where('plan_id', $stripePlan->id)->delete(); $stripePlan->delete(); } foreach ($plans as $plan) { $plan->save(); } return $plans; }