braintree_ruby
braintree_ruby copied to clipboard
Raised "expected :plan or :errors" exception when trying to create a new plan with missing mandatory fields
General information
- SDK/Library version: 4.5.0
- Environment: Both sandbox and production
- Language, language version, and OS: Ruby, all versions, cross-platform.
Issue description
While testing for the recently added feature to create plans in 4.5.0, I found out errors are not handled correctly.
Having this:
# frozen_string_literal: true
require "rubygems"
require "braintree"
gateway = Braintree::Gateway.new(
environment: "sandbox",
merchant_id: "merchant_id",
public_key: "public_key",
private_key: "private_key",
logger: Logger.new($stdout, level: Logger::DEBUG)
)
gateway.plan.create(id: "test-id", name: "test-name")
When it's executed, a expected :plan or :errors
exception is raised.
This is the complete debug output:
D, [2021-12-14T15:16:15.435771 #4930] DEBUG -- : [Braintree] [14/Dec/2021 14:16:15 UTC] POST /merchants/merchant_id/plans
D, [2021-12-14T15:16:15.435966 #4930] DEBUG -- : [Braintree] <?xml version="1.0" encoding="UTF-8"?>
[Braintree] <plan>
[Braintree] <id>test-id</id>
[Braintree] <name>test-name</name>
[Braintree] </plan>
I, [2021-12-14T15:16:16.152664 #4930] INFO -- : [Braintree] [14/Dec/2021 14:16:16 UTC] POST /merchants/merchant_id/plans 422
D, [2021-12-14T15:16:16.152775 #4930] DEBUG -- : [Braintree] [14/Dec/2021 14:16:16 UTC] 422 Unprocessable Entity
D, [2021-12-14T15:16:16.153315 #4930] DEBUG -- : [Braintree] <?xml version="1.0" encoding="UTF-8"?>
[Braintree] <api-error-response>
[Braintree] <errors>
[Braintree] <errors type="array"/>
[Braintree] <plan>
[Braintree] <errors type="array">
[Braintree] <error>
[Braintree] <code>82102</code>
[Braintree] <attribute type="symbol">billing_frequency</attribute>
[Braintree] <message>Billing frequency is required.</message>
[Braintree] </error>
[Braintree] <error>
[Braintree] <code>82127</code>
[Braintree] <attribute type="symbol">currency_iso_code</attribute>
[Braintree] <message>Currency is required.</message>
[Braintree] </error>
[Braintree] <error>
[Braintree] <code>82111</code>
[Braintree] <attribute type="symbol">price</attribute>
[Braintree] <message>Price is required.</message>
[Braintree] </error>
[Braintree] </errors>
[Braintree] </plan>
[Braintree] </errors>
[Braintree] <params>
[Braintree] <id>test-id</id>
[Braintree] <name>test-name</name>
[Braintree] </params>
[Braintree] <message>Billing frequency is required.
[Braintree] Currency is required.
[Braintree] Price is required.</message>
[Braintree] </api-error-response>
/Users/fran/.rvm/gems/ruby-3.0.3/gems/braintree-4.5.0/lib/braintree/plan_gateway.rb:112:in `_do_create': expected :plan or :errors (Braintree::UnexpectedError)
from /Users/fran/.rvm/gems/ruby-3.0.3/gems/braintree-4.5.0/lib/braintree/plan_gateway.rb:21:in `create'
from braintree.rb:14:in `<main>'
From what I understand, it could be fixed replacing this part in lib/braintree/plan_gateway.rb:
elsif response[:errors]
ErrorResult.new(@gateway, response[:errors])
else
raise UnexpectedError, "expected :plan or :errors"
With:
elsif response[:api_error_response]
ErrorResult.new(@gateway, response[:api_error_response])
else
raise UnexpectedError, "expected :plan or :api_error_response"
Similar to how it's handled in the update
method in plan_gateway.rb
🙃 thanks for the PR @franjid hopefully we can get this in the next release. Will leave this open until that happens so we can track it.
for internal tracking, ticket 1905
This change was included in version 4.8.0 of the Ruby SDK. Thanks for the call out!