stripe_checkout_button
Generate a button using Stripe Checkout features.
Attributes
test
– Puts the form in ‘testing’ mode. Forms in this mode use the Stripe test keys and do not make charges. The test credit card (cc=4242-4242-4242-4242, cvc=123) can be used to make transactions. Optionalemail
– Renders an additional input field to collect the user’s email address. Optionallabel
– Sets the trigger button’s text.name
– Sets the title of the payment form.description
– Sets the description of the payment form.address
– Configures the payment form to collect the address information. Optionalshipping
– Configures the payment form to collect the shipping address information. Optionalamount
– Sets the amount shown on the payment form. In the case of a single payment it sets the amount to be charged.plan
– The ID of the recurring plan that is being created. Optionalimg
– The URL to the image displayed on the payment form.url
– The URL the user is redirected to upon successful payment.name='value'
– All other attributes will be added to the data posted back to the server. Optional
Stripe Checkout Button Example
[stripe_checkout_button test="true" email="true" address=false amount="25" plan="test_9_99" name="Test Charge" description="This is a test" label="Pay ME!" other="My special data" img="http://yoursite.com/img/marketplace.png" url="http://yoursite.com/receipt"]
stripe_form_begin
Begin a stripe payment form.
Attributes
test
– Puts the form in ‘testing’ mode. Forms in this mode use the Stripe test keys and do not make charges. The test credit card (cc=4242-4242-4242, cvc=123) can be used to make transactions. Optionaldescription
– Overrides the description sent to stripe.com. If you want a custom description in your stripe.com pages, this will allow you to set one. Optional
stripe_form_end
End a stripe payment form.
Attributes
text
– The text on the form’s submit button. Optional – Defaults to “Submit”
The HTML or short codes between the stripe_form_begin
and the stripe_form_end
elements define the form.
[stripe_form_begin] [stripe_form_standard_amount] [stripe_form_end] [stripe_form_receipt]
Form In Test Mode With Custom Description
[stripe_form_begin test=true description='This sets the description on stripe.com'] [stripe_form_standard_amount short=true] [stripe_form_end] [stripe_form_receipt]
stripe_form_standard_amount
Create a standard fixed amount payment form.
Attributes
amount
– Sets the amount to be paid. If no amount is specified, the form allows the user to enter any amount. Optional
[stripe_form_begin] [stripe_form_standard_amount amount=9.99 short=true] [stripe_form_end] [stripe_form_receipt]
stripe_form_standard_plan
Create a standard recurring payment form.
You must first create a recurring payment plan in your Stripe admin panel. The following assumes you have created a plan called monthly_10
Attributes
All Plan Info Attributes
[stripe_form_begin] [stripe_form_standard_plan plan='monthly_10' short=true] [stripe_form_end] [stripe_form_receipt]
stripe_form_plan_initial
Add an initial fee (e.g. setup fee) to a recurring payment plan.
You must first create a recurring payment plan in your Stripe admin panel. The following assumes you have created a plan called monthly_10
Attributes
fee
– The value of the initial fee in dollars (e.g. 125.49).description
– A description for the initial fee. Optional
[stripe_form_begin] [stripe_form_standard_plan plan='monthly_10' short=true] [stripe_form_plan_initial fee='125.49' description='The setup fee.'] [stripe_form_end] [stripe_form_receipt]
stripe_form_receipt
Inserts the HTML used to generate the receipt displayed on the web page after a successful payment.
Attributes
None
Content
To use the default receipt, no content is necessary. Just include the tag as shown in the top example.
To create a custom receipt provide the HTML content between the opening and closing stripe_form_receipt
elements. Custom elements can be accessed by using the {element name}
notation.
The following elements are available by default:
id
– The transaction ID generated by Stripe. (e.g. “ch_dTtcAht0MS3EXD”)amount
– The amount charged to the card. (e.g. “20.00”)cancel
– An indicator if the transaction was cancelled. (e.g. true or false)card_last4
– The last 4 digits from the credit card. (e.g. “4242”)card_type
– The card type. (e.g. “Visa”)email, fname, lname
– The information collected by the short billing form. (e.g. “test@example.com”, “Bob”, “Cravens”)address1, address2, city, state, zip, country, phone
– The information collected by the long billing form.mode
– The Stripe API keys used. (e.g. “test” or “live”)pubkey
– The Stripe public key used. (e.g. “either your test or live public key”)recurring
– An indicator of a fixed amount (false) or a recurring payment (true). (e.g. false or true).success
– An indicator of issues. (e.g. true or false)token
– The token returned by Stripe that represents the sensitive credit card information. (e.g. “tok_VEEcj8n722JOlm”)download_url
(only available if using Pay Per Download Plugin) – The URL that can be used to download the digital media.
[stripe_form_begin] [stripe_form_standard_plan plan='monthly_10' short=true] [stripe_form_end] [stripe_form_receipt]
Payment Form Using Custom Web Receipt
[stripe_form_begin test=true] [stripe_form_standard_plan plan='monthly_10' short=true] [stripe_form_section_header title="SWAG Section"] [stripe_form_section_row label="t-shirt size"] <select name='tshirt'> <option value='S'>small</option> <option value='M'>medium</option> <option value='L'>large</option> </select> [/stripe_form_section_row] [stripe_form_end] [stripe_form_receipt] <p><strong>Thank You, {fname} {lname}</strong></h4> <p><strong>$ {amount} is making its way to our bank account.</strong></p> <p>A receipt has been sent to <strong>{email}</strong>.</p> <p>Transaction ID: {id}</p> <p>T-SHIRT SIZE: {tshirt}</p> [/stripe_form_receipt]
stripe_form_redirect
Overrides the web receipt by redirecting (HTTP POST) to another page with the all the payment information included as POST variables. This allows custom receipt pages.
Attributes
url
– Use this to set the URL for the redirect.
[stripe_form_begin test=true] [stripe_form_redirect url='http://yoursite.com/receipt'] [stripe_form_standard_plan plan='monthly_10' short=true] [stripe_form_end] [stripe_form_receipt]
stripe_form_data
Provides shortcode access to the POST data from a redirected receipt (e.g. using [stripe_form_redirect]
). During development of the page use the following to emit all the available name/value pair data: [stripe_form_data name="dump_all"]
Attributes
name
– The name of the data to add to the form.
<h1>Thanks for the payment!</h1> <p style="margin:0 20px;"> <table class="stripe-payment-table"> <tr><td>Name</td><td><code>[stripe_form_data name='fname'] [stripe_form_data name='lname']</code></td></tr> <tr><td>Amount ($)</td><td><code>[stripe_form_data name='subtotal']</code></td></tr> <tr><td>Tax ($)</td><td><code>[stripe_form_data name='tax']</code></td></tr> <tr><td>Total ($)</td><td><code>[stripe_form_data name='amount']</code></td></tr> </table> </p> <p><strong>$ [stripe_form_data name='amount'] is making its way to our bank account.</strong></p> <p>A receipt has been sent to <strong>[stripe_form_data name='email']</strong>.</p> <p>Transaction ID: [stripe_form_data name='id']</p> <p><a href="[stripe_form_data name='download_url']">Download Link</a></p>
stripe_form_section_header
Creates a new section in the payment form.
Attributes
title
– The title of the new payment section.
stripe_form_section_row
Creates a row the current payment section. A section can have multiple rows.
Attributes
label
– The row label HTML.input
– The row input HTML.
[stripe_form_begin test=true] [stripe_form_standard_plan plan='monthly_10' short=true] [stripe_form_section_header title="SWAG Section"] [stripe_form_section_row label="t-shirt size"] <select name='tshirt'> <option value='S'>small</option> <option value='M'>medium</option> <option value='L'>large</option> </select> [/stripe_form_section_row] [stripe_form_end] [stripe_form_receipt] <p><strong>Thank You, {fname} {lname}</strong></h4> <p><strong>$ {amount} is making its way to our bank account.</strong></p> <p>A receipt has been sent to <strong>{email}</strong>.</p> <p>Transaction ID: {id}</p> <p>T-SHIRT SIZE: {tshirt}</p> [/stripe_form_receipt]
stripe_form_coupon
Creates a field to enter a coupon code. Note: Coupons are supported by Stripe only on recurring payment plans. The plugin adds support to single payments, by honoring the percent set on the coupon for single payments also.
Attributes
code
– Use this to provide a pre-entered code. Otherwise do not set.
[stripe_form_begin test=true] [stripe_form_standard_plan plan='monthly_10' short=true] [stripe_form_coupon] [stripe_form_end] [stripe_form_receipt] <p><strong>Thank You, {fname} {lname}</strong></h4> <p><strong>$ {amount} is making its way to our bank account.</strong></p> <p>A receipt has been sent to <strong>{email}</strong>.</p> <p>Transaction ID: {id}</p> <p>T-SHIRT SIZE: {tshirt}</p> [/stripe_form_receipt]
Instead of using one of the standard forms, you can customize your payment forms using a combination of the following short codes.
stripe_form_amount
Creates a section in the form to collect a fixed payment amount.
Attributes
amount
– Sets the amount to be paid. If no amount is specified, the form allows the user to enter any amount. Optional
stripe_form_no_amount
Creates a payment form that collected billing information but does not charge any amount. This allows for future billing to occur.
Attributes
None
stripe_form_plan_info
Creates a section in the form to collect a recurring payment.
Attributes
plan
– The ID of a planplan='monthyly_10'
or a comma separated list of plansplan='monthly_10,yearly_100'
that exists in your Stripe account. If this attribute is not provided, the payment form will be generated as a fixed amount with the attribute not specified. The keywordother
as a plan name (either single or in the list) causes the plugin to dynamically create plans. more info
stripe_form_billing_info
Creates a section to collect billing information.
Attributes
short
– If present, generates a form that collects the minimum billing information (first name, last name, email). Optionalmedium
– If present, generates a form that collects country and state in addition to the minimum billing information (first name, last name, email). Otherwise a longer form is presented (first name, last name, email, address1, address2, city, state, zip/postal code, country, phone). Optionallong
– If present, generates a form that collects the most information (first name, last name, email, address1, address2, city, state, zip/postal code, country, phone). Optional. Note if ‘short’, ‘medium’ and ‘long’ are not present the form defaults to ‘long’country
– Set the default country. This should be set to the 2 character ISO value for the country. Check the drop-down for the value to use. Optionalstate
– Set the default state. This should be set to the abbreviation used for the state, province or territory. Check the drop-down for the value to use. Optional
stripe_form_payment_info
Creates a section to collect credit card information.
Attributes
None
[stripe_form_begin test=true] [stripe_form_plan_info plan="test_10_per_month"] [stripe_form_section_header title="SWAG Section"] [stripe_form_section_row label="t-shirt size"] <select name='tshirt'> <option value='S'>small</option> <option value='M'>medium</option> <option value='L'>large</option> </select> [/stripe_form_section_row] [stripe_form_billing_info medium=true country='US' state='WI'] [stripe_form_payment_info] [stripe_form_end] [stripe_form_receipt] <p><strong>Thank You, {fname} {lname}</strong></h4> <p><strong>$ {amount} is making its way to our bank account.</strong></p> <p>A receipt has been sent to <strong>{email}</strong>.</p> <p>Transaction ID: {id}</p> <p>T-SHIRT SIZE: {tshirt}</p> [/stripe_form_receipt]
function stripe_email_body( $data )
This function allows you to completely customize the email. This provides the most power, but requires the most work. This function is typically placed in the functions.php
file of your theme.
Parameters
$data
– An array of available form fields from the transaction (see the previous section onstripe_form_receipt
shortcode for what is available).
Return Value
This function should return the HTML body for the email.
function stripe_email_body( $data ) { // Set the title, message and footer // $title = 'Your Email Title'; $msg = 'This is the message that is shown just above the table.'; $footer = 'My Company'; // Build the email content. // $table_style = 'width: 80%; margin: 20px auto; border: 1px solid #666;'; $header_style = 'padding: 25px 10px 5px; font-weight: bold; font-size: 16px; text-transform: uppercase; background-color: #f2f2f2;'; $left_style = 'text-align: right; white-space: nowrap; font-weight: bold;'; $right_style = 'width: 99%;'; $body = <<<MSG <html> <body> <div style="padding:10px;background-color:#f2f2f2;"> <div style="padding:10px;border:1px solid #eee;background-color:#fff;"> <h2>$title</h2> <div style="margin:10px;"> $msg </div> <table rules="all" style='$table_style' cellpadding="10"> <!--table rows--> </table> $footer </div> </div> </body> </html> MSG; // Dynamically create the rows of the table // $rows = ""; if( !is_null( $data ) ) { foreach( $data as $key => $val ) { if( $val == "section" ) { $rows .= "<tr><td style='$header_style' colspan=2>$key</td></tr>"; } else { $rows .= "<tr><td style='$left_style'>" . $key . "</td><td style='$right_style''>" . $val . "</td></tr>"; } } } $body = str_replace( "<!--table rows-->", $rows, $body ); return $body; }
function stripe_email_footer( $data )
This function allows you insert a customer footer into the default email. You can use this to add a logo for example. This function is typically placed in the functions.php
file of your theme.
Parameters
$data
– An array of available form fields from the transaction (see the previous section onstripe_form_receipt
shortcode for what is available).
Return Value
This function should return the HTML footer for the email.
function stripe_email_footer( $data ) { return <<<FOOT <div style="text-align:center;"> <img src="https://yoursite.com/static/logo.gif" alt="logo" style="width:100px;margin:10px 0 0 0;" /> <p style="margin:0;padding:0;"><strong>Your Company</strong></p> <p style="margin:0;padding:0;">P.O. Box 12345</p> <p style="margin:0;padding:0;">City, State Zip</p> <p style="margin:0;padding:0;">Phone Number</p> </div> FOOT; }
function stripe_email_before_send( &$to, &$subject, &$body, &$headers )
This function allows you one final opportunity to modify the email. This function has all the parameters passed as references so you simple modify the value. For example, you can modify the default (out-of-the-box) subject of the email. Here, you can also modify the email headers (e.g. cc, bcc, reply-to). This function is typically placed in the functions.php
file of your theme.
Parameters
$to
– The ‘to’ address passed by reference.$subject
– The ‘subject’ passed by reference.$body
– The ‘body’ passed by reference.$headers
– The ’email headers’ passed by reference.
Return Value
boolean
– true
to send the email, false
to cancel sending the email.
function stripe_email_before_send(&$to, &$subject, &$body, &$headers) { // Prefix the subject with your company name. // $subject = 'Your Company Name - '.$subject; // Alter the header here if you want // to always add a From, ReplyTo, CC, BCC or other. // $headers .= "From: Support <support@example.com>\r\n"; $headers .= "Reply-To: admin@example.com\r\n"; $headers .= "Cc: person1@company1.org, person2@company2.com\r\n"; $headers .= "Bcc: person3@company3.com, person4@company4.com\r\n"; // return false to cancel sending the email return true; }
- The user fills out a payment form and clicks the submit button.
- An AJAX post (includes credit card info) is sent to securely to Stripe’s API via HTTPS where the card is authorized for the amount. If the card is authorized a token is returned in the response. Otherwise, payment fails and the user is notified.
- An AJAX post (the token is sent in lieu of the credit card info) containing all the other form data is made to your site.
- The plugin’s AJAX handler processes the request. This processing calls any registered ‘begin’ (see below) callbacks. Any of the ‘begin’ callbacks can cancel the operations.
- The plugin’s AJAX handler then uses the Stripe APIs to securely (HTTP) charge the credit card or to setup a recurring payment plan.
- The plugin’s AJAX handler then calls any registered ‘end’ (see below) callbacks.
- The plugin’s AJAX handler then returns a response object (JSON) back to the web page. This information is used to dynamically create the receipt.
Plugin’s Webhook URL = payment-webhook
Stripe’s Webhook URL = http://yoursite.com/payment-webhook/
Notice the trailing slash in the Stripe’s webhook version. The current version of the plugin is requires this slash.
stripe_register_payment_begin_callback( 'begin_callback' );
stripe_register_payment_end_callback( 'end_callback' );
if(function_exists('stripe_register_payment_begin_callback')) { stripe_register_payment_begin_callback('my_payment_begin_callback'); } function my_payment_begin_callback( $response ) { // Do some preprocessing here. // $product = $response[ 'product' ]; if( !is_null( $product ) ) { $expectedPrice = Db::GetPrice( $product ); $payedAmount = floatval( $response[ 'amount' ] ); if( $expectedPrice != $payedAmount ) { // Don't charge, something is wrong. // $response[ 'cancel' ] = true; $response[ 'error' ] = 'Error message shown to the the user.'; } } else { // No valid product...cancel the transaction. // $response[ 'cancel' ] = true; $response[ 'error' ] = 'The error message that will be shown to the user.'; } }
End Example
if(function_exists('stripe_register_payment_end_callback')) { stripe_register_payment_end_callback('my_payment_end_callback'); } function my_payment_end_callback( $response ) { // Email the transaction to the admin // $to = 'admin@yoursite.com'; $subject = 'Transaction Summary'; $email = new DigLabs_Stripe_Helpers_Email(); $body = $email->createBody("Transaction Summary", "This is a summary of a <a href='http://stripe.com'>Stripe</a> transaction.", $response); $email->sendEmail($to, $subject, $body); // This is a custom handler for custom form data // $product = $response['product']; if( !is_null( $product ) ) { // Get the product data from a DB. // $pinfo = Db::GetInfo( $product ); // Include this in the response. // $response[ 'pinfo' ] = $pinfo; } }
Action: stripe_invoice_created_notification
This action gets raised when Stripe sends a notification event with type of invoice.created
. This allows additional items to be added to the invoice (e.g. usage based payments).
StripeInvoice::AddItem($event, $amount, $description)
This is a helper method to be used in conjunction with the stripe_invoice_created_notification
action.
Parameters
$event
– Event received from the action.$amount
– Amount of the payment to add to the invoice.$description
– Description of the new line item. optional
add_action('stripe_invoice_created_notification', 'my_invoice_created_hook', 10, 1); function my_invoice_created_hook($event) { try { StripeInvoice::AddItem($event, 123, "One time fee"); } catch( Exception $e) { // Logging...etc. } }
Action: stripe_payment_notification
This action gets raised when Stripe sends a notification event with type of charge.succeeded
or invoice.payment_failed
. This allows additional processing and customization of the payment workflow.
add_action('stripe_payment_notification', 'my_payment_notification', 10, 1); function my_payment_notification($data) { try { // Do your additional processing here. var_dump($data); } catch( Exception $e) { // Logging...etc. } }