Stripe is the beautiful payment gateway which allow you to accept payments from your customers easily and securely. Stripe also allows to transfer payments from bank to bank. We can create recurring payments through the Stripe. We can create subscriptions for the users through the Stripe API. You can do adaptive payments through the Stripe. For example If customer pat $100 than you can split this payment to multiple users like you can give 30% to cab driver and rest for the Admin.
In this article I'm going to explain followings:
- Card or User registration on stripe.
- Create charge on card or Accept payment from card.
- Create Subscription plan on Stripe.
- Subscribe a particular plan.
- Cancel subscription plan.
- Register bank account on Stripe.
- Transfer payment from card to Bank Account.
Currently service for transferring payments from Bank account to Bank account is available in USA only.
Here are following steps for doing all these things.
1) Install laravel.
2)Add Stripe library
Now you need to install php stripe library through the composer like if you have installed composer globally then
1
|
composer require stripe/stripe-php
|
Or if you have installed composer locally then you can install with below command:
1
|
php composer.phar require stripe/stripe-php
|
View: welcome.blade
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
<script type="text/javascript" src="https://js.stripe.com/v2/"></script>
<script src="jquery-3.1.1.min.js"></script>
<script type="text/javascript">
Stripe.setPublishableKey('pk_test_YpMAPxMrg1RuC1Ph8Ol9ajOg');$(function() {
var $form = $('#payment-form');
$form.submit(function(event) {
// Disable the submit button to prevent repeated clicks:
$form.find('.submit').prop('disabled', true);// Request a token from Stripe:
Stripe.card.createToken($form, stripeResponseHandler);// Prevent the form from being submitted:
return false;
});
});function stripeResponseHandler(status, response) {
// Grab the form:
var $form = $('#payment-form');if (response.error) { // Problem!// Show the errors on the form:
$form.find('.payment-errors').text(response.error.message);
$form.find('.submit').prop('disabled', false); // Re-enable submission
} else { // Token was created!
// Get the token ID:
var token = response.id;
// Insert the token ID into the form so it gets submitted to the server:
$form.append($('<input type="hidden" name="stripeToken">').val(token));
// Submit the form:
$form.get(0).submit();
}
};
</script>
<form action="{{url('register')}}" method="POST" id="payment-form">
{!! csrf_field() !!}
<span class="payment-errors"></span>
<div class="form-row">
<label>
<span>Email</span>
<input type="text" size="20" name="email">
</label>
</div>
<div class="form-row">
<label>
<span>Card Number</span>
<input type="text" size="20" data-stripe="number">
</label>
</div>
<div class="form-row">
<label>
<span>Expiration (MM/YY)</span>
<input type="text" size="2" data-stripe="exp_month">
</label>
<span> / </span>
<input type="text" size="2" data-stripe="exp_year">
</div>
<div class="form-row">
<label>
<span>CVC</span>
<input type="text" size="4" data-stripe="cvc">
</label>
</div>
<input type="submit" class="submit" value="Submit Payment">
</form>
|
Here in this view there is a Stripe form when you enter the card details and your email id It will create the Stripe token and that token will be posted on the server side.
Controller: StripeController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
|
<?php
namespace App\Http\Controllers;
use Illuminate\Support\Facades\Log;
use App\Http\Controllers\Controller;
class StripeController extends Controller {
public function __construct() {
}
public function registerUser() {
$input = \Illuminate\Support\Facades\Input::all();
echo $customerId = $this->registerUserOnStripe($input['email'], $input['stripeToken']); /// here we are getting customer id given by stripe
$chargeDetails = $this->createCharge($customerId);
dd($chargeDetails);
}
// function to register user on stripe
public function registerUserOnStripe($email, $stripeToken) {
try {
$stripe = new \Stripe\Stripe();
$stripe->setApiKey(env('STRIPE_SECRET')); // secret key provided by stripe
$customer = \Stripe\Customer::create(array(
'email' => $email, // customer email id
'source' => $stripeToken // stripe token generated by stripe.js
));
if (!empty($customer) && !empty($customer["id"])) {
return $customer->id;
} else {
return $this->response->responseServerError(Lang::get('User::messages.gateway_error'));
}
} catch (\Exception $e) {
Log::error("PaymentContractImpl::registerUserOnStripe() there is some exception " . $e->getMessage());
return false;
}
}
// function to create charge on stripe
public function createCharge($customerId) {
$stripe = new \Stripe\Stripe();
$stripe->setApiKey(env('STRIPE_SECRET'));
$charge = \Stripe\Charge::create(array(
'customer' => $customerId,
'amount' => 9000,
'currency' => 'AUD'
));
dd($charge);
}
// function to create subscription plan
public function createSubscribePlan() {
$stripe = new \Stripe\Stripe();
$stripe->setApiKey(env('STRIPE_SECRET'));
$plan = \Stripe\Plan::create(array(
"name" => "Premium Plan",
"id" => "prmium-monthly",
"interval" => "month",
"currency" => "AUD",
"amount" => 269.50,
));
dd($plan);
}
// function to subscribe the plan
public function subscribePlan() {
$stripe = new \Stripe\Stripe();
$stripe->setApiKey(env('STRIPE_SECRET'));
$subscription = \Stripe\Subscription::create(array(
"customer" => 'cus_9tkkqiwzyzqMO2',
"plan" => "1",
));
dd($subscription);
}
public function cancelSubscription($stripeSubscriptionId){
$stripe = new \Stripe\Stripe();
$stripe->setApiKey(env('STRIPE_SECRET'));
$subscription = \Stripe\Subscription::retrieve($stripeSubscriptionId);
$result = $subscription->cancel();
}
// function to register bank account on stripe
public function registerBankAccountOnStripe($bankDetails) {
$stripe = new \Stripe\Stripe();
$stripe->setApiKey(env('STRIPE_SECRET'));
$acct = \Stripe\Account::create(array(
"managed" => true,
"country" => 'AU',
"external_account" => array(
"object" => "bank_account",
"country" => 'AU',
"currency" =>'AUD',
"routing_number" => $bankDetails['routingNo'],
"account_number" => $bankDetails['accountNo']
),
"tos_acceptance" => array(
"date" => 1474391808,
"ip" => "150.242.174.243"
)
));
$bankId = $accountDetails->external_accounts->data[0]['id'];
echo $accountId = $accountDetails->id;
}
// function to pay to a user from a card or adaptive payments
public function payToUserFromCard(){
$stripe = new \Stripe\Stripe();
$stripe->setApiKey(env('STRIPE_SECRET'));
$chargeDetails = \Stripe\Charge::create(array(
"amount" => 10 * 100, // $10 amount in cents
"currency" => 'AUD',
"description" => "Admin paid to user",
"source" => 'card_19Gj4RBwZe16jyt5r2xa5Gx4', //card token of customer
"customer" => 'cus_9ZsvlpHNN6sMQ2', // customer who is paying
"application_fee" => 0, // amount in cents
'destination' => $stripe_account_id, // account id given by stripe after register bank details
'capture' => TRUE
));
}
}
|
Route: routes.php
1
2
3
4
5
|
Route::get('/', function () {
return view('welcome');
});Route::post('register','StripeController@registerUser');
Route::get('create-plan','StripeController@createSubscribePlan');
Route::get('subscribe-plan','StripeController@subscribePlan');
|
You also can register the bank account through the token so we would not require to send bank details to our server. It is recommended to send the any confidential detail like bank detail or card detail to send to Stripe server and Stripe will return the token then you can send that token to you server and do the necessary operations.
Here is the article to register the Bank Account through the Stripe Token.