Download the PHP package henshall/php-stripe-wrapper without Composer
On this page you can find all versions of the php package henshall/php-stripe-wrapper. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download henshall/php-stripe-wrapper
More information about henshall/php-stripe-wrapper
Files in henshall/php-stripe-wrapper
Package php-stripe-wrapper
Short Description Simple Stripe wrapper used to make purchases, subscription, and distribute money wbetween accounts with Stripe Connect.
License
Informations about the package php-stripe-wrapper
Stripe Php Wrapper
Version: 1.0.0
This is a wrapper for the Stripe PHP Package 'stripe/stripe-php'.
I wanted to created this package to help simplify payment processing with Stripe - and help developers quickly start using stripe with lots of examples. Although this package is mostly a simple wrapper, it has a unique way of handling errors (important), processing web-hooks, working with stripe connect, and has documentation to provide lots of examples on how to use it.
Installation with Composer:
Pre-requisites
- Create a Stripe account (https://stripe.com/)
- Get your Public and Secret key's in the developers section.
Usage:
To process payments with stripe we need two things, to collect the credit card (and other) information from the customers on a front end HTML form, and then to process this information and send it to stripe on the back end. I have divided the usage section into 1) front end examples and 2) back end examples. You need both to process payments (unless you want to charge your pre-existing stripe customers).
Front End:
Create a form on your HTML page like so. Here you will capture customer information for later use. Make sure to include your public stripe key. in the data-key section, and the data-amount should be in cents (1/100) of the value of your currency. ex. 1000 USD = $10.
Front End Example 1:
Use this to create a simple pop-up form.
Front End Example 2:
Use this to create an inline form with bootstrap. When copying make sure to change the scrf token and insert your stripe public key.
Back End:
Back End Notes: you can see in the examples that the form will send a post request to /pay. Before it hits this location, it will send a request to stripe servers with the information and return a token (["stripeToken"]). We can use this token on the back end to process events like customer creation and payments.
Please see below for some example of what you can do with the token.
Charge an anonymous person (one time charge).
Use this to charge customers when you don't need to collect information such as their address, or instructions regarding the product/service.
Create a customer and then charge that customer (one time charge)
Use this when you want to collect information about a customer. We will create a customer object in stripe, and then charge the customer after.
Charge an existing customer (one time charge).
Use this when you already have a customer - ei, you want to charge an existing customer with one one time fee. Here we need to retrieve a customer from stripe.
Create Subscriptions and charge new customer (subscription)
To create subscriptions with stripe, we first need to create a customer in stripe as well as a plan. We need both to create a subscription. Its possible to use existing customers and plans you have previously created but here we will create a new plan, and a new customer, and use them to create a subscription.
1) Create a plan
Option A) Login into Strip and create a plan manually under the billing/products tab.
Option B) Run the following code only one time to create a plan we will continue to use the plan for all future subscriptions.
2) Create subscription
Once the plan is set up, we can create a customer and use them to create the subscription.
Create Subscriptions and charge existing customer (subscription)
Here we will assume that you have already have a customer and a plan set up. We will use them to create a subscription. Use this if you need to charge an existing customer.
Create Subscriptions without retrieving data from stripe (subscription)
Here we can condense our code down to just three lines since we already have the customer_id and the plan id.
Cancel a Subscription.
To cancel a subscription we need to retreive the subscription object from stripe, and then use the cancel method.
Delete a Customer.
To delete a customer we need to retrieve the customer object from stripe, and then use the delete method. (here we are passing in the entire customer object, but you can also just pass in the customer id)
Process Webhooks
Stripe webhooks can difficult to configure - so I wanted to include a few functions to help users process their webhooks.
The following code will provide you with a $webhook_data object you can parse. There is no need to receive parameters from the post request because the @file_get_contents("php://input") function will return raw data from the request body that stripe sends us.
Create Webhook
You can create a webhook with the following code:
Retreive Webhook
You can retreive a webhook with the following code:
Stripe Tips
Lets say that once a user has a successful payment, you want to store the subscription in the database and also update the user record.
The updating of the database should come first, and then credit card processing second. This is because all of the logic from the database can be reversed and even captured, while the credit card processing cannot easily be reversed (5 business days for refunds).
Its a good idea to always use try/catch in anything that comes before or after your payments so that you can capture any errors. You don't want to be having problems processing payments and not know why.
Your logic should look something like this: