lib/active_merchant/billing/integrations/bit_pay/notification.rb in activemerchant-1.41.0 vs lib/active_merchant/billing/integrations/bit_pay/notification.rb in activemerchant-1.42.0

- old
+ new

@@ -4,51 +4,69 @@ module Billing #:nodoc: module Integrations #:nodoc: module BitPay class Notification < ActiveMerchant::Billing::Integrations::Notification def complete? - status == "complete" + status == "Completed" end def transaction_id params['id'] end + def item_id + JSON.parse(params['posData'])['orderId'] + rescue JSON::ParserError + end + + def status + case params['status'] + when 'complete' + 'Completed' + when 'confirmed' + 'Pending' + when 'invalid' + 'Failed' + end + end + # When was this payment received by the client. def received_at params['invoiceTime'].to_i end def currency params['currency'] end - def amount - params['price'] + def gross + params['price'].to_f end - # the money amount we received in X.2 decimal. - def btcPrice - params['btcPrice'].to_f - end + def acknowledge(authcode = nil) + uri = URI.parse("#{ActiveMerchant::Billing::Integrations::BitPay.invoicing_url}/#{transaction_id}") - def status - params['status'].downcase - end + http = Net::HTTP.new(uri.host, uri.port) + http.use_ssl = true - def acknowledge(authcode = nil) - authcode == params['posData'] + request = Net::HTTP::Get.new(uri.path) + request.basic_auth @options[:credential1], '' + + response = http.request(request) + + posted_json = JSON.parse(@raw).tap { |j| j.delete('currentTime') } + parse(response.body) + retrieved_json = JSON.parse(@raw).tap { |j| j.delete('currentTime') } + + posted_json == retrieved_json + rescue JSON::ParserError end private - - # Take the posted data and move the relevant data into a hash - def parse(post) - @raw = post.to_s - for line in @raw.split('&') - key, value = *line.scan( %r{^([A-Za-z0-9_.]+)\=(.*)$} ).flatten - params[key] = CGI.unescape(value) - end + def parse(body) + @raw = body + @params = JSON.parse(@raw) + rescue JSON::ParserError end end end end end