Summary
- Create a Subscription Plan.
- Make the /subscription/create call.
- Embed the resulting subscription_uri in an iframe so the payer can enter their payment details.
- Track the subscription payments.
Detailed Example
Step 1:
To create a subscription plan for one of your merchants, you can make the /subscription_plan/create call.
You generally only need to do this once. Multiple subscribers can all be using the same plan. So if you have "Gold" and "Silver" plans, you can have multiple subscribers who are subscribed to each plan. The amount each subscriber is charged is determined by the plan.
<?php
// WePay PHP SDK - http://git.io/mY7iQQ
require 'wepay.php';
// application settings
$account_id = 123456789;
$client_id = 123456789;
$client_secret = "1a3b5c7d9";
$access_token = "STAGE_8a19aff55b85a436dad5cd1386db1999437facb5914b494f4da5f206a56a5d20";
// change to useProduction for live environments
Wepay::useStaging($client_id, $client_secret);
$wepay = new WePay($access_token);
// create the subscription plan
$response = $wepay->request('subscription_plan/create', array(
'account_id' => $account_id,
'name' => 'gold',
'short_description' => 'The best plan with great perks',
'period' => 'monthly',
'amount' => 19.99
));
// display the response
print_r($response);
?>
curl https://stage.wepayapi.com/v2/subscription_plan/create \
-H "Authorization: Bearer STAGE_8a19aff55b85a436dad5cd1386db1999437facb5914b494f4da5f206a56a5d20" \
-d "account_id=123456789" \
-d "name=gold" \
-d "short_description=The best plan with great perks" \
-d "period=monthly" \
-d "amount=19.99"
# WePay Ruby SDK - http://git.io/a_c2uQ
require 'wepay'
# application settings
account_id = 123456789
client_id = 123456789
client_secret = '1a3b5c7d9'
access_token = 'STAGE_8a19aff55b85a436dad5cd1386db1999437facb5914b494f4da5f206a56a5d20'
# set _use_stage to false for live environments
wepay = WePay::Client.new(client_id, client_secret, _use_stage = true)
# create the subscription plan
response = wepay.call('/subscription_plan/create', access_token, {
:account_id => account_id,
:name => 'gold',
:short_description => 'The best plan with great perks',
:period => 'monthly',
:amount => 19.99
})
# display the response
p response
# WePay Python SDK - http://git.io/v7Y1jA
from wepay import WePay
# application settings
account_id = 123456789
access_token = 'STAGE_8a19aff55b85a436dad5cd1386db1999437facb5914b494f4da5f206a56a5d20'
production = False
# set production to True for live environments
wepay = WePay(production, access_token)
# create the subscription plan
response = wepay.call('/subscription_plan/create', {
'account_id': account_id,
'name': 'gold',
'short_description': 'The best plan with great perks',
'period': 'monthly',
'amount': 19.99
})
# display the response
print response
Step 2:
When someone wants to subscribe to one of your plans, make the /subscription/create call to get a subscription_id and subscription_uri.
<?php
// create the subscription
$response = $wepay->request('subscription/create', array(
'subscription_plan_id' => 12345,
'redirect_uri' => 'http://example.com/success/',
'mode' => 'iframe'
));
// display the response
print_r($response);
?>
curl https://stage.wepayapi.com/v2/subscription/create \
-H "Authorization: Bearer STAGE_8a19aff55b85a436dad5cd1386db1999437facb5914b494f4da5f206a56a5d20" \
-d "subscription_plan_id=12345" \
-d "name=http://example.com/success/" \
-d "mode=iframe"
# create the subscription
response = wepay.call('/subscription/create', access_token, {
:subscription_plan_id => 12345,
:redirect_uri => 'http://example.com/success/',
:mode => 'iframe'
})
# display the response
p response
# create the subscription
response = wepay.call('/subscription/create', {
'subscription_plan_id': 12345,
'name': 'http://example.com/success/',
'mode': 'iframe'
})
# display the response
print response
Step 3:
Embed the subscription_uri from step 3 into an iframe on your site so the payer can enter their payment details and confirm the subscription.
<div id="wepay_subscription_container"></div>
<script type="text/javascript" src="https://www.wepay.com/min/js/iframe.wepay.js"></script>
<script type="text/javascript">
WePay.iframe_checkout("wepay_subscription_container", "https://stage.wepay.com/api/subscription/12345");
</script>
Step 4:
Once the payer has completed the subscription flow, they will be automatically charged every period (monthly, yearly, etc - whatever you set in the subscription plan). You can view the status of the subscription in the app dashboard, or with the /subscription call. You can also sign up for IPNs for the subscription.
Summary
- Create a Subscription Plan.
- Tokenize the credit card using WePay's tokenization.js to obtain a credit_card_id.
- Make the /subscription/create call and pass the credit_card_id.
- Track the subscription payments.
Detailed Example
Step 1:
To create a subscription plan for one of your merchants, you can make the /subscription_plan/create call.
You generally only need to do this once. Multiple subscribers can all be using the same plan. So if you have "Gold" and "Silver" plans, you can have multiple subscribers who are subscribed to each plan. The amount each subscriber is charged is determined by the plan.
<?php
// WePay PHP SDK - http://git.io/mY7iQQ
require 'wepay.php';
// application settings
$account_id = 123456789;
$client_id = 123456789;
$client_secret = "1a3b5c7d9";
$access_token = "STAGE_8a19aff55b85a436dad5cd1386db1999437facb5914b494f4da5f206a56a5d20";
// change to useProduction for live environments
Wepay::useStaging($client_id, $client_secret);
$wepay = new WePay($access_token);
// create the subscription plan
$response = $wepay->request('subscription_plan/create', array(
'account_id' => $account_id,
'name' => 'gold',
'short_description' => 'The best plan with great perks',
'period' => 'monthly',
'amount' => 19.99
));
// display the response
print_r($response);
?>
curl https://stage.wepayapi.com/v2/subscription_plan/create \
-H "Authorization: Bearer STAGE_8a19aff55b85a436dad5cd1386db1999437facb5914b494f4da5f206a56a5d20" \
-d "account_id=123456789" \
-d "name=gold" \
-d "short_description=The best plan with great perks" \
-d "period=monthly" \
-d "amount=19.99"
# WePay Ruby SDK - http://git.io/a_c2uQ
require 'wepay'
# application settings
account_id = 123456789
client_id = 123456789
client_secret = '1a3b5c7d9'
access_token = 'STAGE_8a19aff55b85a436dad5cd1386db1999437facb5914b494f4da5f206a56a5d20'
# set _use_stage to false for live environments
wepay = WePay::Client.new(client_id, client_secret, _use_stage = true)
# create the subscription plan
response = wepay.call('/subscription_plan/create', access_token, {
:account_id => account_id,
:name => 'gold',
:short_description => 'The best plan with great perks',
:period => 'monthly',
:amount => 19.99
})
# display the response
p response
# WePay Python SDK - http://git.io/v7Y1jA
from wepay import WePay
# application settings
account_id = 123456789
access_token = 'STAGE_8a19aff55b85a436dad5cd1386db1999437facb5914b494f4da5f206a56a5d20'
production = False
# set production to True for live environments
wepay = WePay(production, access_token)
# create the subscription plan
response = wepay.call('/subscription_plan/create', {
'account_id': account_id,
'name': 'gold',
'short_description': 'The best plan with great perks',
'period': 'monthly',
'amount': 19.99
})
# display the response
print response
Step 2:
Tokenize the credit card using WePay's tokenization.js.
Follow the steps in the tokenization tutorial to get a credit_card_id that represents the payer's method of payment. You will use this credit_card_id in the next step to charge the credit card.
Step 3:
Make the /subscription/create call with the credit_card_id from step 2. This will automatically subscribe the payer to the subscription plan indicated.
<?php
// create the subscription
$response = $wepay->request('subscription/create', array(
'subscription_plan_id' => 12345,
'payment_method_id' => '123124124',
'payment_method_type' => 'credit_card'
));
// display the response
print_r($response);
?>
curl https://stage.wepayapi.com/v2/subscription/create \
-H "Authorization: Bearer STAGE_8a19aff55b85a436dad5cd1386db1999437facb5914b494f4da5f206a56a5d20" \
-d "subscription_plan_id=12345" \
-d "payment_method_id=12345" \
-d "payment_method_type=credit_card"
# create the subscription
response = wepay.call('/subscription/create', access_token, {
:subscription_plan_id => 12345,
:payment_method_id => 123124141,
:payment_method_type => 'credit_card'
})
# display the response
p response
# create the subscription
response = wepay.call('/subscription/create', {
'subscription_plan_id': 12345,
'payment_method_id': 1351341213,
'payment_method_type': 'credit_card'
})
# display the response
print response
Step 4:
Once you have created the subscription with the payer's credit_card_id, they will be automatically charged every period (monthly, yearly, etc - whatever you set in the subscription plan). You can view the status of the subcription in the app dashboard, or with the /subscription call. You can also sign up for IPNs for the subscription.