<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS');
header('Access-Control-Allow-Headers: X-Requested-With, content-type, X-Token, x-token');
require_once('vendor/autoload.php');
\Stripe\Stripe::setApiKey("sk_live_ZiEp14EJoCJwGVhQXqOt1soN");

// Save the customer id in your own database!
// Charge the Customer instead of the card
// Subscribe the customer to the plan

try {

$subscription = \Stripe\Subscription::create(array(
    "customer" => $_POST['customerId'],
    "plan" => $_POST['plan'],
     "application_fee_percent" => 10,
    "metadata"=> array("pluginId" => $_POST['plugin'], "pluginTitle" => $_POST['pluginTitle'])
), array("stripe_account" => $_POST['stripeId']));

  // Use Stripe's library to make requests...
} catch(\Stripe\Error\Card $e) {
  // Since it's a decline, \Stripe\Error\Card will be caught
echo $e;
} catch (\Stripe\Error\RateLimit $e) {
  // Too many requests made to the API too quickly
    echo $e;
} catch (\Stripe\Error\InvalidRequest $e) {
  // Invalid parameters were supplied to Stripe's API
    echo $e;
} catch (\Stripe\Error\Authentication $e) {
  // Authentication with Stripe's API failed
  // (maybe you changed API keys recently)
    echo $e;
} catch (\Stripe\Error\ApiConnection $e) {
  // Network communication with Stripe failed
    echo $e;
} catch (\Stripe\Error\Base $e) {
  // Display a very generic error to the user, and maybe send
  // yourself an email
    echo $e;
} catch (Exception $e) {
  // Something else happened, completely unrelated to Stripe
    echo $e;
}


?>