-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathgopay_subscription_example.rb
153 lines (141 loc) · 5.44 KB
/
gopay_subscription_example.rb
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
require 'veritrans'
# This is just for very basic implementation reference, in production, you should validate the incoming requests and implement your backend more securely.
# Set Midtrans config
# You can find it in Merchant Portal -> Settings -> Access keys
Midtrans.config.server_key = "SB-Mid-server-uQmMImQMeo0Ky3Svl90QTUj2"
Midtrans.config.client_key = "SB-Mid-client-ArNfhrh7st9bQKmz"
Midtrans.config.api_host = "https://api.sandbox.midtrans.com"
# To use API subscription for gopay, you should first link your customer gopay account with gopay tokenization
# Refer to this docs: https://api-docs.midtrans.com/#gopay-tokenization
# You will receive gopay payment token using `get_payment_account` API call.
# You can see some Tokenization API examples here (examples/tokenization)
# {
# : status_code => "200",: payment_type => "gopay",: account_id => "7501d1f5-697c-4b4b-b095-69e60003759f",: account_status => "ENABLED",: metadata => {
# "payment_options" => [{
# "name" => "GOPAY_WALLET",
# "active" => true,
# "balance" => {
# "value" => "8000000.00", "currency" => "IDR"
# },
# "metadata" => {},
# "token" => "de60838a-4fb7-48af-89b8-4741ba8e5404"
# }, {
# "name" => "PAY_LATER",
# "active" => true,
# "balance" => {
# "value" => "8000000.00", "currency" => "IDR"
# },
# "metadata" => {},
# "token" => "5b141678-7dc3-4f36-a5ec-c74ce62d2c46"
# }]
# }
# }
# Sample gopay payment option token and gopay account id for testing purpose that has been already activated before
gopay_payment_option_token = "de60838a-4fb7-48af-89b8-4741ba8e5404"
gopay_account_id = "7501d1f5-697c-4b4b-b095-69e60003759f"
# prepare CORE API parameter ( refer to: https://api-docs.midtrans.com/#create-subscription ) create subscription parameter example
begin
parameter = {
"name": "gopay_monthly_subscription",
"amount": "10000",
"currency": "IDR",
"payment_type": "gopay",
"token": gopay_payment_option_token,
"schedule": {
"interval": 1,
"interval_unit": "day",
"max_interval": 7
},
"metadata": {
"description": "Recurring payment for A"
},
"customer_details": {
"first_name": "John A",
"last_name": "Doe A",
"email": "[email protected]",
"phone": "+62812345678"
},
"gopay": {
"account_id": gopay_account_id
}
}
result = Midtrans.create_subscription(parameter)
puts "Create subscription response : #{result.data}"
rescue MidtransError => e
puts e.message # Basic message error
puts e.http_status_code # HTTP status code e.g: 400, 401, etc.
puts e.api_response # API response body in String
puts e.raw_http_client_data # Raw HTTP client response
end
# result.data this will be Hash representation of the API JSON response
# {
# : id => "c04d89cb-ece6-4419-a87a-f773b243760d",
# : name => "SUBS-Gopay-2021",
# : amount => "10000",
# : currency => "IDR",
# : created_at => "2021-12-17 11:05:16",
# : schedule => {
# "interval" => 1, "current_interval" => 0, "max_interval" => 7, "interval_unit" => "day", "start_time" => "2021-12-17 11:05:16", "previous_execution_at" => "2021-12-17 11:05:16", "next_execution_at" => "2021-12-18 11:05:16"
# },
# : status => "active",
# : token => "de60838a-4fb7-48af-89b8-4741ba8e5404",
# : payment_type => "gopay",
# : transaction_ids => [],
# : metadata => {
# "description" => "Recurring payment for A"
# },
# : customer_details => {
# "email" => "[email protected]", "first_name" => "John A", "last_name" => "Doe A", "phone" => "+62812345678"
# }
# }
# sample active subscription id for testing purpose
subscription_id = "c04d89cb-ece6-4419-a87a-f773b243760d"
# get subscription by subscription_id
begin
result_get_subs = Midtrans.get_subscription(subscription_id)
puts "get subscription response : #{result_get_subs.data}"
rescue MidtransError => e
puts e.message # Basic message error
puts e.http_status_code # HTTP status code e.g: 400, 401, etc.
puts e.api_response # API response body in String
puts e.raw_http_client_data # Raw HTTP client response
end
# enable subscription by subscription_id
begin
result_enable_subs = Midtrans.enable_subscription(subscription_id)
puts "enable subscription response : #{result_enable_subs.data}"
rescue MidtransError => e
puts e.message # Basic message error
puts e.http_status_code # HTTP status code e.g: 400, 401, etc.
puts e.api_response # API response body in String
puts e.raw_http_client_data # Raw HTTP client response
end
# update subscription by subscription_id and update_subscription_param
begin
update_subscription_param = {
"name": "gopay_monthly_subscription",
"amount": "300000",
"currency": "IDR",
"token": gopay_payment_option_token,
"schedule": {
"interval": 1
}
}
result_update_subs = Midtrans.update_subscription(subscription_id, update_subscription_param)
puts "update subscription response : #{result_update_subs.data}"
rescue MidtransError => e
puts e.message # Basic message error
puts e.http_status_code # HTTP status code e.g: 400, 401, etc.
puts e.api_response # API response body in String
puts e.raw_http_client_data # Raw HTTP client response
end
# disable subscription by subscription_id
begin
result_disable_subs = Midtrans.disable_subscription(subscription_id)
puts "disable subscription response : #{result_disable_subs.data}"
rescue MidtransError => e
puts e.message # Basic message error
puts e.http_status_code # HTTP status code e.g: 400, 401, etc.
puts e.api_response # API response body in String
puts e.raw_http_client_data # Raw HTTP client response
end