diff --git a/bin/sim_job b/bin/sim_job index 0d0e1c302828b7f5ab2bea88051335692f2d18fc..b67f51687c9830a75e56ec04ed8d4a6d96fee77a 100755 --- a/bin/sim_job +++ b/bin/sim_job @@ -43,6 +43,13 @@ SCHEMA = "{ name : Text, allow_register: Bool, subaccount_discount: Natural + }, + braintree: { + environment : Text, + merchant_accounts : { CAD : Text, USD : Text }, + merchant_id : Text, + private_key : Text, + public_key : Text } }" @@ -55,6 +62,7 @@ CONFIG = Dhall::Coder CONFIG[:keep_area_codes_in] = {} CONFIG[:creds] = {} +require_relative "../lib/async_braintree" require_relative "../lib/blather_notify" require_relative "../lib/customer_repo" require_relative "../lib/low_balance" @@ -62,6 +70,7 @@ require_relative "../lib/postgres" require_relative "../lib/sim_repo" require_relative "../lib/transaction" +BRAINTREE = AsyncBraintree.new(**CONFIG[:braintree]) CUSTOMER_REPO = CustomerRepo.new SIM_REPO = SIMRepo.new diff --git a/lib/async_braintree.rb b/lib/async_braintree.rb new file mode 100644 index 0000000000000000000000000000000000000000..bbbe464037f72f337936a49d9fd04c0bf2f8fef9 --- /dev/null +++ b/lib/async_braintree.rb @@ -0,0 +1,38 @@ +# frozen_string_literal: true + +# Braintree is not async, so wrap in EM.defer for now +class AsyncBraintree + def initialize(environment:, merchant_id:, public_key:, private_key:, **) + @gateway = Braintree::Gateway.new( + environment: environment, + merchant_id: merchant_id, + public_key: public_key, + private_key: private_key + ) + @gateway.config.logger = LOG + end + + def respond_to_missing?(m, *) + @gateway.respond_to?(m) || super + end + + def method_missing(m, *args) + return super unless respond_to_missing?(m, *args) + + EM.promise_defer(klass: PromiseChain) do + @gateway.public_send(m, *args) + end + end + + class PromiseChain < EMPromise + def respond_to_missing?(*) + false && super # We don't actually know what we respond to... + end + + def method_missing(m, *args) + return super if respond_to_missing?(m, *args) + + self.then { |o| o.public_send(m, *args) } + end + end +end diff --git a/sgx_jmp.rb b/sgx_jmp.rb index 84f1b891ae0fc361cf8e44a550816add757b5104..a2e31edaaa5bc8ad14d0b5ccd170d574dc1c0d1b 100644 --- a/sgx_jmp.rb +++ b/sgx_jmp.rb @@ -124,43 +124,7 @@ BANDWIDTH_VOICE = Bandwidth::Client.new( class AuthError < StandardError; end -# Braintree is not async, so wrap in EM.defer for now -class AsyncBraintree - def initialize(environment:, merchant_id:, public_key:, private_key:, **) - @gateway = Braintree::Gateway.new( - environment: environment, - merchant_id: merchant_id, - public_key: public_key, - private_key: private_key - ) - @gateway.config.logger = LOG - end - - def respond_to_missing?(m, *) - @gateway.respond_to?(m) || super - end - - def method_missing(m, *args) - return super unless respond_to_missing?(m, *args) - - EM.promise_defer(klass: PromiseChain) do - @gateway.public_send(m, *args) - end - end - - class PromiseChain < EMPromise - def respond_to_missing?(*) - false && super # We don't actually know what we respond to... - end - - def method_missing(m, *args) - return super if respond_to_missing?(m, *args) - - self.then { |o| o.public_send(m, *args) } - end - end -end - +require_relative "lib/async_braintree" BRAINTREE = AsyncBraintree.new(**CONFIG[:braintree]) def panic(e, hub=nil)