Advertisement
Guest User

Untitled

a guest
Jul 29th, 2018
306
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 40.50 KB | None | 0 0
  1. diff --git a/api-srv.rb b/api-srv.rb
  2. index 24042d0..5ee28ea 100644
  3. --- a/api-srv.rb
  4. +++ b/api-srv.rb
  5. @@ -4,13 +4,11 @@ require 'eventmachine'
  6. require 'thin'
  7. require 'active_merchant'
  8. require 'pp'
  9. -basereq 'db_recurring'
  10. basereq 'data_types'
  11. basereq 'log/log'
  12. basereq 'srv-common'
  13. basereq 'mail/mail'
  14. basereq 'recur/billing_logic'
  15. -basereq 'recur/billing_logic_new'
  16. basereq 'extn/gateway'
  17. basereq 'db/log'
  18. require_goat 'notifications.rb'
  19. @@ -62,17 +60,13 @@ class APIResponse
  20. pretty = pretty.gsub(/\[\s+\]/, '[]').gsub(/\{\s+\}/, '{}')
  21. pretty
  22. end
  23. -
  24. - def add_error_details(dets={})
  25. - dets.each{|k,v| @error[k] = v} if @error
  26. - end
  27. -
  28. +
  29. def error=(e)
  30. raise "We already have an error" if @error
  31. @error = e
  32. end
  33.  
  34. - def card_error(msg, param, code=nil, note=nil, charge=nil)
  35. + def card_error(msg, param, code=nil, note=nil)
  36. h = {
  37. 'message' => msg.sym_to_s,
  38. 'type' => 'card_error'
  39. @@ -81,9 +75,10 @@ class APIResponse
  40. h['param'] = param.sym_to_s if param
  41. h['code'] = code.sym_to_s if code
  42. h['note'] = note if note
  43. +
  44. self.error = h
  45. end
  46. -
  47. +
  48. def card_error_from_validator(er)
  49. card_error(er[:message], er[:detail], er[:code], er[:note])
  50. end
  51. @@ -108,7 +103,6 @@ class APIResponse
  52. def data=(hash)
  53. @data = hash
  54. end
  55. -
  56. end
  57.  
  58. class APIHacks
  59. @@ -284,7 +278,7 @@ class PaymentGateway
  60. end
  61.  
  62. @@errors_seen = YAML.load_file('/tmp/responses_seen.yaml') rescue []
  63. - def self.errors_for_firstdata(response, gateway, charge)
  64. + def self.errors_for_firstdata(response, gateway)
  65. errors = []
  66. params = response.params
  67. rcode = params['rcode']
  68. @@ -320,34 +314,32 @@ class PaymentGateway
  69. :gateway => gateway,
  70. :mailable => true,
  71. :response => response.params,
  72. - :charge => charge.try(:token),
  73. :rtext => rtext)
  74. errors << CardValidator::Processing_Error
  75. end
  76. # Do this until we have a handle on the kinds of responses to come back
  77. unless @@errors_seen.include?(rcode)
  78. - @@errors_seen.push(rcode)
  79. + @@errors_seen.push rcode
  80. File.open('/tmp/responses_seen.yaml', 'w') { |f| f.write YAML.dump(@@errors_seen) }
  81. Log.log_internal_event('new-firsdata-response',
  82. :message => "Firstdata error codes seen on this server: #{@@errors_seen.inspect}",
  83. :gateway => gateway,
  84. :mailable => true,
  85. :response => response.params,
  86. - :charge => charge.try(:token),
  87. :rtext => rtext)
  88. end
  89.  
  90. errors
  91. end
  92.  
  93. - def self.errors_for(response, charge)
  94. + def self.errors_for(response)
  95. case response.gateway
  96. when 'authorize'
  97. errors_for_authnet(response)
  98. when 'merchantpartners'
  99. errors_for_mps(response)
  100. when 'firstdata_north', 'firstdata_north_test', 'firstdata_nashville', 'firstdata_nashville_test'
  101. - errors_for_firstdata(response, response.gateway, charge)
  102. + errors_for_firstdata(response, response.gateway)
  103. else
  104. raise "Unknown gateway type #{response.params['gateway'].inspect}"
  105. end
  106. @@ -453,11 +445,12 @@ class PaymentGateway
  107. rh = self.gateway_request(authz, authz.mrchgateway, 'authorization', :authorize, authz.amount, credit_card, opts)
  108. end
  109.  
  110. - def self.attempt_charge(charge, card, opts={})
  111. + def self.attempt_charge(charge, card)
  112. credit_card = self.cardify(card)
  113. - opts.merge!({ :trace => charge.numeric_id })
  114. - invoice = charge.load_invoice
  115. - opts[:recurring] = true if invoice && !card.cvc && !opts.key?(:recurring)
  116. +
  117. + opts = { :trace => charge.numeric_id }
  118. + recon = charge.load_recon
  119. + opts.merge!(:recurring => true) if recon && !card.cvc
  120.  
  121. gw = MrchGateway.load(charge.mrchgateway)
  122. case gw.gateway
  123. @@ -663,35 +656,6 @@ module CustomerCommon
  124. end
  125. end
  126.  
  127. -module PlanCommon
  128. - def load_plan(plan_code)
  129. - p = Plan.load_alive(:merchant => @merchant.token, :identifier => plan_code)
  130. - unless p
  131. - raise DPException.new("No such plan: #{plan_code}", 'identifier', 404)
  132. - end
  133. - p
  134. - end
  135. -end
  136. -
  137. -module InvoiceItemCommon
  138. - def load_invoice_item(tok)
  139. - item = InvoiceItem.load_alive(:merchant => @merchant.token, :_id => tok)
  140. - unless item
  141. - raise DPException.new("No such invoice item: #{tok}", 'id', 404)
  142. - end
  143. - item
  144. - end
  145. -end
  146. -
  147. -module CouponCommon
  148. - def load_coupon(code)
  149. - c = Coupon.load_one(:merchant => @merchant.token, :code => code)
  150. - raise DPException.new("No such coupon: #{code}", 'code', 404) unless c
  151. - raise InvalidAPIRequestError.new("Coupon expired: #{code}") unless c.valid?
  152. - c
  153. - end
  154. -end
  155. -
  156. module CommonValidations
  157. ULONG_MAX = 2147483647 # 2**31 - 1. Max 32-bit int in Mongo.
  158.  
  159. @@ -821,7 +785,7 @@ module ChargeCommon
  160.  
  161. rhash = PaymentGateway.attempt_authorization(charge, card)
  162. charge.authorized = rhash[:success]
  163. - charge.auth_errors = charge.authorized ? [] : PaymentGateway.errors_for(rhash[:response], charge)
  164. + charge.auth_errors = charge.authorized ? [] : PaymentGateway.errors_for(rhash[:response])
  165. charge.auth_gw_token = rhash[:response].authorization
  166. charge.auth_avs_result = rhash[:response].avs_result[:code]
  167. charge.save
  168. @@ -837,7 +801,7 @@ module ChargeCommon
  169. charge
  170. end
  171.  
  172. - def attempt_charge(charge, card, opts={})
  173. + def attempt_charge(charge, card)
  174. maybe_load_cvc!(card)
  175. # If we didn't get a CVC with this card, see if we have its CVC in the temp storage (maybe this card was from the JS bindings)
  176. charge.card = card.token
  177. @@ -852,12 +816,13 @@ module ChargeCommon
  178. if card.error
  179. raise "This card has an error defined -- why are we trying to charge it?"
  180. end
  181. +
  182. # We get an ActiveMerchant::Billing::Response object,
  183. # annotated with a :gateway param specifying which gateway
  184. - rhash = PaymentGateway.attempt_charge(charge, card, opts)
  185. + rhash = PaymentGateway.attempt_charge(charge, card)
  186.  
  187. charge.authorized = rhash[:success]
  188. - charge.auth_errors = charge.authorized ? [] : PaymentGateway.errors_for(rhash[:response], charge)
  189. + charge.auth_errors = charge.authorized ? [] : PaymentGateway.errors_for(rhash[:response])
  190.  
  191. charge.auth_gw_token = rhash[:response].authorization
  192. charge.auth_avs_result = rhash[:response].avs_result[:code]
  193. @@ -876,7 +841,7 @@ module ChargeCommon
  194.  
  195.  
  196. # The HTTP_X_STRIPE_REQUEST is set in apiori and can be trusted
  197. - if (Dynamic.variable?(:request) && request = Dynamic[:request]) && (request.env['HTTP_X_STRIPE_REQUEST'] == 'healthcheck')
  198. + if (request = Dynamic[:request]) && (request.env['HTTP_X_STRIPE_REQUEST'] == 'healthcheck')
  199. mailable = false
  200. else
  201. mailable = charge.livemode
  202. @@ -946,7 +911,6 @@ class ExecuteChargeMethod < APIMethod
  203. optional :id, :string
  204. optional :customer, :string
  205. optional :mnemonic, :string
  206. - optional :recurring, :valid_boolean
  207. optional :recon, :string
  208. optional :force_recon, :valid_boolean
  209.  
  210. @@ -955,11 +919,16 @@ class ExecuteChargeMethod < APIMethod
  211. tok = param?[:id]
  212. cust = param?[:customer]
  213. card = param?[:card]
  214. + recon = param?[:recon]
  215. +
  216. if amt && cur && !tok
  217. # ok. we'll need to create a charge object for this transaction
  218. elsif !amt && !cur && tok
  219. # ok. we have a charge token already.
  220. + elsif @key.system && recon && !amt && !cur && !tok
  221. + # fine, we're paying a recon
  222. else
  223. + # raise InvalidAPIRequestError.new('You must supply either charge id or an amount')
  224. raise InvalidAPIRequestError.new('You must supply an amount and currency.')
  225. end
  226.  
  227. @@ -967,11 +936,14 @@ class ExecuteChargeMethod < APIMethod
  228. # okay, normal execute_charge customer
  229. elsif !card && cust
  230. # grand, we'll use the card on the customer's account
  231. + elsif @key.system && recon && !card && !cust
  232. + # fine, we're paying a recon
  233. else
  234. raise InvalidAPIRequestError.new('You must supply either a card dictionary or a customer id')
  235. end
  236.  
  237. CommonValidations.validate_card_error(card, @key, "A card can only have the error param with test transactions")
  238. +
  239.  
  240. if @key.publishable && !card
  241. # Stop people randomly charging a merchant's customers
  242. @@ -980,15 +952,30 @@ class ExecuteChargeMethod < APIMethod
  243. end
  244. end
  245.  
  246. + def maybe_load_recon
  247. + recon = param?[:recon]
  248. + if recon
  249. + @recon = CustomerReconciliation.load(recon)
  250. + raise InvalidAPIRequestError.new('No such CustomerReconciliation') unless @recon && @recon.merchant == @merchant.token
  251. + end
  252. + end
  253. +
  254. def maybe_load_customer
  255. mrchid = param?[:customer]
  256. - if mrchid
  257. + if @recon
  258. + @cust = Customer.load(@recon.customer)
  259. + raise "Trying to execute charge on a recon without a customer" if !@cust
  260. + elsif mrchid
  261. @cust = load_customer(mrchid)
  262. end
  263. end
  264.  
  265. def get_amount
  266. + if @recon
  267. + [@recon.charge_amount, @recon.currency]
  268. + else
  269. [param?[:amount], param?[:currency]]
  270. + end
  271. end
  272.  
  273. def load_charge(tok)
  274. @@ -1006,16 +993,17 @@ class ExecuteChargeMethod < APIMethod
  275.  
  276. def get_charge
  277. ch_tok = param?[:id]
  278. +
  279. if ch_tok
  280. c = load_charge(ch_tok)
  281. else
  282. amt, cur = get_amount
  283. - chargs = {}
  284. - chargs[:mnemonic] = param?[:mnemonic] if param?[:mnemonic]
  285. - chargs[:recurring] = param?[:recurring] if param?[:recurring]
  286. + return nil if amt == 0 && @recon
  287. +
  288. + chargs = {:mnemonic => param?[:mnemonic]}
  289. chargs.merge!(:customer => @cust.token) if @cust
  290. + chargs.merge!(:recon => @recon.token) if @recon
  291. c = prepare_charge(@key.livemode, amt, cur, chargs)
  292. - return nil if amt == 0 && @recon
  293. end
  294.  
  295. c
  296. @@ -1030,6 +1018,14 @@ class ExecuteChargeMethod < APIMethod
  297. end
  298. end
  299.  
  300. + def update_recon
  301. + @recon.paid = true if @ch && @ch.paid
  302. + @recon.charge = @ch.token if @ch
  303. + @recon.attempted_at = Time.stamp
  304. + @recon.settled_at = Time.stamp
  305. + @recon.save
  306. + end
  307. +
  308. def do_charge_success_tasks
  309. # Requirements for emailing the receipt:
  310. # - Merchant has an email address
  311. @@ -1043,6 +1039,7 @@ class ExecuteChargeMethod < APIMethod
  312. def respond
  313. begin
  314. Merchant.update(@merchant.token) do
  315. + maybe_load_recon
  316. maybe_load_customer
  317.  
  318. if @recon && (@cust.suspended || (@cust.delinquent && !param?[:force_recon]))
  319. @@ -1059,6 +1056,12 @@ class ExecuteChargeMethod < APIMethod
  320. @card = get_card
  321.  
  322. if !@card
  323. + if @recon
  324. + @recon.charge = @ch.token if @ch
  325. + @recon.attempted_at = Time.stamp
  326. + @recon.settled_at = Time.stamp
  327. + @recon.save
  328. + end
  329. return @response.card_error('There is no active card on this account.', nil, 'no_card')
  330. end
  331.  
  332. @@ -1069,25 +1072,40 @@ class ExecuteChargeMethod < APIMethod
  333. end
  334.  
  335. # reasons for not charging
  336. - if @ch
  337. - opts = {}
  338. - opts[:recurring] = param?[:recurring] if param?[:recurring]
  339. - @ch = attempt_charge(@ch, @card, opts)
  340. - end
  341. + if !@ch
  342. + assert(@recon, "Recon cannot be #{@recon.inspect} if charge is #{@ch.inspect}", true)
  343. + # no @ch means charge not necessary, because the amount was 0
  344. + else
  345. + @ch = attempt_charge(@ch, @card)
  346. + end
  347.  
  348. Merchant.update(@merchant.token) do
  349. + if @recon
  350. + # If @ch is nil, then the only action taken will be to do update_recon.
  351. + # This is equivalent to a serial ordering of these requests, however.
  352. + # TODO: someone should examine the semantics of recon updating and
  353. + # things.
  354. + @recon = CustomerReconciliation.load(@recon.token)
  355. + @recon.charge = @ch.token if @ch
  356. + update_recon
  357. + end
  358. do_charge_success_tasks if @ch && @ch.paid
  359. +
  360. if @ch && @cust
  361. ccj = {:_id => @ch.token, :customer => @cust.token}
  362. ccj[:recon] = @recon.token if @recon
  363. DB.db['charge_customer_join'].insert(ccj)
  364. end
  365. -
  366. - if @ch && @ch.auth_errors.length > 0
  367. +
  368. + if @recon
  369. + @response.data = {:id => @recon.token,
  370. + :attempted => (@ch ? @ch.attempted : false),
  371. + :charge => @recon.charge,
  372. + :paid => (@ch ? @ch.paid : nil)}
  373. + # TODO fix
  374. + elsif @ch && @ch.auth_errors.length > 0
  375. puts "ch is #{@ch.inspect}"
  376. @response.card_error_from_validator(@ch.auth_errors.first)
  377. - # if system key, describe charge even when errors for invoice payer to get failed charge tokens
  378. - @response.add_error_details({:charge => @ch.token}) if @key.system
  379. else
  380. @response.data = DataTypes.describe_charge(@ch)
  381. end
  382. @@ -1297,9 +1315,6 @@ end
  383. class UpdateCustomerMethod < APIMethod
  384. include CustomerCommon
  385. include ChargeCommon
  386. - include PlanCommon
  387. - include CouponCommon
  388. -
  389. attr_accessor :should_create # If we're creating customer first
  390.  
  391. def validate_request
  392. @@ -1311,14 +1326,10 @@ class UpdateCustomerMethod < APIMethod
  393.  
  394. optional :card, :valid_card
  395. optional :mnemonic, :string
  396. - optional :plan, :string
  397. - optional :coupon, :string
  398. optional :subscription, :valid_subscription
  399. optional :schedule
  400. optional :suspended, :valid_boolean
  401. optional :validate, :valid_boolean
  402. - optional :prorate, :valid_boolean
  403. - optional :email, :string
  404.  
  405. if param?[:schedule] and !self.should_create
  406. raise InvalidAPIRequestError.new('The schedule param can only be set when creating a new customer.', 'schedule')
  407. @@ -1331,18 +1342,42 @@ class UpdateCustomerMethod < APIMethod
  408. id ||= String.random(20)
  409. existing = Customer.load_one(:merchant => @merchant.token, :mrchid => id, :deleted => false)
  410. raise DPException.new('Customer already exists.') if existing
  411. +
  412. + prebill = nil
  413. + case param?[:schedule]
  414. + when 'prebill'
  415. + prebill = true
  416. + when 'postbill'
  417. + prebill = false
  418. + when nil
  419. + # okay
  420. + else
  421. + raise InvalidAPIRequestError.new('Invalid value for schedule')
  422. + end
  423.  
  424. c = Customer.init(id)
  425. c.livemode = @key.livemode
  426. c.merchant = @merchant.token
  427. + c.prebill = prebill
  428. c
  429. end
  430. +
  431. + def payable_recons
  432. + # If cr exists, that has to be the only recon
  433. + return [@cr] if @cr
  434.  
  435. - def set_parameters(customer)
  436. - mnem, susp, email = param?[:mnemonic], param?[:suspended], param?[:email]
  437. + CustomerReconciliation.load_all(:customer => @cust.token).select{|rec| (rec.charge && !rec.paid) || (!rec.attempted_at && !rec.settled_at) }
  438. + end
  439. +
  440. + def set_basic_params
  441. + mnem = param?[:mnemonic]
  442. + susp = param?[:suspended]
  443.  
  444. - @cust.mnemonic = mnem if mnem && !mnem.empty?
  445. - @cust.email = email if email && !email.empty?
  446. + if mnem && mnem.empty?
  447. + @cust.mnemonic = nil
  448. + elsif mnem
  449. + @cust.mnemonic = mnem
  450. + end
  451.  
  452. if susp == 'true' || susp == 'True'
  453. @cust.suspended = true
  454. @@ -1352,68 +1387,48 @@ class UpdateCustomerMethod < APIMethod
  455.  
  456. @cust.save if !self.should_create
  457. end
  458. -
  459. - def generate_subscription(plan, previous=nil)
  460. -
  461. - sub = Subscription.init
  462. - sub.status = "active"
  463. - sub.customer = @cust.token
  464. - sub.merchant = @merchant.token
  465. - sub.plan = plan.token
  466. -
  467. - if previous
  468. - interval_change = previous.plan_.interval != sub.plan_.interval
  469. - if interval_change
  470. - previous.ended_at = Time.stamp
  471. - generate_prorations(previous)
  472. - sub.start = previous.current_period_end = previous.ended_at
  473. - else
  474. - sub.current_period_start = previous.current_period_start
  475. - sub.current_period_end = previous.current_period_end
  476. - previous.ended_at = sub.start = Time.stamp
  477. - end
  478. - else
  479. - if plan.trial_period_days
  480. - sub.trial_start = sub.start = Time.stamp
  481. - sub.trial_end = sub.start_.advance(:days => plan.trial_period_days).to_i
  482. - else
  483. - sub.start = Time.stamp
  484. - end
  485. +
  486. + def delete_existing_cb
  487. + existing = CustomerBill.load_one(:customer => @cust.token, :deleted => {'$ne' => true}, :delayed_start => @cb.start) || CustomerBill.load_one(:customer => @cust.token, :created => @cb.start, :delayed_start => nil, :deleted => {'$ne' => true})
  488. + if existing
  489. + existing.deleted = true
  490. + existing.save
  491. end
  492. - prorate = param?[:prorate] == 'false' ? false : true
  493. - generate_prorations(previous, sub) if previous && prorate && !interval_change
  494. - sub
  495. - end
  496. -
  497. - def new_proration
  498. - p = InvoiceItem.proration
  499. - p.merchant = @merchant.token
  500. - p.currency = 'usd'
  501. - p.customer = @cust.token
  502. - p.created = Time.stamp
  503. - p.generate_token
  504. - p
  505. end
  506.  
  507. - def generate_prorations(old_sub, new_sub=nil)
  508. - period_end = old_sub.current_period_end.to_f
  509. - period = (period_end - old_sub.current_period_start).to_f
  510. - old_price = old_sub.plan_.amount.to_f
  511. - remaining_percent = (period_end - old_sub.ended_at.to_f) / period
  512. - credit_amount = remaining_percent * old_price
  513. - credit = new_proration
  514. - credit.amount = -credit_amount.round
  515. - credit.description = "Pro-rated credit for remaining unused time on #{old_sub.plan_.name} plan on #{old_sub.end_.pretty_date}"
  516. - credit.save
  517. + def generate_customerbill
  518. + sub = param?[:subscription]
  519. + @cb = CustomerBill.new
  520. + @cb.generate_token
  521. + @cb.created = Time.stamp
  522. + @cb.delayed_start = sub[:start].to_i if sub[:start]
  523. + @cb.merchant = @merchant.token
  524. + @cb.amount = sub[:amount].to_i
  525. + @cb.currency = sub[:currency]
  526. + @cb.per = sub[:per]
  527. + @cb.mrchid = @cust.mrchid
  528. + @cb.customer = @cust.token
  529. + end
  530.  
  531. - if new_sub
  532. - new_price = new_sub.plan_.amount.to_f
  533. - debit_amount = remaining_percent * new_price
  534. - debit = new_proration
  535. - debit.amount = debit_amount.round
  536. - debit.description = "Pro-rated charge for plan change to #{new_sub.plan_.name} on #{new_sub.start_.pretty_date}"
  537. - debit.save
  538. - end
  539. + def maybe_create_recon
  540. + if @cb && @cust.should_prebill && CustomerBill.load_alives(:customer => @cust.token).empty? && (@cb.start <= Time.stamp)
  541. + @cr = ReconciliationCalculator.new(@cust, [@cb]).calculate_next_recon
  542. + end
  543. + end
  544. +
  545. + def maybe_create_charge
  546. + # ideally, we would pay all the recons, but it gets super messy if
  547. + # one succeeds but then the next doesn't
  548. + @recon = payable_recons.first
  549. + return nil unless @recon && @card
  550. + return nil if @cust.suspended
  551. +
  552. + if @recon.charge
  553. + charge = Charge.load(@recon.charge)
  554. + else
  555. + charge = prepare_charge(@cust.livemode, @recon.amount, @recon.currency)
  556. + end
  557. + charge
  558. end
  559.  
  560. def save_db_objects
  561. @@ -1422,47 +1437,55 @@ class UpdateCustomerMethod < APIMethod
  562. # call maybe_create_recon again) even though maybe_create_recon uses @cust because it only
  563. # uses @cust's token and merchant fields, which we know won't change while we released
  564. # the lock
  565. - # @recon = payable_recons.first
  566. + @recon = payable_recons.first
  567. # Very unlikely, but card might have changed since we made it
  568. @card = Card.load(@card.token) if @card
  569. @cust = Customer.load(@cust.token) if !self.should_create
  570.  
  571. if @charge
  572. if @charge.paid || !self.should_create
  573. - DB.db['charge_customer_join'].insert(:_id => @charge.token, :customer => @cust.token, :invoice => @invoice.token)
  574. + DB.db['charge_customer_join'].insert(:_id => @charge.token, :customer => @cust.token, :recon => @recon.token)
  575. + @recon.charge = @charge.token
  576. + @recon.attempted_at = Time.stamp
  577. + @recon.settled_at = Time.stamp
  578. + @recon.save
  579. end
  580. - unless @charge.paid
  581. +
  582. + if @charge.paid
  583. + @recon.paid = true and @recon.save
  584. + NotificationDict.charge_success(@recon, @cust, @charge.token, @card)
  585. + else
  586. + NotificationDict.charge_failure(@recon, @cust) unless self.should_create
  587. err = @charge.errors.first
  588. raise BadUserInputError.new(err[:message], err[:detail].to_s, err[:code].to_s)
  589. end
  590. end
  591. add_card_to_customer if @card
  592.  
  593. - # update card - try to pay latest unpaid invoice
  594. - if @card && !self.should_create && (current = @cust.current_subscription) && current.past_due?
  595. - old_invoice = current.latest_invoice(q={:closed_at => nil, :paid => false})
  596. - InvoicePayer.new(old_invoice).pay
  597. - end
  598. -
  599. - @invoice.paid = @charge.paid and @invoice.charge = @charge.token if @invoice and @charge
  600. - [@charge, @sub, @invoice, @previous, @discount].each{|obj| obj.save if obj}
  601. - NotificationDict.recurring_payment_succeeded(@invoice) if @invoice and @invoice.paid
  602. + @charge.save if @charge
  603. + # No need to re-fetch as this hasn't been saved yet
  604. + delete_existing_cb if param?[:subscription]
  605. + @cb.save if @cb
  606. + @recon.save if @recon
  607. @cust.save
  608. end
  609.  
  610. def get_customer(tok)
  611. - self.should_create ? create_customer(tok) : load_customer(tok)
  612. + if self.should_create
  613. + create_customer(tok)
  614. + else
  615. + load_customer(tok)
  616. + end
  617. end
  618.  
  619. - def new_card(c)
  620. + def maybe_save_card(c)
  621. return nil unless c
  622. - card = get_card_obj(c)
  623. + @card = get_card_obj(c)
  624.  
  625. if param?[:validate]
  626. - err = validate_card(@key.livemode, card).errors.first
  627. + err = validate_card(@key.livemode, @card).errors.first
  628. raise BadUserInputError.new(err[:message], err[:detail].to_s, err[:code].to_s) if err
  629. end
  630. - card
  631. end
  632.  
  633. def add_card_to_customer
  634. @@ -1470,62 +1493,32 @@ class UpdateCustomerMethod < APIMethod
  635. @cust.active_card = @card.token
  636. end
  637.  
  638. - def generate_discount
  639. - coupon = load_coupon(param?[:coupon])
  640. - d = Discount.init
  641. - d.merchant = @merchant.token
  642. - d.customer = @cust.token
  643. - d.coupon = coupon.token
  644. - d
  645. + def prepare_for_paying_recon
  646. + @charge = maybe_create_charge
  647. + if @charge
  648. + raise "Attempting simultaneous recon pay attempts on the same charge" if @charge.in_use
  649. + @charge.in_use = true
  650. + @charge.save
  651. + end
  652. end
  653.  
  654. def respond
  655. begin
  656. Merchant.update(@merchant.token) do
  657. @cust = get_customer(@params[:id])
  658. - set_parameters(@cust)
  659. -
  660. - @discount = generate_discount if param?[:coupon]
  661. -
  662. - if param?[:plan]
  663. - plan_code = param?[:plan]
  664. - plan = load_plan(plan_code)
  665. - @previous = @cust.active_subscription
  666. - unless @previous and @previous.plan == plan.token
  667. - @sub = generate_subscription(plan,previous=@previous)
  668. - # Invoice the customer immediately if new or there's no active subscription or the interval is changing
  669. - if self.should_create or !@previous or @previous.plan_.free? or @previous.plan_.interval != @sub.plan_.interval
  670. - @invoice = Invoicer.invoice_new_subscription(@cust, @sub, @discount)
  671. - end
  672. - end
  673. - end
  674. + set_basic_params
  675. + generate_customerbill if param?[:subscription]
  676. + maybe_create_recon
  677. end
  678. -
  679. - @card = new_card(param?[:card]) if param?[:card]
  680. +
  681. + maybe_save_card(param?[:card])
  682.  
  683. Merchant.update(@merchant.token) do
  684. - if @invoice
  685. - if @invoice.amount > 0
  686. - @invoice.attempted_at = Time.stamp
  687. - @charge = prepare_charge(@cust.livemode, @invoice.amount, @invoice.currency)
  688. - else
  689. - @invoice.closed_at = Time.stamp
  690. - @invoice.save
  691. - end
  692. - end
  693. -
  694. - if @charge
  695. - raise "Attempting simultaneous recon pay attempts on the same charge" if @charge.in_use
  696. - @charge.in_use = true
  697. - @charge.save
  698. - end
  699. + prepare_for_paying_recon
  700. end
  701.  
  702. # TODO: don't drop lock if you don't have to
  703. - card_to_use = @card || @cust.current_card
  704. - raise InvalidAPIRequestError.new('You must supply a valid card') if !card_to_use && @charge
  705. -
  706. - @charge = attempt_charge(@charge, card_to_use, {:recurring => true}) if @charge
  707. + @charge = attempt_charge(@charge, @card) if @charge
  708.  
  709. Merchant.update(@merchant.token) do
  710. save_db_objects
  711. @@ -1549,7 +1542,9 @@ class PublishablyUpdateCustomerMethod < UpdateCustomerMethod
  712.  
  713. def respond
  714. Merchant.update(@merchant.token) { @cust = get_customer(@params[:id]) }
  715. - @card = new_card(param?[:card])
  716. + maybe_save_card(param?[:card])
  717. + Merchant.update(@merchant.token) { prepare_for_paying_recon }
  718. +
  719. p = attempt_charge(@charge, @card) if @charge
  720.  
  721. Merchant.update(@merchant.token) { save_db_objects }
  722. @@ -1557,87 +1552,6 @@ class PublishablyUpdateCustomerMethod < UpdateCustomerMethod
  723. end
  724. end
  725.  
  726. -class UpdateInvoiceItemMethod < APIMethod
  727. - include InvoiceItemCommon
  728. - def validate_request
  729. - required :id, :string
  730. - optional :amount, :valid_integer_amount
  731. - optional :description, :string
  732. - end
  733. -
  734. - def respond
  735. - Merchant.update(@merchant.token) do
  736. - item = load_invoice_item(param![:id])
  737. - item.amount = param?[:amount].to_i if param?[:amount]
  738. - item.description = param?[:description] if param?[:description]
  739. - item.save
  740. - @response.data = DataTypes.describe_invoice_item(item)
  741. - end
  742. - end
  743. -end
  744. -
  745. -class DeleteInvoiceItemMethod < APIMethod
  746. - include InvoiceItemCommon
  747. - def validate_request
  748. - required :id, :string
  749. - end
  750. -
  751. - def respond
  752. - Merchant.update(@merchant.token) do
  753. - item = load_invoice_item(params[:id])
  754. - item.deleted_at = Time.stamp
  755. - @response.data = {:id => item.token, :deleted => true}
  756. - end
  757. - end
  758. -end
  759. -
  760. -class CancelSubscriptionMethod <APIMethod
  761. - include CustomerCommon
  762. -
  763. - def validate_request
  764. - required :id, :string
  765. - # optional :refund, :string
  766. - optional :cancel_at_end_of_period, :valid_boolean
  767. - end
  768. -
  769. - def respond
  770. - Merchant.update(@merchant.token) do
  771. - c = load_customer(param![:id])
  772. - current = c.current_subscription
  773. - if current
  774. - if param?[:cancel_at_end_of_period] == 'true'
  775. - current.cancel_at_end_of_period = true
  776. - current.canceled_at = Time.stamp
  777. - else
  778. - current.status = "canceled"
  779. - current.ended_at = current.canceled_at = Time.stamp
  780. - end
  781. - current.save
  782. - # refund_subscription(current) if param?[:refund]
  783. - end
  784. -
  785. - # close out any pending prorations
  786. - InvoiceItem.load_all(:customer => c.token, :created => {'$gte' => c.last_assessment_date}).each{|b| b.deleted_at = Time.stamp and b.save }
  787. - @response.data = DataTypes.describe_customer(c)
  788. - end
  789. - end
  790. -
  791. - def refund_subscription(canceled_sub)
  792. - latest = canceled_sub.latest_invoice(:paid => true)
  793. - charge = Charge.load(latest.charge)
  794. - if charge && charge.paid
  795. - refund_amount = case param?[:refund]
  796. - when 'partial'
  797. - canceled_sub.unused_amount
  798. - when 'full'
  799. - charge.amount
  800. - else
  801. - raise InvalidAPIRequestError.new('Invalid value for refund')
  802. - end
  803. - end
  804. - end
  805. -end
  806. -
  807. class DeleteCustomerMethod < APIMethod
  808. include CustomerCommon
  809.  
  810. @@ -1662,25 +1576,22 @@ class BillCustomerMethod < APIMethod
  811. required :id, :string
  812. required :amount, :valid_integer_amount
  813. required :currency, :valid_currency
  814. - optional :description, :string
  815. end
  816.  
  817. def respond
  818. Merchant.update(@merchant.token) do
  819. c = load_customer(param![:id])
  820. - item = InvoiceItem.init
  821. - item.generate_token
  822. - item.description = param?[:description] if param?[:description]
  823. - item.created = Time.stamp
  824. - c.assessment_period = [item.created, Time.at(item.created).advance(:months => 1).to_i] if c.next_assessment_date.nil?
  825. - item.merchant = @merchant.token
  826. - item.amount = param![:amount].to_i
  827. - item.currency = param![:currency]
  828. - item.customer = c.token
  829. - item.save
  830. - c.save
  831. -
  832. - @response.data = DataTypes.describe_invoice_item(item)
  833. +
  834. + cb = CustomerBill.new
  835. + cb.generate_token
  836. + cb.created = Time.stamp
  837. + cb.merchant = @merchant.token
  838. + cb.amount = param![:amount].to_i
  839. + cb.currency = param![:currency]
  840. + cb.mrchid = c.mrchid
  841. + cb.customer = c.token
  842. + cb.save
  843. + @response.data = DataTypes.describe_customer(c)
  844. end
  845. end
  846. end
  847. @@ -1757,14 +1668,14 @@ class BumpCustomerMonthMethod < APIMethod
  848.  
  849. def backshift(obj)
  850. obj.created = (obj.created_ << 1).to_i
  851. - obj.delayed_start = (Time.at(obj.delayed_start) << 1).to_i if obj.is_a?(InvoiceItem) && obj.delayed_start
  852. + obj.delayed_start = (Time.at(obj.delayed_start) << 1).to_i if obj.is_a?(CustomerBill) && obj.delayed_start
  853. obj.from = (obj.from_ << 1).to_i if obj.is_a?(CustomerReconciliation)
  854. obj.until = (obj.until_ << 1).to_i if obj.is_a?(CustomerReconciliation)
  855. obj.save
  856. end
  857.  
  858. def backshift_dates
  859. - cbs = InvoiceItem.load_all(:customer => @c.token)
  860. + cbs = CustomerBill.load_all(:customer => @c.token)
  861. raise BadUserInputError.new("No bills for customer", 'id') if cbs.empty?
  862.  
  863. recons = CustomerReconciliation.load_all(:customer => @c.token)
  864. @@ -1796,23 +1707,16 @@ class API
  865. "execute_charge" => ExecuteChargeMethod,
  866. "refund_charge" => RefundChargeMethod,
  867. "all_charges" => :all_charges,
  868. - "customer_invoices" => :customer_invoices,
  869. - "active_subscription" => :active_subscription,
  870. - "change_subscription" => :change_subscription,
  871. - "retrieve_invoice" => :retrieve_invoice,
  872. - "retrieve_invoice_item" => :retrieve_invoice_item,
  873. - "update_invoice_item" => UpdateInvoiceItemMethod,
  874. - "delete_invoice_item" => :DeleteInvoiceItemMethod,
  875. - "customer_charges" => :customer_charges,
  876. - "upcoming_invoice" => :upcoming_invoice,
  877. +
  878. "create_customer" => :create_customer,
  879. "update_customer" => :update_customer,
  880. "delete_customer" => DeleteCustomerMethod,
  881. "bill_customer" => BillCustomerMethod,
  882. "retrieve_customer" => RetrieveCustomerMethod,
  883. "all_customers" => :all_customers,
  884. +
  885. "create_card" => CreateCardMethod,
  886. - "cancel_subscription" => CancelSubscriptionMethod,
  887. +
  888. 'trigger_error' => :trigger_error,
  889. 'running' => :current_version
  890. }
  891. @@ -1855,80 +1759,7 @@ class API
  892. resp.data = charges
  893. resp
  894. end
  895. -
  896. - def customer_invoices(params)
  897. - cust = Customer.load_one(:mrchid => params[:customer_id])
  898. - invoices = Invoice._find(:customer => cust.token)
  899. - invoices = invoices.map {|i| DataTypes.describe_invoice(Invoice.load(i['_id'])) }
  900. - resp = APIResponse.new
  901. - resp.data = invoices
  902. - resp
  903. - end
  904. -
  905. - def customer_charges(params)
  906. - cust = Customer.load_one(:mrchid => params[:customer_id])
  907. - charges = cust.load_charges.map{|c| DataTypes.describe_charge(c)}
  908. - resp = APIResponse.new
  909. - resp.data = charges
  910. - resp
  911. - end
  912. -
  913. - def upcoming_invoice(params)
  914. - cust = Customer.load_one(:mrchid => params[:customer_id])
  915. - invoice = Invoice.generate(cust, {:calculation_only => true})
  916. - invoice_desc = DataTypes.describe_invoice(invoice)
  917. - [:id, :date, :period_start, :period_end].each { |param| invoice_desc.delete(param)}
  918. - resp = APIResponse.new
  919. - resp.data = invoice_desc
  920. - resp
  921. - end
  922. -
  923. - def active_subscription(params)
  924. - cust = Customer.load_one(:mrchid => params[:customer_id])
  925. - unless sub = cust.active_subscription
  926. - raise DPException.new("No active subscription for customer: #{params[:customer_id]}", nil, 404)
  927. - end
  928. - resp = APIResponse.new
  929. - resp.data = DataTypes.describe_subscription(sub)
  930. - resp
  931. - end
  932. -
  933. - def change_subscription(params)
  934. - puts "hello odongos: #{id}"
  935. - cust = Customer.load_one(:mrchid => params[:id])
  936. - puts "hihihi"
  937. - update_customer(params, false)
  938. - resp = APIResponse.new
  939. - resp.data = DataTypes.describe_subscription(cust.active_subscription)
  940. - resp
  941. - end
  942. -
  943. - def retrieve_invoice(params)
  944. - tok = params[:id]
  945. -
  946. - i = Invoice.load(tok)
  947. - if (not i) or i.merchant != self.merchant.token
  948. - raise DPException.new("Invalid invoice id: #{tok}", nil, 404)
  949. - end
  950. -
  951. - resp = APIResponse.new
  952. - resp.data = DataTypes.describe_invoice(i)
  953. - resp
  954. - end
  955. -
  956. - def retrieve_invoice_item(params)
  957. - tok = params[:id]
  958. -
  959. - item = InvoiceItem.load(tok)
  960. - if (not item) or item.merchant != self.merchant.token
  961. - raise DPException.new("Invalid invoice item id: #{tok}", nil, 404)
  962. - end
  963.  
  964. - resp = APIResponse.new
  965. - resp.data = DataTypes.describe_invoice_item(item)
  966. - resp
  967. - end
  968. -
  969. def all_customers(params)
  970. per_page = params[:count] ? params[:count].to_i : 10
  971. offset = params[:offset] ? params[:offset].to_i : 0
  972. @@ -1995,6 +1826,7 @@ class API
  973. i = self.new
  974. i.merchant = mrch
  975. i.key = MerchantKey.load(params[:key])
  976. +
  977. target = self.method(i.key, m)
  978. raise DPException.new("No such API method: #{m}") unless target
  979.  
  980. @@ -2025,12 +1857,11 @@ class APISrv < PaySrv
  981. # COMPATIBILITY helpers with older API
  982. helpers do
  983. def execute_old_style_api_request(name)
  984. -
  985.  
  986. @new_params.merge! params.reject { |key, value|
  987. ['method', 'key'].include?(key) #we don't want ?key= or ?method=
  988. }
  989. -
  990. +
  991. v1_common_response_handler()
  992.  
  993. raise InvalidAPIRequestError.new("Method not implemented.") if !name
  994. @@ -2047,7 +1878,6 @@ class APISrv < PaySrv
  995. def v1_initialize_request_params()
  996. @new_params = params
  997.  
  998. -
  999. @common_log_params = {}
  1000. reqid = String.randkey('apireq')
  1001.  
  1002. @@ -2124,10 +1954,6 @@ class APISrv < PaySrv
  1003. execute_old_style_api_request('retrieve_charge')
  1004. end
  1005.  
  1006. - get '/v1/customers/:customer_id/charges' do
  1007. - execute_old_style_api_request('customer_charges')
  1008. - end
  1009. -
  1010. post '/v1/charges/:id' do
  1011. if params["action"] == "refund"
  1012. execute_old_style_api_request('refund_charge')
  1013. @@ -2155,78 +1981,35 @@ class APISrv < PaySrv
  1014. end
  1015.  
  1016. post '/v1/customers/:id' do
  1017. - req = case params['action']
  1018. - when 'update'
  1019. - 'update_customer'
  1020. - when 'delete'
  1021. - 'delete_customer'
  1022. - when 'cancel_subscription'
  1023. - 'cancel_subscription'
  1024. - else
  1025. - nil
  1026. - end
  1027. - execute_old_style_api_request(req)
  1028. - end
  1029. -
  1030. - # SUBSCRIPTION API
  1031. -
  1032. - get '/v1/customers/:customer_id/subscription' do
  1033. - execute_old_style_api_request("active_subscription")
  1034. - end
  1035. -
  1036. - post '/v1/customers/:id/subscription' do
  1037. - req = case params['action']
  1038. - when 'cancel'
  1039. - 'cancel_subscription'
  1040. - when 'update', 'create'
  1041. - puts "hihihih"
  1042. - 'change_subscription'
  1043. - else
  1044. - nil
  1045. - end
  1046. - execute_old_style_api_request(req)
  1047. - end
  1048. -
  1049. -
  1050. - # INVOICES API
  1051. - get '/v1/invoices' do
  1052. - execute_old_style_api_request('all_invoices')
  1053. - end
  1054. -
  1055. - get '/v1/invoices/:id' do
  1056. - execute_old_style_api_request('retrieve_invoice')
  1057. - end
  1058. -
  1059. - get '/v1/customers/:customer_id/invoices' do
  1060. - execute_old_style_api_request("customer_invoices")
  1061. - end
  1062. -
  1063. - get '/v1/customers/:customer_id/invoices/upcoming' do
  1064. - execute_old_style_api_request("upcoming_invoice")
  1065. - end
  1066. -
  1067. - post '/v1/customers/:id/invoices/upcoming' do
  1068. - if params["action"] == "add"
  1069. - execute_old_style_api_request('bill_customer')
  1070. - else
  1071. - execute_old_style_api_request(nil)
  1072. - end
  1073. - end
  1074. -
  1075. - get '/v1/customers/:customer_id/invoices/upcoming/items/:id' do
  1076. - execute_old_style_api_request('retrieve_invoice_item')
  1077. - end
  1078. -
  1079. - post '/v1/customers/:customer_id/invoices/upcoming/items/:id' do
  1080. if params["action"] == "update"
  1081. - execute_old_style_api_request('update_invoice_item')
  1082. + execute_old_style_api_request('update_customer')
  1083. elsif params["action"] == "delete"
  1084. - execute_old_style_api_request('delete_invoice_item')
  1085. + execute_old_style_api_request('delete_customer')
  1086. else
  1087. execute_old_style_api_request(nil)
  1088. end
  1089. end
  1090. - before '/v1' do
  1091. +
  1092. + # SUBSCRIPTION API
  1093. +
  1094. + #get '/v1/customers/:customer_id/subscriptions' do
  1095. + #end
  1096. +
  1097. + #post '/v1/customers/:id/bills/create' do
  1098. + # execute_old_style_api_request('bill_customer')
  1099. + #end
  1100. +
  1101. + #get '/v1/customers/:customer_id/subscriptions/:subscription_id' do
  1102. + #end
  1103. +
  1104. + #post '/v1/customers/:customer_id/subscriptions/:subscription_id' do
  1105. + #end
  1106. +
  1107. + #delete '/v1/customers/:customer_id/bills/:id' do
  1108. + #end
  1109. +
  1110. +
  1111. + before '/v1' do
  1112. v1_initialize_request_params()
  1113. end
  1114.  
  1115. @@ -2262,84 +2045,15 @@ class APISrv < PaySrv
  1116. post '/v1' do
  1117. v1_common_response_handler()
  1118.  
  1119. - # convert old params
  1120. - @new_params = convert_from_old_params(@new_params)
  1121. -
  1122. # Create variable in this scope
  1123. @resp = nil
  1124. Dynamic.let :request => request do
  1125. @resp = API.invoke(@new_params[:method], @mrch, @new_params)
  1126. end
  1127. - @resp = convert_to_old_style_response(@new_params[:method], @resp)
  1128. +
  1129. return_response(@resp)
  1130. end
  1131.  
  1132. - def convert_from_old_params(params)
  1133. - # temporarily support old subscription parameters
  1134. - if params[:subscription]
  1135. - sub = params[:subscription]
  1136. - amount, interval, currency, delayed_start = sub[:amount], sub[:per], sub[:currency], sub[:start]
  1137. - q = {:merchant => @mrch.token, :interval => interval, :amount => amount.to_i}
  1138. -
  1139. - if delayed_start
  1140. - trial_days = ((delayed_start.to_i - Time.stamp) / (24 * 60 * 60)).round if delayed_start
  1141. - q.merge!({:trial_period_days => trial_days})
  1142. - end
  1143. -
  1144. - plan = Plan.load_one(q)
  1145. - unless plan
  1146. - count = Plan._find.count
  1147. - plan = Plan.init
  1148. - plan.name = "Plan #{count + 1}"
  1149. - plan.amount = amount.to_i
  1150. - plan.interval = interval
  1151. - plan.currency = currency
  1152. - plan.identifier = "plan#{count + 1}"
  1153. - plan.trial_period_days = trial_days if trial_days
  1154. - plan.merchant = @mrch.token
  1155. - plan.save
  1156. - end
  1157. - params.delete(:subscription)
  1158. - params[:plan] = plan.identifier
  1159. - end
  1160. - params.delete(:schedule)
  1161. - params
  1162. - end
  1163. -
  1164. - def old_style_customer(resp)
  1165. - cust = Customer._find_one(:mrchid => resp.data[:id]).token
  1166. - subscriptions = Subscription.load_all(:customer => cust).sort_by(&:start)
  1167. - resp.data[:subscription_history] = subscriptions.map do |sub|
  1168. - plan = sub.plan_
  1169. - h = {:amount => plan.amount, :per => plan.interval, :currency => plan.currency, :start => sub.start.to_i}
  1170. - h[:end] = sub.ended_at.to_i if sub.ended_at
  1171. - h
  1172. - end
  1173. - resp.data[:schedule] = 'prebill' # force prebill
  1174. - # legacy nonsense from before
  1175. - legacy = @mrch.created < 1294079244
  1176. - resp.data[:subscriptions] = resp.data[:subscription_history] if legacy
  1177. - resp.data.delete(:subscription)
  1178. - resp.data[:bills] = resp.data[:invoiceitems]
  1179. - resp.data.delete(:invoiceitems)
  1180. - resp
  1181. - end
  1182. -
  1183. - def convert_to_old_style_response(method, resp)
  1184. - case method
  1185. - when 'retrieve_charge', 'execute_charge'
  1186. - resp.data.delete(:invoice)
  1187. - when 'bill_customer'
  1188. - c = InvoiceItem.load(resp.data[:id]).customer
  1189. - resp.data = DataTypes.describe_customer(Customer.load(c))
  1190. - resp = old_style_customer(resp)
  1191. - when 'create_customer', 'update_customer', 'retrieve_customer'
  1192. - resp = old_style_customer(resp)
  1193. - end
  1194. - resp
  1195. - end
  1196. -
  1197. -
  1198. error [BadUserInputError, DPException, InvalidAPIRequestError, InvalidAPICredentialsError] do
  1199. @common_log_params ||= { :common_log_params_was_nil => true }
  1200. begin
  1201. @@ -2430,28 +2144,35 @@ class APISrv < PaySrv
  1202. end
  1203.  
  1204. APISrv.configure do |srv|
  1205. - $port = PayConf.get('api-bind-port') || 15003
  1206. + set_svcname PayConf.get('api-srv-svcname') || 'bapi-srv'
  1207. + pidfile
  1208. + standard_log
  1209. +
  1210. + DB::initdb
  1211.  
  1212. + $port = PayConf.get('api-bind-port') || 15003
  1213. +
  1214. srv.set :raise_errors, false
  1215. srv.set :show_exceptions, false
  1216. - srv.set :static, true
  1217. +
  1218. + srv.set :static
  1219. srv.set :public, File.dirname(__FILE__) + '/stripejs/public/'
  1220. srv.set :views, File.dirname(__FILE__) + '/api/views'
  1221. srv.set :port, $port
  1222. srv.set :bind, PayConf.get('api-bind-host') || '127.0.0.1'
  1223. srv.disable_logging # stops using the *normal* logger, instead we put in the NoHealthCheckLogger below
  1224. srv.use NoHealthCheckLogger
  1225. +
  1226. + if $0 == __FILE__ || $0 == File.expand_path(__FILE__)
  1227. + Goat::QueueClient.configure(:host => PayConf.get('queue-host'),
  1228. + :port => PayConf.get('queue-port'),
  1229. + :prefix => PayConf.get('version'))
  1230. + Goat::NotificationCenter.initialize_queue
  1231. + emit_notifications
  1232. + end
  1233. +
  1234. end
  1235.  
  1236. -if Sys.invoked?(__FILE__)
  1237. - set_svcname PayConf.get('api-srv-svcname') || 'bapi-srv'
  1238. - pidfile
  1239. - standard_log
  1240. - Goat::QueueClient.configure(:host => PayConf.get('queue-host'),
  1241. - :port => PayConf.get('queue-port'),
  1242. - :prefix => PayConf.get('version'))
  1243. - Goat::NotificationCenter.initialize_queue
  1244. - emit_notifications
  1245. - DB::initdb
  1246. +if $0 == __FILE__ || $0 == File.expand_path(__FILE__)
  1247. APISrv.run!
  1248. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement