<commit_before><?php
/**
* @package janitor.shop
*/


require_once("includes/stripe-php-4.1.1/init.php");


class JanitorStripe {

	/**
	*
	*/
	function __construct() {

		// include stripe connection keys
		@include_once("config/connect_stripe.php");

		$this->stripe = array(
		  "secret_key"      => $stripe_keys["secret"],
		  "publishable_key" => $stripe_keys["public"]
		);

		\Stripe\Stripe::setApiKey($this->stripe['secret_key']);
	
	}


	// Process payment
	function processCardAndPayOrder($order, $card_number, $card_exp_month, $card_exp_year, $card_cvc) {

		if($order && $order["user"] && $order["user"]["email"]) {

			// does customer already exist in Stripe account
			$customer_id = $this->getCustomerId($order["user_id"]);

			// create customer, if it doesn't exist
			if(!$customer_id) {
				$customer_id = $this->createCustomer($order);
			}

			// customer created or updated
			if($customer_id) {

				// create token for card
				$token_id = $this->createToken($card_number, $card_exp_month, $card_exp_year, $card_cvc);
				if($token_id) {

					// Add card to customer
					$response = $this->addCard($customer_id, $token_id);
					if($response) {

						// Charge customer
						if($this->chargeCustomer($order, $customer_id)) {
							return true;
						}
					}
				}
			}
		}

		return false;
	}

	// Create customer in Stripe account
	function createToken($card_number, $card_exp_month, $card_exp_year, $card_cvc) {

		global $page;

		try {

			$token = \Stripe\Token::create(array(
			  "card" => array(
			    "number" => $card_number,
			    "exp_month" => $card_exp_month,
			    "exp_year" => $card_exp_year,
			    "cvc" => $card_cvc
			  )
			));

			if($token) {

				$page->addLog("Token created: brand:".$token->Brand.", last4:".$token->last4, "stripe");
				return $token->id;

			}

		}
		// Card error
		catch(\Stripe\Error\Card $exception) {

			$this->exceptionHandler("Creating token", $exception);
			return false;
		}
		// Too many requests made to the API too quickly
		catch (\Stripe\Error\RateLimit $exception) {

			$this->exceptionHandler("Creating token", $exception);
			return false;
		} 
		// Invalid parameters were supplied to Stripe's API
		catch (\Stripe\Error\InvalidRequest $exception) {

			$this->exceptionHandler("Creating token", $exception);
			return false;
		}
		// Authentication with Stripe's API failed
		catch (\Stripe\Error\Authentication $exception) {

			$this->exceptionHandler("Creating token", $exception);
			return false;
		}
		// Network communication with Stripe failed
		catch (\Stripe\Error\ApiConnection $exception) {<commit_msg>Updating logged info for stripe/create/card<commit_after><?php
/**
* @package janitor.shop
*/


require_once("includes/stripe-php-4.1.1/init.php");


class JanitorStripe {

	/**
	*
	*/
	function __construct() {

		// include stripe connection keys
		@include_once("config/connect_stripe.php");

		$this->stripe = array(
		  "secret_key"      => $stripe_keys["secret"],
		  "publishable_key" => $stripe_keys["public"]
		);

		\Stripe\Stripe::setApiKey($this->stripe['secret_key']);
	
	}


	// Process payment
	function processCardAndPayOrder($order, $card_number, $card_exp_month, $card_exp_year, $card_cvc) {

		if($order && $order["user"] && $order["user"]["email"]) {

			// does customer already exist in Stripe account
			$customer_id = $this->getCustomerId($order["user_id"]);

			// create customer, if it doesn't exist
			if(!$customer_id) {
				$customer_id = $this->createCustomer($order);
			}

			// customer created or updated
			if($customer_id) {

				// create token for card
				$token_id = $this->createToken($card_number, $card_exp_month, $card_exp_year, $card_cvc);
				if($token_id) {

					// Add card to customer
					$response = $this->addCard($customer_id, $token_id);
					if($response) {

						// Charge customer
						if($this->chargeCustomer($order, $customer_id)) {
							return true;
						}
					}
				}
			}
		}

		return false;
	}

	// Create customer in Stripe account
	function createToken($card_number, $card_exp_month, $card_exp_year, $card_cvc) {

		global $page;

		try {

			$token = \Stripe\Token::create(array(
			  "card" => array(
			    "number" => $card_number,
			    "exp_month" => $card_exp_month,
			    "exp_year" => $card_exp_year,
			    "cvc" => $card_cvc
			  )
			));

			if($token) {

				$page->addLog("Token created: brand:".$token->card->Brand.", last4:".$token->card->last4, "stripe");
				return $token->id;

			}

		}
		// Card error
		catch(\Stripe\Error\Card $exception) {

			$this->exceptionHandler("Creating token", $exception);
			return false;
		}
		// Too many requests made to the API too quickly
		catch (\Stripe\Error\RateLimit $exception) {

			$this->exceptionHandler("Creating token", $exception);
			return false;
		} 
		// Invalid parameters were supplied to Stripe's API
		catch (\Stripe\Error\InvalidRequest $exception) {

			$this->exceptionHandler("Creating token", $exception);
			return false;
		}
		// Authentication with Stripe's API failed
		catch (\Stripe\Error\Authentication $exception) {

			$this->exceptionHandler("Creating token", $exception);
			return false;
		}
		// Network communication with Stripe failed
		catch (\Stripe\Error\ApiConnection $exception) {
{'fraction_non_alphanumeric': 0.15547971179370496, 'fraction_numerical': 0.0018960940462646946, 'mean_word_length': 3.3703396851698426, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 49, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1556769', 'n_tokens_mistral': 2105, 'n_tokens_neox': 1729, 'n_words': 354}