Skip to content
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

add pay with uk and eu account #1

Merged
merged 1 commit into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions lib/flutterwave_sdk.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require_relative "flutterwave_sdk/flutterwave_objects/base/base"
require_relative "flutterwave_sdk/flutterwave_modules/util"
require_relative "flutterwave_sdk/flutterwave_objects/card"
require_relative "flutterwave_sdk/flutterwave_objects/uk_and_eu_account"
require_relative "flutterwave_sdk/flutterwave_objects/mobile_money"
require_relative "flutterwave_sdk/flutterwave_objects/account_payment"
require_relative "flutterwave_sdk/flutterwave_objects/bank_transfer"
Expand Down Expand Up @@ -52,7 +53,7 @@ def base_url
# check if we set our public , secret and encryption keys to the environment variable
if (public_key.nil?)
@public_key = ENV['FLUTTERWAVE_PUBLIC_KEY']
else
else
@public_key = public_key
end

Expand All @@ -77,7 +78,7 @@ def base_url
unless @public_key[0..7] == 'FLWPUBK-' || @public_key[0..11] == 'FLWPUBK_TEST'
raise FlutterwaveBadKeyError, "Invalid public key #{@public_key}"
end

# raise this error if no secret key is passed
unless !@secret_key.nil?
raise FlutterwaveBadKeyError, "No secret key supplied and couldn't find any in environment variables. Make sure to set secret key as an environment variable FLUTTERWAVE_SECRET_KEY"
Expand All @@ -94,11 +95,11 @@ def base_url
end

#tracking activities
def flutterwave_tracking
def flutterwave_tracking
endpoint = "https://kgelfdz7mf.execute-api.us-east-1.amazonaws.com/staging/sendevent"
public_key = @public_key


payload = {
"PBFPubKey" => public_key,
"language" => "Ruby",
Expand All @@ -107,17 +108,17 @@ def flutterwave_tracking
"message" => "test is done"
}
data = payload.to_json

response = HTTParty.post(endpoint, {
body: data,
headers: {
'Content-Type' => 'application/json'
}
})

unless (response.code == 200 || response.code == 201)
raise RaveServerError.new(response), "HTTP Code #{response.code}: #{response.body}"
end
end
return response
end

Expand Down
31 changes: 31 additions & 0 deletions lib/flutterwave_sdk/flutterwave_objects/uk_and_eu_account.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require_relative "base/base.rb"
require 'json'

class UkPayment < Base
def initiate_charge(data)
base_url = flutterwave_object.base_url

# only update the payload with the transaction reference if it isn't already added to the payload
if !data.key?("tx_ref")
data.merge!({"tx_ref" => Util.transaction_reference_generator})
end
# check the currency to determine the type and the required parameters
currency = data["currency"]
if currency == "GBP"
required_parameters = [ "amount", "email", "tx_ref", "currency", "is_token_io"]
type = "account-ach-uk"
elsif currency == "EUR"
required_parameters = [ "amount", "email", "tx_ref", "currency", "is_token_io"]
type = "account-ach-uk"
else
return "pass a valid currency"
end

check_passed_parameters(required_parameters, data)
type = type
payload = data.to_json

response = post_request("#{base_url}#{BASE_ENDPOINTS::CHARGE_ENDPOINT}?type=#{type}", payload)
return response
end
end
71 changes: 71 additions & 0 deletions spec/flutterwave_uk_and_eu_account_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
require 'dotenv'
require 'spec_helper'
require "flutterwave_sdk/flutterwave_objects/uk_and_eu_account"

Dotenv.load

test_public_key = ENV['TEST_PUBLIC_KEY']
test_secret_key = ENV['TEST_SECRET_KEY']
test_encryption_key = ENV['TEST_ENCRYPTION_KEY']

payload = {
"amount" => "100",
"email" => "[email protected]",
"tx_ref" => "MC-#{Time.now.to_i}",
"currency" => "GBP",
"is_token_io" => 1
}

incomplete_payload = {
"amount" => "100",
"email" => "[email protected]",
"tx_ref" => "MC-#{Time.now.to_i}",
"currency" => "GBP",
}

invalid_currency_payload = {
"amount" => "100",
"email" => "[email protected]",
"tx_ref" => "MC-#{Time.now.to_i}",
"currency" => "NGN",
"is_token_io" => 1
}

RSpec.describe UkPayment do
flutterwave = Flutterwave.new(test_public_key, test_secret_key, test_encryption_key)
payment = UkPayment.new(flutterwave)
payment_charge = payment.initiate_charge(payload)


context "when a merchant tries to charge a customers via mobile money" do
it "should return a ach payment object" do
expect(payment.nil?).to eq(false)
end

it 'should raise Error if the ach payload is incomplete' do
begin
incomplete_payment_response = payment.initiate_charge(incomplete_payload)
rescue => e
expect(e.instance_of? IncompleteParameterError).to eq true
end
end

it 'should successfully initate payment and return authorization mode for the charge' do
payload_response = payment_charge
expect(payload_response["data"]["status"]).to eq("pending")
expect(payload_response["message"]).to eq("Charge initiated")
expect(payload_response["meta"]["authorization"]["mode"]).to eq("redirect")
end

it 'should successfully return processor response ' do
payload_response = payment_charge
expect(payload_response["data"]["processor_response"]).to eq("Transaction is pending authentication")
end

it 'should successfully return the auth model' do
payload_response = payment_charge
expect(payload_response["data"]["auth_model"]).to eq("TOKEN")
end

end
end