Class: Braintree::CustomerSessionGateway

Inherits:
Object
  • Object
show all
Defined in:
lib/braintree/customer_session_gateway.rb

Constant Summary collapse

CREATE_CUSTOMER_SESSION =
<<~GRAPHQL
  mutation CreateCustomerSession($input: CreateCustomerSessionInput!) {
    createCustomerSession(input: $input) {
      sessionId
    }
  }
GRAPHQL
UPDATE_CUSTOMER_SESSION =
<<~GRAPHQL
  mutation UpdateCustomerSession($input: UpdateCustomerSessionInput!) {
    updateCustomerSession(input: $input) {
      sessionId
    }
  }
GRAPHQL
GET_CUSTOMER_RECOMMENDATIONS =
<<~GRAPHQL
  query CustomerRecommendations($input: CustomerRecommendationsInput!) {
    customerRecommendations(input: $input) {
      isInPayPalNetwork
      recommendations {
        ... on PaymentRecommendations {
          paymentOptions {
            paymentOption
            recommendedPriority
          }
        }
      }
    }
  }
GRAPHQL

Instance Method Summary collapse

Constructor Details

#initialize(gateway, graphql_client) ⇒ CustomerSessionGateway

Returns a new instance of CustomerSessionGateway.



37
38
39
40
# File 'lib/braintree/customer_session_gateway.rb', line 37

def initialize(gateway, graphql_client)
  @gateway = gateway
  @graphql_client = graphql_client
end

Instance Method Details

#create_customer_session(input) ⇒ (Successful|Error)Result

Creates a new customer session.

Example:

customer = {
 email: "test@example.com",
 device_fingerprint_id: "1234",
 phone: {country_phone_code: "1", phone_number: "5555555555"},
 paypal_app_installed: true,
 venmo_app_installed: true,
}
input = Braintree::CreateCustomerSessionInput.new(
             customer: customer,
)
result = gateway.customer_session.create_customer_session(input)
if result.success?
  puts "Created session #{result.session_id}"
else
  puts "Validations failed"
  puts result.errors.first.message
end

Parameters:

Returns:

  • ((Successful|Error)Result)

    A result object with session ID if successful, or errors otherwise.

Raises:

  • (UnexpectedError)

    If there is an unexpected error during the process.



68
69
70
# File 'lib/braintree/customer_session_gateway.rb', line 68

def create_customer_session(input)
  execute_mutation(CREATE_CUSTOMER_SESSION, input, :createCustomerSession)
end

#get_customer_recommendations(customer_recommendations_input) ⇒ Result\Error|Result\Successful

Retrieves customer recommendations associated with a customer session.

Example:

customer = {
 email: "test@example.com",
 device_fingerprint_id: "1234",
 phone: {country_phone_code: "1", phone_number: "5555555555"},
 paypal_app_installed: true,
 venmo_app_installed: true,
}
input = Braintree::CustomerRecommendationsInput.new(
             session_id: "11EF-34BC-2702904B-9026-555555555555",
             customer: customer,
             recommendations: [Braintree::Recommendations::PAYMENT_RECOMMENDATIONS]
)
result = gateway.customer_session.get_customer_recommendations(input)
if result.success?
  puts "Fetched customer recommendations"
  payload = result.customer_recommendations
  puts payload
else
  puts "Validations failed"
  puts result.errors.first.message
end

Parameters:

Returns:

  • (Result\Error|Result\Successful)

    A result object containing the customer recommendations if successful, or errors otherwise.

Raises:

  • (UnexpectedError)

    If there is an unexpected error during the process.



134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/braintree/customer_session_gateway.rb', line 134

def get_customer_recommendations(customer_recommendations_input)
  variables = {"input" => customer_recommendations_input.to_graphql_variables}

  begin
    response = @graphql_client.query(GET_CUSTOMER_RECOMMENDATIONS, variables)
    errors = GraphQLClient.get_validation_errors(response)
    if errors
      ErrorResult.new(@gateway, {errors:errors})
    else
      SuccessfulResult.new(:customer_recommendations => extract_customer_recommendations_payload(response))
    end
  rescue StandardError => e
    raise UnexpectedError, e.message
  end
end

#update_customer_session(input) ⇒ (Successful|Error)Result

Updates an existing customer session.

Example:

customer = {
 email: "test@example.com",
 device_fingerprint_id: "1234",
 phone: {country_phone_code: "1", phone_number: "5555555555"},
 paypal_app_installed: true,
 venmo_app_installed: true,
}
input = Braintree::UpdateCustomerSessionInput.new(
             session_id: "11EF-34BC-2702904B-9026-555555555555",
             customer: customer,
)
result = gateway.customer_session.updated_customer_session(input)
if result.success?
  puts "Updated session #{result.session_id}"
else
  puts "Validations failed"
  puts result.errors.first.message
end

Parameters:

Returns:

  • ((Successful|Error)Result)

    A result object with session ID if successful, or errors otherwise.

Raises:

  • (UnexpectedError)

    If there is an unexpected error during the process.



100
101
102
# File 'lib/braintree/customer_session_gateway.rb', line 100

def update_customer_session(input)
  execute_mutation(UPDATE_CUSTOMER_SESSION, input, :updateCustomerSession)
end