-
Notifications
You must be signed in to change notification settings - Fork 1
Add Mobile Money channel support via Charges resource #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,11 +1,11 @@ | ||
| #!/usr/bin/env ruby | ||
| # frozen_string_literal: true | ||
|
|
||
| require "bundler/setup" | ||
| require "paystack_sdk" | ||
| require 'bundler/setup' | ||
| require 'paystack_sdk' | ||
|
|
||
| # You can add fixtures and/or initialization code here to make experimenting | ||
| # with your gem easier. You can also use a different console, if you like. | ||
|
|
||
| require "irb" | ||
| require 'irb' | ||
| IRB.start(__FILE__) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative "base" | ||
|
|
||
| module PaystackSdk | ||
| module Resources | ||
| # The `Charges` resource exposes helpers for initiating and managing charges | ||
| # through alternative payment channels such as Mobile Money. | ||
| # | ||
| # At the moment the SDK focuses on supporting the Mobile Money channel which | ||
| # requires posting to the `/charge` endpoint with the customer's email, | ||
| # amount, currency, and the provider specific `mobile_money` payload. | ||
| class Charges < PaystackSdk::Resources::Base | ||
| MOBILE_MONEY_PROVIDERS = %w[mtn atl vod mpesa orange wave].freeze | ||
|
|
||
| # Initiates a Mobile Money payment. | ||
| # | ||
| # @param payload [Hash] The payload containing charge details. | ||
| # @option payload [String] :email Customer's email address (required) | ||
| # @option payload [Integer] :amount Amount in the lowest currency unit (required) | ||
| # @option payload [String] :currency ISO currency code (default: GHS) | ||
| # @option payload [String] :reference Optional reference supplied by the merchant | ||
| # @option payload [String] :callback_url Optional callback URL for Paystack to redirect to | ||
| # @option payload [Hash] :metadata Optional metadata to attach to the transaction | ||
| # @option payload [Hash] :mobile_money The mobile money details (required) | ||
| # - :phone [String] Customer's mobile money phone number (required) | ||
| # - :provider [String] Mobile money provider code (required) | ||
| # | ||
| # @return [PaystackSdk::Response] The wrapped API response. | ||
| # @raise [PaystackSdk::ValidationError] If the payload is invalid. | ||
| def mobile_money(payload) | ||
| validate_mobile_money_payload!(payload) | ||
|
|
||
| normalized_payload = normalize_mobile_money_provider(payload) | ||
| response = @connection.post("/charge", normalized_payload) | ||
| handle_response(response) | ||
| end | ||
|
|
||
| # Submits an OTP for authorising a pending Mobile Money charge (e.g. Vodafone). | ||
| # | ||
| # @param payload [Hash] Payload containing the OTP and charge reference. | ||
| # @option payload [String] :otp The OTP supplied by the customer (required) | ||
| # @option payload [String] :reference The charge reference returned from initiation (required) | ||
| # | ||
| # @return [PaystackSdk::Response] The wrapped API response. | ||
| # @raise [PaystackSdk::ValidationError] If the payload is invalid. | ||
| def submit_otp(payload) | ||
| validate_fields!( | ||
| payload: payload, | ||
| validations: { | ||
| otp: {type: :string, required: true}, | ||
| reference: {type: :reference, required: true} | ||
| } | ||
| ) | ||
|
|
||
| response = @connection.post("/charge/submit_otp", payload) | ||
| handle_response(response) | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def validate_mobile_money_payload!(payload) | ||
| validate_fields!( | ||
| payload: payload, | ||
| validations: { | ||
| email: {type: :email, required: true}, | ||
| amount: {type: :positive_integer, required: true}, | ||
| currency: {type: :currency, required: false}, | ||
| reference: {type: :reference, required: false}, | ||
| callback_url: {required: false}, | ||
| metadata: {required: false}, | ||
| mobile_money: {required: true} | ||
| } | ||
| ) | ||
|
|
||
| mobile_money = payload[:mobile_money] || payload["mobile_money"] | ||
| validate_hash!(input: mobile_money, name: "mobile_money") | ||
|
|
||
| phone = mobile_money[:phone] || mobile_money["phone"] | ||
| validate_presence!(value: phone, name: "mobile_money phone") | ||
|
|
||
| provider = mobile_money[:provider] || mobile_money["provider"] | ||
| validate_mobile_money_provider!(provider) | ||
| end | ||
|
|
||
| def validate_mobile_money_provider!(provider) | ||
| normalized = provider&.to_s&.downcase | ||
| validate_allowed_values!( | ||
| value: normalized, | ||
| allowed_values: MOBILE_MONEY_PROVIDERS, | ||
| name: "mobile_money provider", | ||
| allow_nil: false | ||
| ) | ||
| end | ||
|
|
||
| def normalize_mobile_money_provider(payload) | ||
| mm = payload[:mobile_money] || payload["mobile_money"] || {} | ||
| provider = mm[:provider] || mm["provider"] | ||
| normalized_provider = provider&.to_s&.downcase | ||
| # Avoid mutating the caller's payload | ||
| payload.merge(mobile_money: mm.merge(provider: normalized_provider)) | ||
| end | ||
| end | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice refactors, sweet.