|
1 | | -# Subscription payments |
| 1 | +# Subscription Payments |
2 | 2 |
|
3 | | -It is possible to create long term subscription for your customers. The setup for subsription is done through regular endpoint for payment creation, but with the subscription properties set. The payment created can then be repeatedly paid by the customer, through a different endpoint. |
| 3 | +Subscription payments allow you to charge a customer repeatedly over time. |
| 4 | +A subscription is initialized through a standard payment creation request, but with subscription properties attached. |
| 5 | +Once the initial (parent) payment is completed, follow-up (child) payments can be realized using dedicated subscription endpoints. |
4 | 6 |
|
5 | | -There are three different use cases with different endpoints for subscription payments, depending on payment time and payment amount needs. |
6 | | -* Fixed time interval & fixed payment amount |
7 | | -* Variable time interval & fixed payment amount |
8 | | -* Fixed time interval & variable payment amount |
| 7 | +There are three main subscription types, depending on whether the time interval and payment amount are fixed or variable: |
| 8 | +- Fixed time interval & fixed amount |
| 9 | +- Variable time interval & fixed amount |
| 10 | +- Fixed time interval & variable amount |
9 | 11 |
|
10 | | -The time interval or amount with fixed value is always set in the first (parent) payment upon payment creation. It is your responsibility to adhere to the values set in the parent payment. If you need to change the fixed payment amount or time period, a new subscription payment with different parameters must be used. |
| 12 | +Any value marked as fixed must be set in the parent payment, and it is your responsibility to adhere to it when creating child payments. |
| 13 | +If your subscription requires a different amount or interval, you must create a new parent subscription payment. |
11 | 14 |
|
12 | | -**RECURRING PAYMENTS MUST BE ENABLED ON PROJECT** |
| 15 | +⚠️ **Important / Prerequisites** |
| 16 | +- **Recurring payments must be enabled on your project**. |
13 | 17 |
|
14 | | -## Creating payment with subscription properties |
| 18 | + |
| 19 | +## Creating a Payment with Subscription Properties |
15 | 20 |
|
16 | 21 | ```php |
17 | 22 | /** @var \ThePay\ApiClient\TheClient $thePayClient */ |
18 | 23 |
|
19 | | -// prepare subscription properties (first parameter is type, second parameter is day period between payments) |
| 24 | +// Prepare subscription properties: |
| 25 | +// First parameter: subscription type |
| 26 | +// Second parameter: number of days between payments (for fixed interval types) |
20 | 27 | $subscription = new \ThePay\ApiClient\Model\Subscription( |
21 | 28 | \ThePay\ApiClient\ValueObject\SubscriptionType::REGULAR, |
22 | 29 | 30 |
23 | 30 | ); |
24 | 31 |
|
25 | | -// Create payment (105.20 € with unique id 'subscriptionpayment') |
26 | | -$createPayment = new \ThePay\ApiClient\Model\CreatePaymentParams(10520, 'EUR', 'subscriptionpayment'); |
| 32 | +// Create initial subscription payment (105.20 € with UID 'uid_subscriptionpayment') |
| 33 | +$createPayment = new \ThePay\ApiClient\Model\CreatePaymentParams(10520, 'EUR', 'uid_subscriptionpayment', $customer); |
27 | 34 | $createPayment->setSubscription($subscription); |
28 | 35 |
|
29 | 36 | $payment = $thePayClient->createPayment($createPayment); |
30 | 37 |
|
31 | | -// Get url where user can pay |
| 38 | +// Redirect the customer to complete the payment |
32 | 39 | echo $payment->getPayUrl(); // https://demo.gate.thepay.cz/5aa4f4af546a74848/pay/ |
33 | 40 | ``` |
34 | 41 |
|
35 | 42 | ## Realizing subscription payment |
36 | 43 |
|
37 | | -After the created payment was paid, we can realize subscription: |
| 44 | +After the parent payment is paid, you may charge the customer again using one of the three subscription realization types. |
38 | 45 |
|
39 | | -### Realizing regular subscription payment type |
| 46 | +### Realizing a Regular (Fixed Interval & Fixed Amount) Subscription |
40 | 47 |
|
41 | 48 | ```php |
42 | 49 | /** @var \ThePay\ApiClient\TheClient $thePayClient */ |
43 | 50 |
|
44 | | -// parameter is uid of new (child) payment |
45 | | -$params = new \ThePay\ApiClient\Model\RealizeRegularSubscriptionPaymentParams('childpayment'); |
| 51 | +// UID of the new (child) payment |
| 52 | +$params = new \ThePay\ApiClient\Model\RealizeRegularSubscriptionPaymentParams('uid_childpayment'); |
46 | 53 |
|
47 | | -// adding items is optional, if you do not add any item, items from parent payment will be used |
| 54 | +// You may optionally define items. If omitted, items from parent payment are reused. |
48 | 55 | $item = new \ThePay\ApiClient\Model\CreatePaymentItem('item', 'Magazine #2', 10520, 1); |
49 | 56 | $params->addItem($item); |
50 | 57 |
|
51 | | - |
52 | | -// first parameter is uid of parent payment (the one we create above with subscription property). |
53 | | -// method will return ApiResponse |
54 | | -$response = $thePayClient->realizeRegularSubscriptionPayment('subscriptionpayment', $params); |
| 58 | +// Parent payment UID is passed as the first parameter. |
| 59 | +// Method returns an ApiResponse. |
| 60 | +$response = $thePayClient->realizeRegularSubscriptionPayment('uid_subscriptionpayment', $params); |
55 | 61 |
|
56 | 62 | if ($response->wasSuccessful()) { |
57 | 63 | echo 'Subscription payment was realized'; |
58 | 64 | } |
59 | 65 | ``` |
60 | 66 |
|
61 | | -### Realizing irregular subscription payment type |
| 67 | +### Realizing an Irregular (Variable Interval & Fixed Amount) Subscription |
62 | 68 |
|
63 | 69 | ```php |
64 | 70 | /** @var \ThePay\ApiClient\TheClient $thePayClient */ |
65 | 71 |
|
66 | | -// parameter is uid of new (child) payment |
67 | | -$params = new \ThePay\ApiClient\Model\RealizeIrregularSubscriptionPaymentParams('childpayment2'); |
| 72 | +// UID of the new (child) payment |
| 73 | +$params = new \ThePay\ApiClient\Model\RealizeIrregularSubscriptionPaymentParams('uid_childpayment2'); |
68 | 74 |
|
69 | | -// adding items is optional, if you do not add any item, items from parent payment will be used |
| 75 | +// You may optionally define items. If omitted, items from parent payment are reused. |
70 | 76 | $item = new \ThePay\ApiClient\Model\CreatePaymentItem('item', 'New book', 10520, 1); |
71 | 77 | $params->addItem($item); |
72 | 78 |
|
73 | | - |
74 | | -// first parameter is uid of parent payment (the one we create above with subscription property). |
75 | | -// method will return ApiResponse |
76 | | -$response = $thePayClient->realizeIrregularSubscriptionPayment('subscriptionpayment', $params); |
| 79 | +// Parent payment UID is passed as the first parameter. |
| 80 | +// Method returns an ApiResponse. |
| 81 | +$response = $thePayClient->realizeIrregularSubscriptionPayment('uid_subscriptionpayment', $params); |
77 | 82 |
|
78 | 83 | if ($response->wasSuccessful()) { |
79 | 84 | echo 'Subscription payment was realized'; |
80 | 85 | } |
81 | 86 | ``` |
82 | 87 |
|
83 | | -### Realizing usage based subscription payment type |
| 88 | +### Realizing a Usage-Based (Fixed Interval & Variable Amount) Subscription |
84 | 89 |
|
85 | 90 | ```php |
86 | 91 | /** @var \ThePay\ApiClient\TheClient $thePayClient */ |
87 | 92 |
|
88 | | -// first parameter is uid of new (child) payment |
89 | | -// second parameter is amount in cents (in parent payment currency) |
90 | | -$params = new \ThePay\ApiClient\Model\RealizeUsageBasedSubscriptionPaymentParams('childpayment3', 18000); |
| 93 | +// First param: UID of child payment |
| 94 | +// Second param: amount in cents (in the parent payment currency) |
| 95 | +$params = new \ThePay\ApiClient\Model\RealizeUsageBasedSubscriptionPaymentParams('uid_childpayment3', 18000); |
91 | 96 |
|
92 | | -// adding items is optional, if you do not add any item, items from parent payment will be used |
| 97 | +// You may optionally define items. If omitted, items from parent payment are reused. |
93 | 98 | $item = new \ThePay\ApiClient\Model\CreatePaymentItem('item', 'Server usage', 18000, 1); |
94 | 99 | $params->addItem($item); |
95 | 100 |
|
96 | 101 |
|
97 | | -// first parameter is uid of parent payment (the one we create above with subscription property). |
98 | | -// method will return ApiResponse |
99 | | -$response = $thePayClient->realizeUsageBasedSubscriptionPayment('subscriptionpayment', $params); |
| 102 | +// Parent payment UID is passed as the first parameter. |
| 103 | +// Method returns an ApiResponse. |
| 104 | +$response = $thePayClient->realizeUsageBasedSubscriptionPayment('uid_subscriptionpayment', $params); |
100 | 105 |
|
101 | 106 | if ($response->wasSuccessful()) { |
102 | 107 | echo 'Subscription payment was realized'; |
|
0 commit comments