toss_payments 0.0.0 → 0.1.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 +156 -2
  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: b16cccf42152733567c97daae4c9949e97d15dc7b7f2b2d8d0ee228959bb6a52
4
+ data.tar.gz: e4fab7035a23e2e82464a35c960e5bad288673b7112b802bc2a95694d32a6d7a
5
5
  SHA512:
6
- metadata.gz: '09eeb48de04535a5e20e77ed01ed229f17f11cc0dfa4f0df6f0f87c5c006373439b13b2f47378c3d1b2e073744a3bc50c21942a14714110c71d8a6863452e5ed'
7
- data.tar.gz: 3aae4d18e2b2c87106889ea51f0290530893595fc1ebb8f236f007c18bc28c505f3984775f0ea93f18673c41a9e8c8e0df0eb9088568d486ea760225da4d4341
6
+ metadata.gz: a18b3ee6f20c0fe4ba41bfe22b308aae55ccb2c8125d3acf6b3b3f0f116490803552d1ce5a75c9e883852f5b0290fd97334092ef4bed5f97ccc19e79f562e536
7
+ data.tar.gz: b648fa7b53f35efdc236c028ce0479af6e8dd892fc0273e71dfc6bef2c9e28717f10686da5f281d50d18a9e88618a33237a30ad1d8ecc69e449fbe6c0b39c484
data/lib/toss_payments.rb CHANGED
@@ -1,6 +1,46 @@
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
+
4
44
  HOST = "https://api.tosspayments.com/v1"
5
45
 
6
46
  class Config
@@ -74,12 +114,126 @@ module TossPayments
74
114
 
75
115
  def get(uri, payload = {})
76
116
  url = "#{HOST}/#{uri}"
77
- HTTParty.get(url, headers: headers, body: payload)
117
+ response = HTTParty.get(url, headers: headers, body: payload).parsed_response
118
+ {
119
+ code: response["code"],
120
+ message: response["message"],
121
+ data: response_data_to_model(response["data"]),
122
+ }
78
123
  end
79
124
 
80
125
  def post
81
126
  url = "#{HOST}/#{uri}"
82
- HTTParty.post(url, headers: headers.merge("Content-Type": "application/json"), body: payload.to_json)
127
+ response = HTTParty.post(url, headers: headers.merge("Content-Type": "application/json"), body: payload.to_json).parsed_response
128
+ {
129
+ code: response["code"],
130
+ message: response["message"],
131
+ data: response_data_to_model(response["data"]),
132
+ }
133
+ end
134
+
135
+ def response_data_to_model(data)
136
+ return nil if data.nil?
137
+ PaymentResponseData.new(
138
+ version: data["version"],
139
+ payment_key: data["paymentKey"],
140
+ type: data["type"],
141
+ order_id: data["orderId"],
142
+ order_name: data["orderName"],
143
+ m_id: data["mId"],
144
+ currency: data["currency"],
145
+ method: data["method"],
146
+ total_amount: data["totalAmount"],
147
+ balanced_amount: data["balancedAmount"],
148
+ status: data["status"].downcase.to_sym,
149
+ requested_at: Time.parse(data["requestedAt"]),
150
+ approved_at: Time.parse(data["approvedAt"]),
151
+ use_escrow: data["useEscrow"],
152
+ last_transaction_key: data["lastTransactionKey"],
153
+ supplied_amount: data["suppliedAmount"],
154
+ vat: data["vat"],
155
+ culture_expense: data["cultureExpense"],
156
+ tax_free_amount: data["taxFreeAmount"],
157
+ tax_exemption_amount: data["taxExemptionAmount"],
158
+ cancels: data["canceles"]&.map do |cancel|
159
+ {
160
+ cancel_amount: cancel["cancelAmount"],
161
+ cancel_reason: cancel["cancelReason"],
162
+ tax_free_amount: cancel["taxFreeAmount"],
163
+ tax_exemption_amount: cancel["taxExemptionAmount"],
164
+ refundable_amount: cancel["refundableAmount"],
165
+ easy_pay_discount_amount: cancel["easyPayDiscountAmount"],
166
+ canceled_at: Time.parse(cancel["canceledAt"]),
167
+ transaction_key: cancel["transactionKey"],
168
+ }
169
+ end,
170
+ is_partial_cancelable: data["isPartialCancelable"],
171
+ card: data["card"] ? {
172
+ amount: data["card"]["amount"],
173
+ issuer_code: data["card"]["issuerCode"],
174
+ acquirer_code: data["card"]["acquirerCode"],
175
+ number: data["card"]["number"],
176
+ installment_plan_months: data["card"]["installmentPlanMonths"],
177
+ approve_no: data["card"]["approveNo"],
178
+ use_card_point: data["card"]["useCardPoint"],
179
+ card_type: data["card"]["cardType"],
180
+ owner_type: data["card"]["ownerType"],
181
+ acquire_status: data["card"]["acquireStatus"].downcase.to_sym,
182
+ is_interest_free: data["card"]["isInterestFree"],
183
+ interset_payer: data["card"]["intersetPayer"].downcase.to_sym,
184
+ } : nil,
185
+ virtual_account: data["virtualAccount"] ? {
186
+ account_type: data["virtualAccount"]["accountType"],
187
+ account_number: data["virtualAccount"]["accountNumber"],
188
+ bank_code: data["virtualAccount"]["bankCode"],
189
+ customer_name: data["virtualAccount"]["customerName"],
190
+ due_date: Time.parse(data["virtualAccount"]["dueDate"]),
191
+ refund_status: data["virtualAccount"]["refundStatus"].downcase.to_sym,
192
+ expired: data["virtualAccount"]["expired"],
193
+ settlement_status: data["virtualAccount"]["settlementStatus"],
194
+ } : nil,
195
+ secret: data["secret"],
196
+ mobile_phone: data["mobilePhone"] ? {
197
+ customer_mobile_phone: data["mobilePhone"]["customerMobilePhone"],
198
+ settlement_status: data["mobilePhone"]["settlementStatus"],
199
+ receipt_url: data["mobilePhone"]["receiptUrl"],
200
+ } : nil,
201
+ gift_certificate: data["giftCertificate"] ? {
202
+ approve_no: data["giftCertificate"]["approveNo"],
203
+ settlement_status: data["giftCertificate"]["settlementStatus"],
204
+ } : nil,
205
+ transfer: data["transfer"] ? {
206
+ bank_code: data["transfer"]["bankCode"],
207
+ settlement_status: data["transfer"]["settlementStatus"],
208
+ } : nil,
209
+ receipt: data["receipt"] ? {
210
+ url: data["receipt"]["url"],
211
+ } : nil,
212
+ checkout: data["checkout"] ? {
213
+ url: data["checkout"]["url"],
214
+ } : nil,
215
+ easy_pay: data["easyPay"] ? {
216
+ provider: data["easyPay"]["provider"],
217
+ amount: data["easyPay"]["amount"],
218
+ discount_amount: data["easyPay"]["discountAmount"],
219
+ } : nil,
220
+ country: data["country"],
221
+ failure: data["failure"] ? {
222
+ code: data["failure"]["code"],
223
+ message: data["failure"]["message"],
224
+ } : nil,
225
+ cash_receipt: data["cashReceipt"] ? {
226
+ receipt_key: data["cashReceipt"]["receiptKey"],
227
+ type: data["cashReceipt"]["type"],
228
+ amount: data["cashReceipt"]["amount"],
229
+ tax_free_amount: data["cashReceipt"]["taxFreeAmount"],
230
+ issue_number: data["cashReceipt"]["issueNumber"],
231
+ receipt_url: data["cashReceipt"]["receiptUrl"],
232
+ } : nil,
233
+ discount: data["discount"] ? {
234
+ amount: data["discount"]["amount"],
235
+ } : nil,
236
+ )
83
237
  end
84
238
  end
85
239
  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.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Soohyeon Lee