diff --git a/Gemfile.lock b/Gemfile.lock index d31106d..471b489 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -7,8 +7,8 @@ PATH GEM remote: https://rubygems.org/ specs: - addressable (2.7.0) - public_suffix (>= 2.0.2, < 5.0) + addressable (2.8.1) + public_suffix (>= 2.0.2, < 6.0) coderay (1.1.2) coveralls (0.8.23) json (>= 1.8, < 3) @@ -16,21 +16,22 @@ GEM term-ansicolor (~> 1.3) thor (>= 0.19.4, < 2.0) tins (~> 1.6) - crack (0.4.3) - safe_yaml (~> 1.0.0) + crack (0.4.5) + rexml diff-lcs (1.3) docile (1.3.2) faraday (1.0.0) multipart-post (>= 1.2, < 3) - hashdiff (1.0.0) + hashdiff (1.0.1) json (2.3.0) method_source (0.9.2) multipart-post (2.1.1) pry (0.12.2) coderay (~> 1.1.0) method_source (~> 0.9.0) - public_suffix (4.0.3) + public_suffix (5.0.1) rake (13.0.1) + rexml (3.2.5) rspec (3.9.0) rspec-core (~> 3.9.0) rspec-expectations (~> 3.9.0) @@ -44,7 +45,6 @@ GEM diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.9.0) rspec-support (3.9.2) - safe_yaml (1.0.5) simplecov (0.16.1) docile (~> 1.1) json (>= 1.8, < 3) @@ -57,10 +57,10 @@ GEM tins (1.24.1) sync vcr (5.1.0) - webmock (2.3.2) - addressable (>= 2.3.6) + webmock (3.18.1) + addressable (>= 2.8.0) crack (>= 0.3.2) - hashdiff + hashdiff (>= 0.4.0, < 2.0.0) PLATFORMS ruby @@ -73,7 +73,7 @@ DEPENDENCIES rake (~> 13.0) rspec (~> 3.0) vcr (~> 5.1) - webmock (~> 2.1) + webmock (~> 3.18) BUNDLED WITH - 2.0.2 + 2.1.4 diff --git a/lib/momoapi-ruby/client.rb b/lib/momoapi-ruby/client.rb index cf2507f..ea524c2 100644 --- a/lib/momoapi-ruby/client.rb +++ b/lib/momoapi-ruby/client.rb @@ -14,7 +14,7 @@ class Client def send_request(method, path, headers, body = {}) begin auth_token = get_auth_token['access_token'] - conn = Faraday.new(url: Momoapi.config.base_url) + conn = faraday_with_block(url: Momoapi.config.base_url) conn.headers = headers conn.authorization(:Bearer, auth_token) @@ -24,7 +24,7 @@ def send_request(method, path, headers, body = {}) when 'post' response = conn.post(path, body.to_json) end - rescue ArgumentError + rescue ArgumentError => e raise "Missing configuration key(s) for #{@product.capitalize}s" end interpret_response(response) @@ -53,7 +53,7 @@ def get_auth_token(path, subscription_key) "Ocp-Apim-Subscription-Key": subscription_key } url = Momoapi.config.base_url - conn = Faraday.new(url: url) + conn = faraday_with_block(url: url) conn.headers = headers @product = path.split('/')[0] get_credentials(@product) @@ -103,5 +103,17 @@ def get_transaction_status(path, subscription_key) def is_user_active(path, subscription_key) prepare_get_request(path, subscription_key) end + + private + + def faraday_with_block(options) + Faraday.new(options) + block = Momoapi.config.faraday_block + if block + Faraday.new(options, &block) + else + Faraday.new(options) + end + end end end diff --git a/lib/momoapi-ruby/collection.rb b/lib/momoapi-ruby/collection.rb index 9883399..a2de0b7 100644 --- a/lib/momoapi-ruby/collection.rb +++ b/lib/momoapi-ruby/collection.rb @@ -58,6 +58,14 @@ def request_to_pay(phone_number, amount, external_id, path = '/collection/v1_0/requesttopay' send_request('post', path, headers, body) { transaction_reference: uuid } + rescue Exception => error + if uuid + class << error + attr_accessor :transaction_reference + end + error.transaction_reference = uuid + end + raise error end def is_user_active(phone_number) diff --git a/lib/momoapi-ruby/config.rb b/lib/momoapi-ruby/config.rb index 5670d66..ded1fa9 100644 --- a/lib/momoapi-ruby/config.rb +++ b/lib/momoapi-ruby/config.rb @@ -11,7 +11,7 @@ class Config :collection_user_id, :collection_api_secret, :disbursement_primary_key, :disbursement_user_id, :disbursement_api_secret, :remittance_primary_key, - :remittance_user_id, :remittance_api_secret + :remittance_user_id, :remittance_api_secret, :faraday_block def initialize @environment = nil @@ -26,6 +26,7 @@ def initialize @remittance_primary_key = nil @remittance_user_id = nil @remittance_api_secret = nil + @faraday_block = nil # pass Proxy end def base_url diff --git a/lib/momoapi-ruby/remittance.rb b/lib/momoapi-ruby/remittance.rb index 8312545..f668274 100644 --- a/lib/momoapi-ruby/remittance.rb +++ b/lib/momoapi-ruby/remittance.rb @@ -52,6 +52,14 @@ def transfer(phone_number, amount, external_id, path = '/remittance/v1_0/transfer' send_request('post', path, headers, body) { transaction_reference: uuid } + rescue Exception => error + if uuid + class << error + attr_accessor :transaction_reference + end + error.transaction_reference = uuid + end + raise error end def is_user_active(phone_number) diff --git a/momoapi-ruby.gemspec b/momoapi-ruby.gemspec index 0a773cd..83c59e9 100644 --- a/momoapi-ruby.gemspec +++ b/momoapi-ruby.gemspec @@ -32,5 +32,5 @@ Gem::Specification.new do |spec| spec.add_development_dependency 'rake', '~> 13.0' spec.add_development_dependency 'rspec', '~> 3.0' spec.add_development_dependency 'vcr', '~> 5.1' - spec.add_development_dependency 'webmock', '~> 2.1' + spec.add_development_dependency 'webmock', '~> 3.18' end diff --git a/spec/cassettes/Momoapi_Collection/collections/when_no_errors/makes_request_to_pay.yml b/spec/cassettes/Momoapi_Collection/collections/when_no_errors/makes_request_to_pay.yml new file mode 100644 index 0000000..d354a91 --- /dev/null +++ b/spec/cassettes/Momoapi_Collection/collections/when_no_errors/makes_request_to_pay.yml @@ -0,0 +1,87 @@ +--- +http_interactions: +- request: + method: post + uri: https://sandbox.momodeveloper.mtn.com/collection/token/ + body: + encoding: UTF-8 + string: '' + headers: + Ocp-Apim-Subscription-Key: + - b5e0f061fa5a4cbbbd2192530b7cedfb + Authorization: + - Basic NzgzYThmMjEtM2ZjNi00ZTM5LWFhNzYtZGE4ZTU2MmZiYTdlOjMxM2NmYWY5YTQwYjQwMjliNzEzOWY0YzlkOGFmYjIz + Content-Length: + - '0' + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Cache-Control: + - no-store + Pragma: + - no-cache + Content-Length: + - '628' + Content-Type: + - application/json;charset=utf-8 + X-Xss-Protection: + - 1; mode=block + Request-Context: + - appId=cid-v1:e996501c-e721-4ac1-97ff-dc6887b85e8c + Date: + - Fri, 17 Feb 2023 14:56:54 GMT + body: + encoding: UTF-8 + string: '{"access_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSMjU2In0.eyJjbGllbnRJZCI6Ijc4M2E4ZjIxLTNmYzYtNGUzOS1hYTc2LWRhOGU1NjJmYmE3ZSIsImV4cGlyZXMiOiIyMDIzLTAyLTE3VDE1OjU2OjU0LjY1NCIsInNlc3Npb25JZCI6IjQzY2Y1ODBjLTE2MjctNGE5Yy04MzkzLTJkOWUyYjE2NGJmZCJ9.esFByJ58Q5rn_4AIPT1HGyRDG3ax-Y5QYn76MLaFIq7cEPxdk8bQqa1IFHDQEtcdk9a0jbO-tWQ-Yjtk5hubJ1OYhrit59eQ7cbV0_LJeZI3xGNIyVzwOnXd8dw_PEtL_m78twLm9bDDiEQjo7X8ep1hGI8MdFyXBhbGkTMk_fenfJQUEXwrWZolF1U4W5tEkaj_ZOVwGtm1-pjgtnNEgh7DVQ07l7IVxThEZP2X4YdmalifxsM43kzrIZAYYuQ44rtN81FvvOlMI0Pwjaqmrss0BXDJqPbQtjBRTkh6rTOSbZiqq1smecMVYAh3opwvJbisJnOkrROYXLA3XkvraA","token_type":"access_token","expires_in":3600}' + http_version: + recorded_at: Fri, 17 Feb 2023 14:56:54 GMT +- request: + method: post + uri: https://sandbox.momodeveloper.mtn.com/collection/v1_0/requesttopay + body: + encoding: UTF-8 + string: '{"payer":{"partyIdType":"MSISDN","partyId":"0775671360"},"payeeNote":"testing","payerMessage":"testing","externalId":"6353636","currency":"EUR","amount":"5.0"}' + headers: + X-Target-Environment: + - sandbox + Content-Type: + - application/json + X-Reference-Id: + - f2edf507-b754-4a33-9995-177cd9aa217b + Ocp-Apim-Subscription-Key: + - b5e0f061fa5a4cbbbd2192530b7cedfb + Authorization: + - Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSMjU2In0.eyJjbGllbnRJZCI6Ijc4M2E4ZjIxLTNmYzYtNGUzOS1hYTc2LWRhOGU1NjJmYmE3ZSIsImV4cGlyZXMiOiIyMDIzLTAyLTE3VDE1OjU2OjU0LjY1NCIsInNlc3Npb25JZCI6IjQzY2Y1ODBjLTE2MjctNGE5Yy04MzkzLTJkOWUyYjE2NGJmZCJ9.esFByJ58Q5rn_4AIPT1HGyRDG3ax-Y5QYn76MLaFIq7cEPxdk8bQqa1IFHDQEtcdk9a0jbO-tWQ-Yjtk5hubJ1OYhrit59eQ7cbV0_LJeZI3xGNIyVzwOnXd8dw_PEtL_m78twLm9bDDiEQjo7X8ep1hGI8MdFyXBhbGkTMk_fenfJQUEXwrWZolF1U4W5tEkaj_ZOVwGtm1-pjgtnNEgh7DVQ07l7IVxThEZP2X4YdmalifxsM43kzrIZAYYuQ44rtN81FvvOlMI0Pwjaqmrss0BXDJqPbQtjBRTkh6rTOSbZiqq1smecMVYAh3opwvJbisJnOkrROYXLA3XkvraA + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 202 + message: Accepted + headers: + Content-Length: + - '0' + X-Xss-Protection: + - 1; mode=block + Request-Context: + - appId=cid-v1:e996501c-e721-4ac1-97ff-dc6887b85e8c + Date: + - Fri, 17 Feb 2023 14:56:55 GMT + body: + encoding: UTF-8 + string: '' + http_version: + recorded_at: Fri, 17 Feb 2023 14:56:55 GMT +recorded_with: VCR 5.1.0 diff --git a/spec/cassettes/Momoapi_Remittance/remittances/_transfer/when_error/raises_error_with_preset_tx_id.yml b/spec/cassettes/Momoapi_Remittance/remittances/_transfer/when_error/raises_error_with_preset_tx_id.yml new file mode 100644 index 0000000..5475c06 --- /dev/null +++ b/spec/cassettes/Momoapi_Remittance/remittances/_transfer/when_error/raises_error_with_preset_tx_id.yml @@ -0,0 +1,89 @@ +--- +http_interactions: +- request: + method: post + uri: https://sandbox.momodeveloper.mtn.com/remittance/token/ + body: + encoding: UTF-8 + string: '' + headers: + Ocp-Apim-Subscription-Key: + - d314b91c889340b682a9a3144a9ffd1b + Authorization: + - Basic Y2YwMjhkZTQtNzM0MS00MWMwLWI0ZmYtM2ZhMTkwYjc3MjM2OmRjYWNjMTE1ZTZmZDQ2NjliMWRiNjIyZDRiOTQ5OTQ3 + Content-Length: + - '0' + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Cache-Control: + - no-store + Pragma: + - no-cache + Content-Length: + - '628' + Content-Type: + - application/json;charset=utf-8 + X-Xss-Protection: + - 1; mode=block + Request-Context: + - appId=cid-v1:e996501c-e721-4ac1-97ff-dc6887b85e8c + Date: + - Fri, 10 Feb 2023 13:47:33 GMT + body: + encoding: UTF-8 + string: '{"access_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSMjU2In0.eyJjbGllbnRJZCI6ImNmMDI4ZGU0LTczNDEtNDFjMC1iNGZmLTNmYTE5MGI3NzIzNiIsImV4cGlyZXMiOiIyMDIzLTAyLTEwVDE0OjQ3OjMzLjY1NyIsInNlc3Npb25JZCI6IjY2YWI3NjdlLTE4NTctNDE2Yi1hNjZlLThlMjJhMjczMzAxMiJ9.k3If07TfPRWa8lTMR-RN4VHahdVzkRKsmgnAqno3OnILX0fbviMFyiG9Bg7ExUyyoMiH-iyQlx500eGsY0satJPto4tBIE1AdIzii83-USJjgjBBYPmoPjAgZwv7FnU7oJ_ot4irf854ru8SwKRbjBYZzSzawksyLzziwm9RKtglNvVeEyY4W-W7ERDATFffuz6VVabFLvC86sLJKdzKX-0pnwlpJ3RMrNpBDcrNBslikAQQ0HWAPZ7Hs0CSqt55avQh53vX2rT6BVzZLZJsr2FKTFe754ma1Nhb3rphvgvovmw9_jH3KV2hG5rQUY4m-bNXPskX3GMprQrTwnn4RA","token_type":"access_token","expires_in":3600}' + http_version: + recorded_at: Fri, 10 Feb 2023 13:47:33 GMT +- request: + method: post + uri: https://sandbox.momodeveloper.mtn.com/remittance/v1_0/transfer + body: + encoding: UTF-8 + string: '{"payee":{"partyIdType":"MSISDN","partyId":"0775671360"},"payeeNote":"testing","payerMessage":"testing","externalId":"6353636","currency":"EUR","amount":"5.0"}' + headers: + X-Target-Environment: + - sandbox + Content-Type: + - application/json + X-Reference-Id: + - ef7e29c9-ddd2-420b-85a5-5373ca1a48dd + Ocp-Apim-Subscription-Key: + - d314b91c889340b682a9a3144a9ffd1b + Authorization: + - Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSMjU2In0.eyJjbGllbnRJZCI6ImNmMDI4ZGU0LTczNDEtNDFjMC1iNGZmLTNmYTE5MGI3NzIzNiIsImV4cGlyZXMiOiIyMDIzLTAyLTEwVDE0OjQ3OjMzLjY1NyIsInNlc3Npb25JZCI6IjY2YWI3NjdlLTE4NTctNDE2Yi1hNjZlLThlMjJhMjczMzAxMiJ9.k3If07TfPRWa8lTMR-RN4VHahdVzkRKsmgnAqno3OnILX0fbviMFyiG9Bg7ExUyyoMiH-iyQlx500eGsY0satJPto4tBIE1AdIzii83-USJjgjBBYPmoPjAgZwv7FnU7oJ_ot4irf854ru8SwKRbjBYZzSzawksyLzziwm9RKtglNvVeEyY4W-W7ERDATFffuz6VVabFLvC86sLJKdzKX-0pnwlpJ3RMrNpBDcrNBslikAQQ0HWAPZ7Hs0CSqt55avQh53vX2rT6BVzZLZJsr2FKTFe754ma1Nhb3rphvgvovmw9_jH3KV2hG5rQUY4m-bNXPskX3GMprQrTwnn4RA + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 409 + message: Conflict + headers: + Content-Length: + - '99' + Content-Type: + - application/json;charset=utf-8 + X-Xss-Protection: + - 1; mode=block + Request-Context: + - appId=cid-v1:e996501c-e721-4ac1-97ff-dc6887b85e8c + Date: + - Fri, 10 Feb 2023 13:47:34 GMT + body: + encoding: UTF-8 + string: '{"message":"Duplicated reference id. Creation of resource failed.","code":"RESOURCE_ALREADY_EXIST"}' + http_version: + recorded_at: Fri, 10 Feb 2023 13:47:34 GMT +recorded_with: VCR 5.1.0 diff --git a/spec/cassettes/Momoapi_Remittance/remittances/_transfer/when_error/saves_tx_id_anyway.yml b/spec/cassettes/Momoapi_Remittance/remittances/_transfer/when_error/saves_tx_id_anyway.yml new file mode 100644 index 0000000..8938e80 --- /dev/null +++ b/spec/cassettes/Momoapi_Remittance/remittances/_transfer/when_error/saves_tx_id_anyway.yml @@ -0,0 +1,87 @@ +--- +http_interactions: +- request: + method: post + uri: https://sandbox.momodeveloper.mtn.com/remittance/token/ + body: + encoding: UTF-8 + string: '' + headers: + Ocp-Apim-Subscription-Key: + - d314b91c889340b682a9a3144a9ffd1b + Authorization: + - Basic Y2YwMjhkZTQtNzM0MS00MWMwLWI0ZmYtM2ZhMTkwYjc3MjM2OmRjYWNjMTE1ZTZmZDQ2NjliMWRiNjIyZDRiOTQ5OTQ3 + Content-Length: + - '0' + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Cache-Control: + - no-store + Pragma: + - no-cache + Content-Length: + - '628' + Content-Type: + - application/json;charset=utf-8 + X-Xss-Protection: + - 1; mode=block + Request-Context: + - appId=cid-v1:e996501c-e721-4ac1-97ff-dc6887b85e8c + Date: + - Fri, 10 Feb 2023 13:39:16 GMT + body: + encoding: UTF-8 + string: '{"access_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSMjU2In0.eyJjbGllbnRJZCI6ImNmMDI4ZGU0LTczNDEtNDFjMC1iNGZmLTNmYTE5MGI3NzIzNiIsImV4cGlyZXMiOiIyMDIzLTAyLTEwVDE0OjM5OjE2LjMyOSIsInNlc3Npb25JZCI6IjAzNTUwMGE2LTE2OGUtNGUwOS1hMWMzLTBlYThiMDE1M2JhZiJ9.jZ784B7ZtGc3sQyU8bvTAfXPn8VjH9xt_l4O1IX4sB313ZVvn8vEvXQWNsKFchEJDjYiW57kTgn8E_92-NdhwskURfwzDOSoQYW3Ci-oLLXwznGqWZYEhZBOmgOZf4n9lzlAwir3EmdZwU9KBam7taN8TB3yaAY2SPW2rVqYUuDNIYMNgmyx4pPgfKtINbcZ1_7T9f2XUVIcbSKQHcJEdMixbRP1PgV_fGZgQ3mYZwmT9-sae5mppYCyHA09JBClUoIakej7QDHGut51JR62PkE6UOQVnWxQLPHjpJDIfdmTt-kSYE1iuJ8FIJBzcPeoxOZ-IKlryvD0I6S3VLBaSg","token_type":"access_token","expires_in":3600}' + http_version: + recorded_at: Fri, 10 Feb 2023 13:39:16 GMT +- request: + method: post + uri: https://sandbox.momodeveloper.mtn.com/remittance/v1_0/transfer + body: + encoding: UTF-8 + string: '{"payee":{"partyIdType":"MSISDN","partyId":"0775671360"},"payeeNote":"testing","payerMessage":"testing","externalId":"6353636","currency":"EUR","amount":"5.0"}' + headers: + X-Target-Environment: + - sandbox + Content-Type: + - application/json + X-Reference-Id: + - ef7e29c9-ddd2-420b-85a5-5373ca1a48dd + Ocp-Apim-Subscription-Key: + - d314b91c889340b682a9a3144a9ffd1b + Authorization: + - Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSMjU2In0.eyJjbGllbnRJZCI6ImNmMDI4ZGU0LTczNDEtNDFjMC1iNGZmLTNmYTE5MGI3NzIzNiIsImV4cGlyZXMiOiIyMDIzLTAyLTEwVDE0OjM5OjE2LjMyOSIsInNlc3Npb25JZCI6IjAzNTUwMGE2LTE2OGUtNGUwOS1hMWMzLTBlYThiMDE1M2JhZiJ9.jZ784B7ZtGc3sQyU8bvTAfXPn8VjH9xt_l4O1IX4sB313ZVvn8vEvXQWNsKFchEJDjYiW57kTgn8E_92-NdhwskURfwzDOSoQYW3Ci-oLLXwznGqWZYEhZBOmgOZf4n9lzlAwir3EmdZwU9KBam7taN8TB3yaAY2SPW2rVqYUuDNIYMNgmyx4pPgfKtINbcZ1_7T9f2XUVIcbSKQHcJEdMixbRP1PgV_fGZgQ3mYZwmT9-sae5mppYCyHA09JBClUoIakej7QDHGut51JR62PkE6UOQVnWxQLPHjpJDIfdmTt-kSYE1iuJ8FIJBzcPeoxOZ-IKlryvD0I6S3VLBaSg + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 202 + message: Accepted + headers: + Content-Length: + - '0' + X-Xss-Protection: + - 1; mode=block + Request-Context: + - appId=cid-v1:e996501c-e721-4ac1-97ff-dc6887b85e8c + Date: + - Fri, 10 Feb 2023 13:39:16 GMT + body: + encoding: UTF-8 + string: '' + http_version: + recorded_at: Fri, 10 Feb 2023 13:39:16 GMT +recorded_with: VCR 5.1.0 diff --git a/spec/cassettes/Momoapi_Remittance/remittances/_transfer/when_no_errors/creates_tx.yml b/spec/cassettes/Momoapi_Remittance/remittances/_transfer/when_no_errors/creates_tx.yml new file mode 100644 index 0000000..da24b25 --- /dev/null +++ b/spec/cassettes/Momoapi_Remittance/remittances/_transfer/when_no_errors/creates_tx.yml @@ -0,0 +1,87 @@ +--- +http_interactions: +- request: + method: post + uri: https://sandbox.momodeveloper.mtn.com/remittance/token/ + body: + encoding: UTF-8 + string: '' + headers: + Ocp-Apim-Subscription-Key: + - d314b91c889340b682a9a3144a9ffd1b + Authorization: + - Basic Y2YwMjhkZTQtNzM0MS00MWMwLWI0ZmYtM2ZhMTkwYjc3MjM2OmRjYWNjMTE1ZTZmZDQ2NjliMWRiNjIyZDRiOTQ5OTQ3 + Content-Length: + - '0' + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 200 + message: OK + headers: + Cache-Control: + - no-store + Pragma: + - no-cache + Content-Length: + - '628' + Content-Type: + - application/json;charset=utf-8 + X-Xss-Protection: + - 1; mode=block + Request-Context: + - appId=cid-v1:e996501c-e721-4ac1-97ff-dc6887b85e8c + Date: + - Fri, 17 Feb 2023 15:01:24 GMT + body: + encoding: UTF-8 + string: '{"access_token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJSMjU2In0.eyJjbGllbnRJZCI6ImNmMDI4ZGU0LTczNDEtNDFjMC1iNGZmLTNmYTE5MGI3NzIzNiIsImV4cGlyZXMiOiIyMDIzLTAyLTE3VDE2OjAxOjI0LjI5NSIsInNlc3Npb25JZCI6IjczNTE0Y2ExLTY4NmQtNGQxOC1hMzBlLTk5YzM3YWE4YjhkOSJ9.Vid9VV2Cm9eAepFjhmo8Mv-_8O6Ojro3j8lne8YODkX1cvgIfmcOWUNfAusFEpao_5uyCpTVtNMo3SfAHz0kqcAK-TBpgogthS8D0I7XhChBY_aJ1VxeLFEUJdIhsxQo3j6EXcfwMQsMasdvstbbAzrEi9UOW2_GBxHTcD1ANhOUooiRvtvPHoW5oFimFKqH2lF9UtC9k91AyLaESxGYb_sL6FMsG_RlkvUC4AhO_0ItPmWsSxnl2fT0SuUfmeOPil1wWLJcEupuS9uwiT_FiRTYvDAGzZ961EFA4yAvJpWPamhicwSOCEDLgBNl5X--dYu_h93WhS7Avl8ILWmI_Q","token_type":"access_token","expires_in":3600}' + http_version: + recorded_at: Fri, 17 Feb 2023 15:01:24 GMT +- request: + method: post + uri: https://sandbox.momodeveloper.mtn.com/remittance/v1_0/transfer + body: + encoding: UTF-8 + string: '{"payee":{"partyIdType":"MSISDN","partyId":"0775671360"},"payeeNote":"testing","payerMessage":"testing","externalId":"6353636","currency":"EUR","amount":"5.0"}' + headers: + X-Target-Environment: + - sandbox + Content-Type: + - application/json + X-Reference-Id: + - ef7e29c9-ddd2-420b-85a5-5373ca2a48dd + Ocp-Apim-Subscription-Key: + - d314b91c889340b682a9a3144a9ffd1b + Authorization: + - Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJSMjU2In0.eyJjbGllbnRJZCI6ImNmMDI4ZGU0LTczNDEtNDFjMC1iNGZmLTNmYTE5MGI3NzIzNiIsImV4cGlyZXMiOiIyMDIzLTAyLTE3VDE2OjAxOjI0LjI5NSIsInNlc3Npb25JZCI6IjczNTE0Y2ExLTY4NmQtNGQxOC1hMzBlLTk5YzM3YWE4YjhkOSJ9.Vid9VV2Cm9eAepFjhmo8Mv-_8O6Ojro3j8lne8YODkX1cvgIfmcOWUNfAusFEpao_5uyCpTVtNMo3SfAHz0kqcAK-TBpgogthS8D0I7XhChBY_aJ1VxeLFEUJdIhsxQo3j6EXcfwMQsMasdvstbbAzrEi9UOW2_GBxHTcD1ANhOUooiRvtvPHoW5oFimFKqH2lF9UtC9k91AyLaESxGYb_sL6FMsG_RlkvUC4AhO_0ItPmWsSxnl2fT0SuUfmeOPil1wWLJcEupuS9uwiT_FiRTYvDAGzZ961EFA4yAvJpWPamhicwSOCEDLgBNl5X--dYu_h93WhS7Avl8ILWmI_Q + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + User-Agent: + - Ruby + response: + status: + code: 202 + message: Accepted + headers: + Content-Length: + - '0' + X-Xss-Protection: + - 1; mode=block + Request-Context: + - appId=cid-v1:e996501c-e721-4ac1-97ff-dc6887b85e8c + Date: + - Fri, 17 Feb 2023 15:01:24 GMT + body: + encoding: UTF-8 + string: '' + http_version: + recorded_at: Fri, 17 Feb 2023 15:01:24 GMT +recorded_with: VCR 5.1.0 diff --git a/spec/features/collection_spec.rb b/spec/features/collection_spec.rb index ae41534..e1b597c 100644 --- a/spec/features/collection_spec.rb +++ b/spec/features/collection_spec.rb @@ -30,14 +30,51 @@ .to raise_error(Momoapi::Error) end - it 'makes request to pay' do - expect do - Momoapi::Collection.new.request_to_pay( - '0775671360', - 5.0, '6353636', - 'testing', 'testing', 'EUR' - ) - end .to raise_error(Momoapi::Error) + context 'when error' do + let(:tx_id) { 'ef7e29c9-ddd2-420b-85a5-5373ca1a48d4' } + + before do + allow(SecureRandom).to receive(:uuid).and_return(tx_id) + allow_any_instance_of(described_class).to receive(:send_request).and_raise(Momoapi::Error.new('test error', '400')) + end + + after do + allow(SecureRandom).to receive(:uuid).and_call_original + allow_any_instance_of(described_class).to receive(:send_request).and_call_original + end + + it 'stores tx reference id in error object' do + expect do + Momoapi::Collection.new.request_to_pay( + '0775671360', + 5.0, '6353636', + 'testing', 'testing', 'EUR' + ) + end .to raise_error(Momoapi::Error) do |error| + expect(error.transaction_reference).to eql(tx_id) + end + end + end + + context 'when no errors' do + let(:tx_id) { 'f2edf507-b754-4a33-9995-177cd9aa217b' } + + before do + allow(SecureRandom).to receive(:uuid).and_return(tx_id) + end + + after do + allow(SecureRandom).to receive(:uuid).and_call_original + end + + it 'makes request to pay' do + res = Momoapi::Collection.new.request_to_pay( + '0775671360', + 5.0, '6353636', + 'testing', 'testing', 'EUR' + ) + expect(res).to eql(transaction_reference: tx_id) + end end end end diff --git a/spec/features/remittance_spec.rb b/spec/features/remittance_spec.rb index ef726c4..248c673 100644 --- a/spec/features/remittance_spec.rb +++ b/spec/features/remittance_spec.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true require 'spec_helper' +require 'faraday' +require 'pry' RSpec.describe Momoapi::Remittance do before(:all) do @@ -10,6 +12,9 @@ config.remittance_primary_key = 'd314b91c889340b682a9a3144a9ffd1b' config.remittance_user_id = 'cf028de4-7341-41c0-b4ff-3fa190b77236' config.remittance_api_secret = 'dcacc115e6fd4669b1db622d4b949947' + config.faraday_block = Proc.new do |f| + f.use Faraday::Response::Logger + end end end @@ -30,14 +35,55 @@ .to raise_error(Momoapi::Error) end - it 'makes transfer' do - expect do - Momoapi::Remittance.new.transfer( - '0775671360', - 5.0, '6353636', - 'testing', 'testing', 'EUR' - ) - end .to raise_error(Momoapi::Error) + context '#transfer' do + + context 'when error' do + let(:tx_id) { 'ef7e29c9-ddd2-420b-85a5-5373ca1a48dd' } + + before do + allow(SecureRandom).to receive(:uuid).and_return(tx_id) + allow_any_instance_of(described_class).to receive(:send_request).and_raise(Momoapi::Error.new('test error', '400')) + end + + after do + allow(SecureRandom).to receive(:uuid).and_call_original + allow_any_instance_of(described_class).to receive(:send_request).and_call_original + end + + it 'raises error with preset tx id' do + expect do + res = Momoapi::Remittance.new.transfer( + '0775671360', + 5.0, '6353636', + 'testing', 'testing', 'EUR' + ) + end .to raise_error(Momoapi::Error) do |error| + expect(error.transaction_reference).to eql(tx_id) + end + end + end + + # TODO: add VCRs and generate all API responses + context 'when no errors' do + let(:tx_id) { 'ef7e29c9-ddd2-420b-85a5-5373ca2a48dd' } + + before do + allow(SecureRandom).to receive(:uuid).and_return(tx_id) + end + + after do + allow(SecureRandom).to receive(:uuid).and_call_original + end + + it 'creates tx' do + res = Momoapi::Remittance.new.transfer( + '0775671360', + 5.0, '6353636', + 'testing', 'testing', 'EUR' + ) + expect(res).to eql({transaction_reference: tx_id}) + end + end end end end