Class: PaystackSdk::Resources::Charges

Inherits:
Base
  • Object
show all
Defined in:
lib/paystack_sdk/resources/charges.rb

Overview

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.

Constant Summary collapse

MOBILE_MONEY_PROVIDERS =
%w[mtn atl vod mpesa orange wave].freeze

Constants included from Utils::ConnectionUtils

Utils::ConnectionUtils::BASE_URL

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods included from Utils::ConnectionUtils

#create_connection, #initialize_connection

Methods included from Validations

#validate_allowed_values!, #validate_currency!, #validate_date_format!, #validate_email!, #validate_fields!, #validate_hash!, #validate_positive_integer!, #validate_presence!, #validate_reference_format!, #validate_required_params!

Constructor Details

This class inherits a constructor from PaystackSdk::Resources::Base

Instance Method Details

#mobile_money(payload) ⇒ PaystackSdk::Response

Initiates a Mobile Money payment.

Parameters:

  • payload (Hash)

    The payload containing charge details.

Options Hash (payload):

  • :email (String)

    Customer’s email address (required)

  • :amount (Integer)

    Amount in the lowest currency unit (required)

  • :currency (String)

    ISO currency code (default: GHS)

  • :reference (String)

    Optional reference supplied by the merchant

  • :callback_url (String)

    Optional callback URL for Paystack to redirect to

  • :metadata (Hash)

    Optional metadata to attach to the transaction

  • :mobile_money (Hash)

    The mobile money details (required)

    • :phone [String] Customer’s mobile money phone number (required)

    • :provider [String] Mobile money provider code (required)

Returns:

Raises:



31
32
33
34
35
36
37
# File 'lib/paystack_sdk/resources/charges.rb', line 31

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

#submit_otp(payload) ⇒ PaystackSdk::Response

Submits an OTP for authorising a pending Mobile Money charge (e.g. Vodafone).

Parameters:

  • payload (Hash)

    Payload containing the OTP and charge reference.

Options Hash (payload):

  • :otp (String)

    The OTP supplied by the customer (required)

  • :reference (String)

    The charge reference returned from initiation (required)

Returns:

Raises:



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/paystack_sdk/resources/charges.rb', line 47

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