paystack_sdk 0.1.1 → 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.
@@ -1,104 +1,218 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # Generated by bin/paystack-scaffold from Paystack's OpenAPI spec (PaystackOSS/openapi@d9d444d).
4
+ # Do not edit by hand: regenerate it with `bin/paystack-scaffold Charge`. Hand-written extras go in lib/paystack_sdk/resources/extensions/charges.rb.
5
+ # scaffold-digest: 8744e957871c8477379f6dc1389cc1e79df5884a4e594936b9ce64ffb6907ef2
6
+
3
7
  require_relative "base"
8
+ require_relative "extensions/charges"
4
9
 
5
10
  module PaystackSdk
6
11
  module Resources
7
- # The `Charges` resource exposes helpers for initiating and managing charges
8
- # through alternative payment channels such as Mobile Money.
9
- #
10
- # At the moment the SDK focuses on supporting the Mobile Money channel which
11
- # requires posting to the `/charge` endpoint with the customer's email,
12
- # amount, currency, and the provider specific `mobile_money` payload.
12
+ # Charge operations.
13
13
  class Charges < PaystackSdk::Resources::Base
14
- MOBILE_MONEY_PROVIDERS = %w[mtn atl vod mpesa orange wave].freeze
14
+ include Extensions::Charges
15
+
16
+ # Ruby keyword => the parameter name Paystack documents.
17
+ WIRE_NAMES = {}.freeze
15
18
 
16
- # Initiates a Mobile Money payment.
19
+ # Create Charge.
17
20
  #
18
- # @param payload [Hash] The payload containing charge details.
19
- # @option payload [String] :email Customer's email address (required)
20
- # @option payload [Integer] :amount Amount in the lowest currency unit (required)
21
- # @option payload [String] :currency ISO currency code (default: GHS)
22
- # @option payload [String] :reference Optional reference supplied by the merchant
23
- # @option payload [String] :callback_url Optional callback URL for Paystack to redirect to
24
- # @option payload [Hash] :metadata Optional metadata to attach to the transaction
25
- # @option payload [Hash] :mobile_money The mobile money details (required)
26
- # - :phone [String] Customer's mobile money phone number (required)
27
- # - :provider [String] Mobile money provider code (required)
21
+ # Initiate a payment by integrating the payment channel of your choice.
28
22
  #
29
- # @return [PaystackSdk::Response] The wrapped API response.
30
- # @raise [PaystackSdk::ValidationError] If the payload is invalid.
31
- def mobile_money(payload)
32
- validate_mobile_money_payload!(payload)
33
-
34
- normalized_payload = normalize_mobile_money_provider(payload)
35
- response = @connection.post("/charge", normalized_payload)
36
- handle_response(response)
23
+ # @param email [String] Customer's email address
24
+ # @param amount [Integer] Amount should be in kobo if currency is NGN, pesewas, if currency is GHS, and cents, if currency is ZAR
25
+ # @param authorization_code [String] An authorization code to charge.
26
+ # @param pin [String] 4-digit PIN (send with a non-reusable authorization code)
27
+ # @param reference [String] Unique transaction reference.
28
+ # @param birthday [String] The customer's birthday in the format YYYY-MM-DD e.g 2017-05-16
29
+ # @param device_id [String] This is the unique identifier of the device a user uses in making payment.
30
+ # @param metadata [Hash] JSON object of custom data
31
+ # @param bank [Hash] The bank object if charging a bank account
32
+ # @param mobile_money [Hash] Details of the mobile service provider
33
+ # @param ussd [Hash] The USSD code for the provider to charge
34
+ # @param eft [Hash] Details of the EFT provider
35
+ # @param currency [String] The currency to charge in (GHS, KES and so on); Paystack uses your integration's currency when it is left out.
36
+ # @param split_code [String] The split code (SPL_...) of a previously created split.
37
+ # @param subaccount [String] The code (ACCT_...) of the subaccount that owns the payment.
38
+ # @param bank_transfer [Hash] Settings for the Pay with Transfer and Pesalink channel (account_expires_at, the expiry time of the account).
39
+ # @param qr [Hash] The QR provider details (provider, scan-to-pay being the only one); South Africa only.
40
+ # @param capitec_pay [Hash] The Capitec Pay account holder (identifier_key, one of CELLPHONE, IDNUMBER or ACCOUNTNUMBER, and identifier_value); South Africa only.
41
+ # @return [PaystackSdk::Response] The response from the Paystack API.
42
+ # @raise [PaystackSdk::Error] If a parameter is invalid or the API request fails.
43
+ # @see https://paystack.com/docs/api/charge/#create
44
+ def create(
45
+ email:,
46
+ amount:,
47
+ authorization_code: nil,
48
+ pin: nil,
49
+ reference: nil,
50
+ birthday: nil,
51
+ device_id: nil,
52
+ metadata: nil,
53
+ bank: nil,
54
+ mobile_money: nil,
55
+ ussd: nil,
56
+ eft: nil,
57
+ currency: nil,
58
+ split_code: nil,
59
+ subaccount: nil,
60
+ bank_transfer: nil,
61
+ qr: nil,
62
+ capitec_pay: nil
63
+ )
64
+ validate_presence!(value: email, name: "email")
65
+ validate_email!(email: email, name: "email")
66
+ validate_presence!(value: amount, name: "amount")
67
+ validate_positive_integer!(value: amount, name: "amount")
68
+ validate_reference_format!(reference: reference, name: "reference") unless reference.nil?
69
+
70
+ wire_body = to_wire(
71
+ {
72
+ email:,
73
+ amount:,
74
+ authorization_code:,
75
+ pin:,
76
+ reference:,
77
+ birthday: format_date(birthday, name: "birthday"),
78
+ device_id:,
79
+ metadata:,
80
+ bank:,
81
+ mobile_money:,
82
+ ussd:,
83
+ eft:,
84
+ currency:,
85
+ split_code:,
86
+ subaccount:,
87
+ bank_transfer:,
88
+ qr:,
89
+ capitec_pay:
90
+ },
91
+ WIRE_NAMES
92
+ )
93
+
94
+ handle_response(@connection.post("/charge", wire_body))
37
95
  end
38
96
 
39
- # Submits an OTP for authorising a pending Mobile Money charge (e.g. Vodafone).
97
+ # Submit PIN.
40
98
  #
41
- # @param payload [Hash] Payload containing the OTP and charge reference.
42
- # @option payload [String] :otp The OTP supplied by the customer (required)
43
- # @option payload [String] :reference The charge reference returned from initiation (required)
99
+ # Submit PIN to continue a charge
44
100
  #
45
- # @return [PaystackSdk::Response] The wrapped API response.
46
- # @raise [PaystackSdk::ValidationError] If the payload is invalid.
47
- def submit_otp(payload)
48
- validate_fields!(
49
- payload: payload,
50
- validations: {
51
- otp: {type: :string, required: true},
52
- reference: {type: :reference, required: true}
53
- }
54
- )
101
+ # @param pin [String] Customer's PIN for the ongoing transaction
102
+ # @param reference [String] Transaction reference that requires the PIN
103
+ # @return [PaystackSdk::Response] The response from the Paystack API.
104
+ # @raise [PaystackSdk::Error] If a parameter is invalid or the API request fails.
105
+ # @see https://paystack.com/docs/api/charge/#submit-pin
106
+ def submit_pin(pin:, reference:)
107
+ validate_presence!(value: pin, name: "pin")
108
+ validate_presence!(value: reference, name: "reference")
109
+ validate_reference_format!(reference: reference, name: "reference")
110
+
111
+ wire_body = to_wire({pin:, reference:}, WIRE_NAMES)
55
112
 
56
- response = @connection.post("/charge/submit_otp", payload)
57
- handle_response(response)
113
+ handle_response(@connection.post("/charge/submit_pin", wire_body))
58
114
  end
59
115
 
60
- private
61
-
62
- def validate_mobile_money_payload!(payload)
63
- validate_fields!(
64
- payload: payload,
65
- validations: {
66
- email: {type: :email, required: true},
67
- amount: {type: :positive_integer, required: true},
68
- currency: {type: :currency, required: false},
69
- reference: {type: :reference, required: false},
70
- callback_url: {required: false},
71
- metadata: {required: false},
72
- mobile_money: {required: true}
73
- }
74
- )
116
+ # Submit OTP.
117
+ #
118
+ # Submit OTP to complete a charge
119
+ #
120
+ # @param otp [String] Customer's OTP for ongoing transaction
121
+ # @param reference [String] The reference of the ongoing transaction
122
+ # @return [PaystackSdk::Response] The response from the Paystack API.
123
+ # @raise [PaystackSdk::Error] If a parameter is invalid or the API request fails.
124
+ # @see https://paystack.com/docs/api/charge/#submit-otp
125
+ def submit_otp(otp:, reference:)
126
+ validate_presence!(value: otp, name: "otp")
127
+ validate_presence!(value: reference, name: "reference")
128
+ validate_reference_format!(reference: reference, name: "reference")
129
+
130
+ wire_body = to_wire({otp:, reference:}, WIRE_NAMES)
75
131
 
76
- mobile_money = payload[:mobile_money] || payload["mobile_money"]
77
- validate_hash!(input: mobile_money, name: "mobile_money")
132
+ handle_response(@connection.post("/charge/submit_otp", wire_body))
133
+ end
134
+
135
+ # Submit Phone.
136
+ #
137
+ # Submit phone number when requested
138
+ #
139
+ # @param phone [String] Customer's mobile number
140
+ # @param reference [String] The reference of the ongoing transaction
141
+ # @return [PaystackSdk::Response] The response from the Paystack API.
142
+ # @raise [PaystackSdk::Error] If a parameter is invalid or the API request fails.
143
+ # @see https://paystack.com/docs/api/charge/#submit-phone
144
+ def submit_phone(phone:, reference:)
145
+ validate_presence!(value: phone, name: "phone")
146
+ validate_presence!(value: reference, name: "reference")
147
+ validate_reference_format!(reference: reference, name: "reference")
78
148
 
79
- phone = mobile_money[:phone] || mobile_money["phone"]
80
- validate_presence!(value: phone, name: "mobile_money phone")
149
+ wire_body = to_wire({phone:, reference:}, WIRE_NAMES)
81
150
 
82
- provider = mobile_money[:provider] || mobile_money["provider"]
83
- validate_mobile_money_provider!(provider)
151
+ handle_response(@connection.post("/charge/submit_phone", wire_body))
84
152
  end
85
153
 
86
- def validate_mobile_money_provider!(provider)
87
- normalized = provider&.to_s&.downcase
88
- validate_allowed_values!(
89
- value: normalized,
90
- allowed_values: MOBILE_MONEY_PROVIDERS,
91
- name: "mobile_money provider",
92
- allow_nil: false
154
+ # Submit Birthday.
155
+ #
156
+ # Submit the customer's birthday when requested
157
+ #
158
+ # @param birthday [String] Customer's birthday in the format YYYY-MM-DD e.g 2016-09-21
159
+ # @param reference [String] The reference of the ongoing transaction
160
+ # @return [PaystackSdk::Response] The response from the Paystack API.
161
+ # @raise [PaystackSdk::Error] If a parameter is invalid or the API request fails.
162
+ # @see https://paystack.com/docs/api/charge/#submit-birthday
163
+ def submit_birthday(birthday:, reference:)
164
+ validate_presence!(value: birthday, name: "birthday")
165
+ validate_presence!(value: reference, name: "reference")
166
+ validate_reference_format!(reference: reference, name: "reference")
167
+
168
+ wire_body = to_wire(
169
+ {
170
+ birthday: format_date(birthday, name: "birthday"),
171
+ reference:
172
+ },
173
+ WIRE_NAMES
93
174
  )
175
+
176
+ handle_response(@connection.post("/charge/submit_birthday", wire_body))
177
+ end
178
+
179
+ # Submit Address.
180
+ #
181
+ # Send the details of the customer's address for address verification
182
+ #
183
+ # @param address [String] Customer's address
184
+ # @param city [String] Customer's city
185
+ # @param state [String] Customer's state
186
+ # @param zip_code [String] Customer's zipcode
187
+ # @param reference [String] The reference of the ongoing transaction
188
+ # @return [PaystackSdk::Response] The response from the Paystack API.
189
+ # @raise [PaystackSdk::Error] If a parameter is invalid or the API request fails.
190
+ # @see https://paystack.com/docs/api/charge/#submit-address
191
+ def submit_address(address:, city:, state:, zip_code:, reference:)
192
+ validate_presence!(value: address, name: "address")
193
+ validate_presence!(value: city, name: "city")
194
+ validate_presence!(value: state, name: "state")
195
+ validate_presence!(value: zip_code, name: "zip_code")
196
+ validate_presence!(value: reference, name: "reference")
197
+ validate_reference_format!(reference: reference, name: "reference")
198
+
199
+ wire_body = to_wire({address:, city:, state:, zip_code:, reference:}, WIRE_NAMES)
200
+
201
+ handle_response(@connection.post("/charge/submit_address", wire_body))
94
202
  end
95
203
 
96
- def normalize_mobile_money_provider(payload)
97
- mm = payload[:mobile_money] || payload["mobile_money"] || {}
98
- provider = mm[:provider] || mm["provider"]
99
- normalized_provider = provider&.to_s&.downcase
100
- # Avoid mutating the caller's payload
101
- payload.merge(mobile_money: mm.merge(provider: normalized_provider))
204
+ # Check pending charge.
205
+ #
206
+ # When you get `pending` as a charge status or if there was an exception when calling any of the `/charge` endpoints, wait 10 seconds or more, then make a check to see if its status has changed.
207
+ #
208
+ # @param reference [String] The reference of the ongoing transaction
209
+ # @return [PaystackSdk::Response] The response from the Paystack API.
210
+ # @raise [PaystackSdk::Error] If a parameter is invalid or the API request fails.
211
+ # @see https://paystack.com/docs/api/charge/#check
212
+ def check_pending(reference:)
213
+ validate_presence!(value: reference, name: "reference")
214
+
215
+ handle_response(@connection.get("/charge/#{escape_path(reference, name: "reference")}"))
102
216
  end
103
217
  end
104
218
  end