toss_payments 0.0.0 → 0.2.0

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 +188 -6
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e315444c4049b45e7230121e53497109beec335dad556bba86531e73920cb9c8
4
- data.tar.gz: 6c449188660da72d13d3de59567c59fbe0cd29292a4f4f305d1beb6091d28905
3
+ metadata.gz: 75a55683e83f0eaa4cead2cf31ce9d57fe73934b51edfe19886afc7130a9eccc
4
+ data.tar.gz: d4d69e88f3ea4056d8b9476f3c9219b017455c6d28cf5c501e500a27576524e2
5
5
  SHA512:
6
- metadata.gz: '09eeb48de04535a5e20e77ed01ed229f17f11cc0dfa4f0df6f0f87c5c006373439b13b2f47378c3d1b2e073744a3bc50c21942a14714110c71d8a6863452e5ed'
7
- data.tar.gz: 3aae4d18e2b2c87106889ea51f0290530893595fc1ebb8f236f007c18bc28c505f3984775f0ea93f18673c41a9e8c8e0df0eb9088568d486ea760225da4d4341
6
+ metadata.gz: 60e9a7babf7bce954585fa4e35e975b56e24d39fd28f64d3897dfec8af3f810187950a4e2a565ec7007b1aa2886cf71bc602cff94583d11cc7e2a33786fcb74e
7
+ data.tar.gz: ec26884c0664c3f3b5ec8b57b5441d843f4a7781d35e6d827a09b502b5279a7119d0d4c95d4347b6b88f7f8cc6ff8f24bf9e7f740dba1947eec9cf44e3d3a511
data/lib/toss_payments.rb CHANGED
@@ -1,6 +1,56 @@
1
1
  require "httparty"
2
+ require "time"
2
3
 
3
4
  module TossPayments
5
+ PaymentResponseData = Struct.new(
6
+ :version,
7
+ :payment_key,
8
+ :type,
9
+ :order_id,
10
+ :order_name,
11
+ :m_id,
12
+ :currency,
13
+ :method,
14
+ :total_amount,
15
+ :balanced_amount,
16
+ :status,
17
+ :requested_at,
18
+ :approved_at,
19
+ :use_escrow,
20
+ :last_transaction_key,
21
+ :supplied_amount,
22
+ :vat,
23
+ :culture_expense,
24
+ :tax_free_amount,
25
+ :tax_exemption_amount,
26
+ :cancels, # nullable
27
+ :is_partial_cancelable,
28
+ :card, # nullable
29
+ :virtual_account, # nullable
30
+ :secret, # nullable
31
+ :mobile_phone, # nullable
32
+ :gift_certificate, # nullable
33
+ :transfer, # nullable
34
+ :receipt,
35
+ :checkout,
36
+ :easy_pay, # nullable
37
+ :country, # ISO-3166
38
+ :failure, # nullable
39
+ :cash_receipt, # nullable
40
+ :discount, # nullable
41
+ keyword_init: true,
42
+ )
43
+
44
+ BillingResponseData = Struct.new(
45
+ :m_id,
46
+ :customer_key,
47
+ :authenticated_at,
48
+ :method,
49
+ :billing_key,
50
+ :card,
51
+ keyword_init: true,
52
+ )
53
+
4
54
  HOST = "https://api.tosspayments.com/v1"
5
55
 
6
56
  class Config
@@ -53,12 +103,12 @@ module TossPayments
53
103
 
54
104
  def billing_auth_card(payload = {})
55
105
  uri = "billing/authorizations/card"
56
- post(uri, payload)
106
+ post(uri, payload, type: :billing)
57
107
  end
58
108
 
59
109
  def billing_auth_issue(payload = {})
60
110
  uri = "billing/authorizations/issue"
61
- post(uri, payload)
111
+ post(uri, payload, type: :billing)
62
112
  end
63
113
 
64
114
  def billing(billing_key, payload = {})
@@ -72,14 +122,146 @@ module TossPayments
72
122
  { "Authorization": "Basic #{Base64.strict_encode64(config.secret_key)}:" }
73
123
  end
74
124
 
75
- def get(uri, payload = {})
125
+ def get(uri, payload = {}, type: :payment)
76
126
  url = "#{HOST}/#{uri}"
77
- HTTParty.get(url, headers: headers, body: payload)
127
+ 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
+ }
78
133
  end
79
134
 
80
- def post
135
+ def post(uri, payload = {}, type: :payment)
81
136
  url = "#{HOST}/#{uri}"
82
- HTTParty.post(url, headers: headers.merge("Content-Type": "application/json"), body: payload.to_json)
137
+ response = HTTParty.post(url, headers: headers.merge("Content-Type": "application/json"), body: payload.to_json).parsed_response
138
+ {
139
+ code: response["code"],
140
+ message: response["message"],
141
+ data: type == :payment ? payment_response_data_to_model(response["data"]) : billing_response_data_to_model(response["data"]),
142
+ }
143
+ end
144
+
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|
169
+ {
170
+ cancel_amount: cancel["cancelAmount"],
171
+ cancel_reason: cancel["cancelReason"],
172
+ tax_free_amount: cancel["taxFreeAmount"],
173
+ tax_exemption_amount: cancel["taxExemptionAmount"],
174
+ refundable_amount: cancel["refundableAmount"],
175
+ easy_pay_discount_amount: cancel["easyPayDiscountAmount"],
176
+ canceled_at: Time.parse(cancel["canceledAt"]),
177
+ transaction_key: cancel["transactionKey"],
178
+ }
179
+ 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,
194
+ } : 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"],
204
+ } : 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"],
210
+ } : nil,
211
+ gift_certificate: data["giftCertificate"] ? {
212
+ approve_no: data["giftCertificate"]["approveNo"],
213
+ settlement_status: data["giftCertificate"]["settlementStatus"],
214
+ } : nil,
215
+ transfer: data["transfer"] ? {
216
+ bank_code: data["transfer"]["bankCode"],
217
+ settlement_status: data["transfer"]["settlementStatus"],
218
+ } : nil,
219
+ receipt: data["receipt"] ? {
220
+ url: data["receipt"]["url"],
221
+ } : nil,
222
+ checkout: data["checkout"] ? {
223
+ url: data["checkout"]["url"],
224
+ } : nil,
225
+ easy_pay: data["easyPay"] ? {
226
+ provider: data["easyPay"]["provider"],
227
+ amount: data["easyPay"]["amount"],
228
+ discount_amount: data["easyPay"]["discountAmount"],
229
+ } : nil,
230
+ country: data["country"],
231
+ failure: data["failure"] ? {
232
+ code: data["failure"]["code"],
233
+ message: data["failure"]["message"],
234
+ } : 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"],
242
+ } : nil,
243
+ discount: data["discount"] ? {
244
+ amount: data["discount"]["amount"],
245
+ } : nil,
246
+ )
247
+ end
248
+
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"],
257
+ 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"],
263
+ },
264
+ )
83
265
  end
84
266
  end
85
267
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: toss_payments
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Soohyeon Lee