toss_payments 0.2.2 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/toss_payments.rb +123 -106
  3. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f112e220a8d2f49711528bfb6ca8044d9aa6ece18cab174dce2f26c698d6bc3c
4
- data.tar.gz: 7948683c48e4285cf0d544de10f7887031c73792f28de3d64f4964ddb37de88f
3
+ metadata.gz: b558ed78758da3b5beb0ebfbdcfd33695eea788c7c99013f19c68540684ac9d0
4
+ data.tar.gz: 2c72bca37718d46803a8fcc0072f7ebb3fa2f7d056dae6a4539503914334873c
5
5
  SHA512:
6
- metadata.gz: 03db0992350ce08a832ec8428542543b5942567f1eea93e4e27e9785eb6608ece582e1b444d09fe6d5e6b6b238df675971a962447327a9b9aeb35bec263c989f
7
- data.tar.gz: a224135e3135bb55009df17167627796094dc5b7557ade95c56842391f0d1691ad23f03a3eddae0bcc948c9c4a9f291dab19e562d7eea37e297207a1e9fe6a5e
6
+ metadata.gz: b15817f5ced9187d0d4793ccfcff357457730c1a2a9e3ea6c9c42df35d2c7f2b4ebc763806b95a373e9a87e69290e3fc3f9f4393dea45e2dcc546bce0c328f6c
7
+ data.tar.gz: 91ddb6b8c707585531c98f6ec325572a30599fb4800d0a8afaa77509f5bc3a99722b35bf648622e5a9762aecb82097d46b1d6d628bdd7bd3b7e1015eebb73f58
data/lib/toss_payments.rb CHANGED
@@ -2,7 +2,16 @@ require "httparty"
2
2
  require "time"
3
3
 
4
4
  module TossPayments
5
- PaymentResponseData = Struct.new(
5
+ ErrorResponse = Struct.new(
6
+ :response_type,
7
+ :code,
8
+ :message,
9
+ :response,
10
+ keyword_init: true,
11
+ )
12
+
13
+ PaymentResponse = Struct.new(
14
+ :response_type,
6
15
  :version,
7
16
  :payment_key,
8
17
  :type,
@@ -41,7 +50,8 @@ module TossPayments
41
50
  keyword_init: true,
42
51
  )
43
52
 
44
- BillingResponseData = Struct.new(
53
+ BillingResponse = Struct.new(
54
+ :response_type,
45
55
  :m_id,
46
56
  :customer_key,
47
57
  :authenticated_at,
@@ -103,12 +113,12 @@ module TossPayments
103
113
 
104
114
  def billing_auth_card(payload = {})
105
115
  uri = "billing/authorizations/card"
106
- post(uri, payload, type: :billing)
116
+ post(uri, payload, response_type: :billing)
107
117
  end
108
118
 
109
119
  def billing_auth_issue(payload = {})
110
120
  uri = "billing/authorizations/issue"
111
- post(uri, payload, type: :billing)
121
+ post(uri, payload, response_type: :billing)
112
122
  end
113
123
 
114
124
  def billing(billing_key, payload = {})
@@ -122,50 +132,57 @@ module TossPayments
122
132
  { "Authorization": "Basic #{Base64.strict_encode64(config.secret_key + ":")}" }
123
133
  end
124
134
 
125
- def get(uri, payload = {}, type: :payment)
135
+ def get(uri, payload = {}, response_type: :payment)
126
136
  url = "#{HOST}/#{uri}"
127
137
  response = HTTParty.get(url, headers: headers, body: payload).parsed_response
128
- {
129
- code: response["code"],
130
- message: response["message"],
131
- data: type == :payment ? payment_response_data_to_model(response["data"]) : billing_response_data_to_model(response["data"]),
132
- }
138
+ response_to_model(response, response_type: response_type)
133
139
  end
134
140
 
135
- def post(uri, payload = {}, type: :payment)
141
+ def post(uri, payload = {}, response_type: :payment)
136
142
  url = "#{HOST}/#{uri}"
137
143
  response = HTTParty.post(url, headers: headers.merge("Content-Type": "application/json"), body: payload.to_json).parsed_response
138
- {
144
+ response_to_model(response, response_type: response_type)
145
+ end
146
+
147
+ def response_to_model(response, response_type:)
148
+ return error_response_to_model(response) if response.keys.include?("code")
149
+ return payment_response_to_model(response) if response_type == :payment
150
+ return billing_response_to_model(response) if response_type == :billing
151
+ end
152
+
153
+ def error_response_to_model(response)
154
+ ErrorResponse.new(
155
+ response_type: :error,
139
156
  code: response["code"],
140
157
  message: response["message"],
141
- data: type == :payment ? payment_response_data_to_model(response["data"]) : billing_response_data_to_model(response["data"]),
142
- }
158
+ data: response["data"],
159
+ )
143
160
  end
144
161
 
145
- def payment_response_data_to_model(data)
146
- return nil if data.nil?
147
- PaymentResponseData.new(
148
- version: data["version"],
149
- payment_key: data["paymentKey"],
150
- type: data["type"],
151
- order_id: data["orderId"],
152
- order_name: data["orderName"],
153
- m_id: data["mId"],
154
- currency: data["currency"],
155
- method: data["method"],
156
- total_amount: data["totalAmount"],
157
- balanced_amount: data["balancedAmount"],
158
- status: data["status"].downcase.to_sym,
159
- requested_at: Time.parse(data["requestedAt"]),
160
- approved_at: Time.parse(data["approvedAt"]),
161
- use_escrow: data["useEscrow"],
162
- last_transaction_key: data["lastTransactionKey"],
163
- supplied_amount: data["suppliedAmount"],
164
- vat: data["vat"],
165
- culture_expense: data["cultureExpense"],
166
- tax_free_amount: data["taxFreeAmount"],
167
- tax_exemption_amount: data["taxExemptionAmount"],
168
- cancels: data["canceles"]&.map do |cancel|
162
+ def payment_response_to_model(response)
163
+ PaymentResponse.new(
164
+ response_type: :payment,
165
+ version: response["version"],
166
+ payment_key: response["paymentKey"],
167
+ type: response["type"],
168
+ order_id: response["orderId"],
169
+ order_name: response["orderName"],
170
+ m_id: response["mId"],
171
+ currency: response["currency"],
172
+ method: response["method"],
173
+ total_amount: response["totalAmount"],
174
+ balanced_amount: response["balancedAmount"],
175
+ status: response["status"].downcase.to_sym,
176
+ requested_at: Time.parse(response["requestedAt"]),
177
+ approved_at: Time.parse(response["approvedAt"]),
178
+ use_escrow: response["useEscrow"],
179
+ last_transaction_key: response["lastTransactionKey"],
180
+ supplied_amount: response["suppliedAmount"],
181
+ vat: response["vat"],
182
+ culture_expense: response["cultureExpense"],
183
+ tax_free_amount: response["taxFreeAmount"],
184
+ tax_exemption_amount: response["taxExemptionAmount"],
185
+ cancels: response["canceles"]&.map do |cancel|
169
186
  {
170
187
  cancel_amount: cancel["cancelAmount"],
171
188
  cancel_reason: cancel["cancelReason"],
@@ -177,89 +194,89 @@ module TossPayments
177
194
  transaction_key: cancel["transactionKey"],
178
195
  }
179
196
  end,
180
- is_partial_cancelable: data["isPartialCancelable"],
181
- card: data["card"] ? {
182
- amount: data["card"]["amount"],
183
- issuer_code: data["card"]["issuerCode"],
184
- acquirer_code: data["card"]["acquirerCode"],
185
- number: data["card"]["number"],
186
- installment_plan_months: data["card"]["installmentPlanMonths"],
187
- approve_no: data["card"]["approveNo"],
188
- use_card_point: data["card"]["useCardPoint"],
189
- card_type: data["card"]["cardType"],
190
- owner_type: data["card"]["ownerType"],
191
- acquire_status: data["card"]["acquireStatus"].downcase.to_sym,
192
- is_interest_free: data["card"]["isInterestFree"],
193
- interset_payer: data["card"]["intersetPayer"].downcase.to_sym,
197
+ is_partial_cancelable: response["isPartialCancelable"],
198
+ card: response["card"] ? {
199
+ amount: response["card"]["amount"],
200
+ issuer_code: response["card"]["issuerCode"],
201
+ acquirer_code: response["card"]["acquirerCode"],
202
+ number: response["card"]["number"],
203
+ installment_plan_months: response["card"]["installmentPlanMonths"],
204
+ approve_no: response["card"]["approveNo"],
205
+ use_card_point: response["card"]["useCardPoint"],
206
+ card_type: response["card"]["cardType"],
207
+ owner_type: response["card"]["ownerType"],
208
+ acquire_status: response["card"]["acquireStatus"].downcase.to_sym,
209
+ is_interest_free: response["card"]["isInterestFree"],
210
+ interset_payer: response["card"]["intersetPayer"].downcase.to_sym,
194
211
  } : nil,
195
- virtual_account: data["virtualAccount"] ? {
196
- account_type: data["virtualAccount"]["accountType"],
197
- account_number: data["virtualAccount"]["accountNumber"],
198
- bank_code: data["virtualAccount"]["bankCode"],
199
- customer_name: data["virtualAccount"]["customerName"],
200
- due_date: Time.parse(data["virtualAccount"]["dueDate"]),
201
- refund_status: data["virtualAccount"]["refundStatus"].downcase.to_sym,
202
- expired: data["virtualAccount"]["expired"],
203
- settlement_status: data["virtualAccount"]["settlementStatus"],
212
+ virtual_account: response["virtualAccount"] ? {
213
+ account_type: response["virtualAccount"]["accountType"],
214
+ account_number: response["virtualAccount"]["accountNumber"],
215
+ bank_code: response["virtualAccount"]["bankCode"],
216
+ customer_name: response["virtualAccount"]["customerName"],
217
+ due_date: Time.parse(response["virtualAccount"]["dueDate"]),
218
+ refund_status: response["virtualAccount"]["refundStatus"].downcase.to_sym,
219
+ expired: response["virtualAccount"]["expired"],
220
+ settlement_status: response["virtualAccount"]["settlementStatus"],
204
221
  } : nil,
205
- secret: data["secret"],
206
- mobile_phone: data["mobilePhone"] ? {
207
- customer_mobile_phone: data["mobilePhone"]["customerMobilePhone"],
208
- settlement_status: data["mobilePhone"]["settlementStatus"],
209
- receipt_url: data["mobilePhone"]["receiptUrl"],
222
+ secret: response["secret"],
223
+ mobile_phone: response["mobilePhone"] ? {
224
+ customer_mobile_phone: response["mobilePhone"]["customerMobilePhone"],
225
+ settlement_status: response["mobilePhone"]["settlementStatus"],
226
+ receipt_url: response["mobilePhone"]["receiptUrl"],
210
227
  } : nil,
211
- gift_certificate: data["giftCertificate"] ? {
212
- approve_no: data["giftCertificate"]["approveNo"],
213
- settlement_status: data["giftCertificate"]["settlementStatus"],
228
+ gift_certificate: response["giftCertificate"] ? {
229
+ approve_no: response["giftCertificate"]["approveNo"],
230
+ settlement_status: response["giftCertificate"]["settlementStatus"],
214
231
  } : nil,
215
- transfer: data["transfer"] ? {
216
- bank_code: data["transfer"]["bankCode"],
217
- settlement_status: data["transfer"]["settlementStatus"],
232
+ transfer: response["transfer"] ? {
233
+ bank_code: response["transfer"]["bankCode"],
234
+ settlement_status: response["transfer"]["settlementStatus"],
218
235
  } : nil,
219
- receipt: data["receipt"] ? {
220
- url: data["receipt"]["url"],
236
+ receipt: response["receipt"] ? {
237
+ url: response["receipt"]["url"],
221
238
  } : nil,
222
- checkout: data["checkout"] ? {
223
- url: data["checkout"]["url"],
239
+ checkout: response["checkout"] ? {
240
+ url: response["checkout"]["url"],
224
241
  } : nil,
225
- easy_pay: data["easyPay"] ? {
226
- provider: data["easyPay"]["provider"],
227
- amount: data["easyPay"]["amount"],
228
- discount_amount: data["easyPay"]["discountAmount"],
242
+ easy_pay: response["easyPay"] ? {
243
+ provider: response["easyPay"]["provider"],
244
+ amount: response["easyPay"]["amount"],
245
+ discount_amount: response["easyPay"]["discountAmount"],
229
246
  } : nil,
230
- country: data["country"],
231
- failure: data["failure"] ? {
232
- code: data["failure"]["code"],
233
- message: data["failure"]["message"],
247
+ country: response["country"],
248
+ failure: response["failure"] ? {
249
+ code: response["failure"]["code"],
250
+ message: response["failure"]["message"],
234
251
  } : nil,
235
- cash_receipt: data["cashReceipt"] ? {
236
- receipt_key: data["cashReceipt"]["receiptKey"],
237
- type: data["cashReceipt"]["type"],
238
- amount: data["cashReceipt"]["amount"],
239
- tax_free_amount: data["cashReceipt"]["taxFreeAmount"],
240
- issue_number: data["cashReceipt"]["issueNumber"],
241
- receipt_url: data["cashReceipt"]["receiptUrl"],
252
+ cash_receipt: response["cashReceipt"] ? {
253
+ receipt_key: response["cashReceipt"]["receiptKey"],
254
+ type: response["cashReceipt"]["type"],
255
+ amount: response["cashReceipt"]["amount"],
256
+ tax_free_amount: response["cashReceipt"]["taxFreeAmount"],
257
+ issue_number: response["cashReceipt"]["issueNumber"],
258
+ receipt_url: response["cashReceipt"]["receiptUrl"],
242
259
  } : nil,
243
- discount: data["discount"] ? {
244
- amount: data["discount"]["amount"],
260
+ discount: response["discount"] ? {
261
+ amount: response["discount"]["amount"],
245
262
  } : nil,
246
263
  )
247
264
  end
248
265
 
249
- def billing_response_data_to_model(data)
250
- return nil if data.nil?
251
- BillingResponseData.new(
252
- m_id: data["mId"],
253
- customer_key: data["customerKey"],
254
- authenticated_at: Time.parse(data["authenticatedAt"]),
255
- method: data["method"],
256
- billing_key: data["billingKey"],
266
+ def billing_response_to_model(response)
267
+ BillingResponse.new(
268
+ response_type: :billing,
269
+ m_id: response["mId"],
270
+ customer_key: response["customerKey"],
271
+ authenticated_at: Time.parse(response["authenticatedAt"]),
272
+ method: response["method"],
273
+ billing_key: response["billingKey"],
257
274
  card: {
258
- issuer_code: data["card"]["issuerCode"],
259
- acquirer_code: data["card"]["acquirerCode"],
260
- number: data["card"]["number"],
261
- card_type: data["card"]["cardType"],
262
- owner_type: data["card"]["ownerType"],
275
+ issuer_code: response["card"]["issuerCode"],
276
+ acquirer_code: response["card"]["acquirerCode"],
277
+ number: response["card"]["number"],
278
+ card_type: response["card"]["cardType"],
279
+ owner_type: response["card"]["ownerType"],
263
280
  },
264
281
  )
265
282
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: toss_payments
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Soohyeon Lee
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-03-10 00:00:00.000000000 Z
11
+ date: 2023-03-13 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: toss payments api
14
14
  email: lsh59727@gmail.com