Class: Braintree::CustomerSessionGateway
- Inherits:
-
Object
- Object
- Braintree::CustomerSessionGateway
- 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 mutation GenerateCustomerRecommendations($input: GenerateCustomerRecommendationsInput!) { generateCustomerRecommendations(input: $input) { sessionId isInPayPalNetwork paymentRecommendations { paymentOption recommendedPriority } } } GRAPHQL
Instance Method Summary collapse
-
#create_customer_session(input) ⇒ (Successful|Error)Result
Creates a new customer session.
-
#get_customer_recommendations(customer_recommendations_input) ⇒ Result\Error|Result\Successful
Retrieves customer recommendations associated with a customer session.
-
#initialize(gateway, graphql_client) ⇒ CustomerSessionGateway
constructor
A new instance of CustomerSessionGateway.
-
#update_customer_session(input) ⇒ (Successful|Error)Result
Updates an existing customer session.
Constructor Details
#initialize(gateway, graphql_client) ⇒ CustomerSessionGateway
Returns a new instance of CustomerSessionGateway.
34 35 36 37 |
# File 'lib/braintree/customer_session_gateway.rb', line 34 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: "[email protected]",
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.
end
65 66 67 |
# File 'lib/braintree/customer_session_gateway.rb', line 65 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: "[email protected]",
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.
end
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/braintree/customer_session_gateway.rb', line 131 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 if e..include?("Authorization failed") raise AuthorizationError, e. else raise ServerError, e. end end end |
#update_customer_session(input) ⇒ (Successful|Error)Result
Updates an existing customer session.
Example:
customer = {
email: "[email protected]",
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.
end
97 98 99 |
# File 'lib/braintree/customer_session_gateway.rb', line 97 def update_customer_session(input) execute_mutation(UPDATE_CUSTOMER_SESSION, input, :updateCustomerSession) end |