Created
February 2, 2019 16:55
-
-
Save jgmuchiri/d328e3c45127af775e5aa7c6cb28aa62 to your computer and use it in GitHub Desktop.
PHP Stripe gateway
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"stripe/stripe-php": "^6.8", | |
"phpmailer/phpmailer": "^6.0", | |
"symfony/polyfill-mbstring": "^1.8" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once('vendor/autoload.php'); | |
define('ENV','dev'); //dev, live | |
define('STRIPE_TEST_PK',''); | |
define('STRIPE_TEST_SK',''); | |
define('STRIPE_LIVE_PK',''); | |
define('STRIPE_LIVE_SK',''); | |
class StripeGateway | |
{ | |
/** | |
* Enviroment | |
* | |
* const ENV = development or production | |
*/ | |
const ENV = 'development'; | |
private $stripe_test_sk = STRIPE_TEST_SK; | |
private $stripe_live_sk = STRIPE_LIVE_SK; | |
public function __construct() | |
{ | |
\Stripe\Stripe::setApiKey(self::ENV == 'development' ? $this->stripe_test_sk : $this->stripe_live_sk); | |
} | |
public function charge() | |
{ | |
$error = ''; | |
try { | |
if(isset($_POST['stripeToken']) && isset($_POST['amount']) && isset($_POST['currency'])) { | |
$token = $_POST['stripeToken']; | |
$amount = $_POST['amount']; | |
$currency = $_POST['currency']; | |
$charge = \Stripe\Charge::create(['amount' => $amount, 'currency' => $currency, 'source' => $token]); | |
} | |
} catch (\Stripe\Error\Card $e) { | |
$error = $e->getMessage(); | |
} catch (\Stripe\Error\InvalidRequest $e) { | |
$error = $e->getMessage(); | |
} catch (\Stripe\Error\Authentication $e) { | |
$error = $e->getMessage(); | |
} catch (\Stripe\Error\ApiConnection $e) { | |
$error = $e->getMessage(); | |
} catch (\Stripe\Error\Base $e) { | |
$error = $e->getMessage(); | |
} catch (Exception $e) { | |
$error = $e->getMessage(); | |
} | |
if($error !== "") | |
return ['status' => 'error', 'message' => $error]; | |
//return charge object | |
return $charge; | |
} | |
public function createCustomer() | |
{ | |
$error = ''; | |
try { | |
if(isset($_POST['stripeToken']) && isset($_POST['stripeEmail'])) { | |
$token = $_POST['stripeToken']; | |
$email = $_POST['stripeEmail']; | |
$description = $_POST['description']; | |
$customer = \Stripe\Customer::create([ | |
"description" => $description, | |
"email" => $email, | |
"source" => $token, | |
]); | |
} | |
} catch (\Stripe\Error\Card $e) { | |
$error = $e->getMessage(); | |
} catch (\Stripe\Error\InvalidRequest $e) { | |
$error = $e->getMessage(); | |
} catch (\Stripe\Error\Authentication $e) { | |
$error = $e->getMessage(); | |
} catch (\Stripe\Error\ApiConnection $e) { | |
$error = $e->getMessage(); | |
} catch (\Stripe\Error\Base $e) { | |
$error = $e->getMessage(); | |
} catch (Exception $e) { | |
$error = $e->getMessage(); | |
} | |
if($error !== "") | |
return ['status' => 'error', 'message' => $error]; | |
//return customer object | |
return $customer; | |
} | |
public function getCustomer($customer = "") { } | |
public function subscribe($customer, $plan) | |
{ | |
$error = ''; | |
try { | |
$subscription = \Stripe\Subscription::create([ | |
"customer" => $customer->id, | |
"items" => [ | |
[ | |
"plan" => $plan, | |
], | |
], | |
]); | |
} catch (\Stripe\Error\InvalidRequest $e) { | |
$error = $e->getMessage(); | |
} catch (\Stripe\Error\Authentication $e) { | |
$error = $e->getMessage(); | |
} catch (\Stripe\Error\ApiConnection $e) { | |
$error = $e->getMessage(); | |
} catch (\Stripe\Error\Base $e) { | |
$error = $e->getMessage(); | |
} catch (Exception $e) { | |
$error = $e->getMessage(); | |
} | |
if($error !== "") | |
return ['status' => 'error', 'message' => $error]; | |
//return subscription object | |
return $subscription; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" integrity="sha384-WskhaSGFgHYWDcbwN70/dfYBj47jz9qbsMId/iRN3ewGhXQFZCSftd1LZCfmhktB" crossorigin="anonymous"> | |
</head> | |
<bodY> | |
<div class="container"> | |
<div class="row"> | |
<form method="POST" action="charge.php"> | |
<label>Firstname</label> | |
<input type="text" name="fname" class="form-control"> | |
<label>Lastname</label> | |
<input type="text" name="lname" class="form-control"> | |
<script src="https://checkout.stripe.com/checkout.js" | |
class="stripe-button" data-key="" | |
data-name="Emma's Farm CSA" data-description="Subscription for 1 weekly box" data-amount="2000" data-label="Sign Me Up!"> | |
// <![CDATA[// ]]></script> | |
</form> | |
<h2>Subscribe</h2> | |
<form method="POST" action=""> | |
<script src="https://checkout.stripe.com/checkout.js" | |
class="stripe-button" data-key="" | |
data-name="Emma's Farm CSA" data-description="Subscription for 1 weekly box" data-amount="2000" data-label="Sign Me Up!"> | |
// <![CDATA[// ]]></script> | |
</form> | |
</div> | |
</div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment