Module: ZipMoney::WebHook

Defined in:
lib/zipMoney/webhook.rb

Constant Summary collapse

EVENT_AUTH_SUCCESS =
"authorise_succeeded"
EVENT_AUTH_FAIL =
"authorise_failed"
EVENT_AUTH_REVIEW =
"authorise_under_review"
EVENT_AUTH_DECLINED =
"authorise_declined"
EVENT_CANCEL_SUCCESS =
"cancel_succeeded"
EVENT_CANCEL_FAIL =
"cancel_failed"
EVENT_CAPTURE_SUCCESS =
"capture_succeeded"
EVENT_CAPTURE_FAIL =
"capture_failed"
EVENT_REFUND_SUCCESS =
"refund_succeeded"
EVENT_REFUND_FAIL =
"refund_failed"
EVENT_ORDER_CANCELLED =
"order_cancelled"
EVENT_CHARGE_SUCCESS =
"charge_succeeded"
EVENT_CHARGE_FAIL =
"charge_failed"
EVENT_CONFIG_UPDATE =
"configuration_updated"
TYPE_SUBSCRIPTION_CONFIRMATION =
"SubscriptionConfirmation"
TYPE_NOTIFICATION =
"Notification"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Attribute Details

#merchant_idObject

Returns the value of attribute merchant_id.



3
4
5
# File 'lib/zipMoney/webhook.rb', line 3

def merchant_id
  @merchant_id
end

#merchant_keyObject

Returns the value of attribute merchant_key.



3
4
5
# File 'lib/zipMoney/webhook.rb', line 3

def merchant_key
  @merchant_key
end

Class Method Details

.process(request, &block) ⇒ Object

Process the webhook

Parameters:

  • WebHook's (request)

    request

  • Actions (block)

    to be taken for respective notifications

Raises:



27
28
29
30
31
32
33
34
35
# File 'lib/zipMoney/webhook.rb', line 27

def self.process(request,&block)
		raise WebHookRequestError, "Payload emtpy" if request.nil? 
		request = Util.json_parse(request)
		if request["Type"] == TYPE_SUBSCRIPTION_CONFIRMATION
		subscribe(request["SubscribeURL"])
	elsif request["Type"] == TYPE_NOTIFICATION	
		process_notifications(request, &block)
	end	
end

.process_notifications(request, &block) ⇒ Object

Process the webhook notifications

Parameters:

  • WebHook's (request)

    request

  • Actions (block)

    to be taken for respective notifications

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
48
49
50
# File 'lib/zipMoney/webhook.rb', line 41

def self.process_notifications(request, &block)
	raise ArgumentError, "Invalid params provided" if request["Message"].nil?
	message = Util.json_parse(request["Message"])
	Configuration.credentials_valid(message["response"]["merchant_id"], message["response"]["merchant_key"])
	raise ArgumentError, "Response empty" if message["response"].nil?
	
	if (block.arity > 0)
  		block.call(message['type'], message["response"])
	end	
end

.subscribe(url) ⇒ Object

Subscribes for the webhook notifications by calling the subscription url

Parameters:

  • WebHook's (request)

    request

  • Actions (block)

    to be taken for respective notifications

Raises:



56
57
58
59
60
61
62
63
64
65
# File 'lib/zipMoney/webhook.rb', line 56

def self.subscribe(url)			
	raise WebHookError, "Url emtpy" if url.nil? 
	
	begin
		response = RestClient.get(url)
	rescue
		raise WebHookError, "Unable to reach the subscription url #{url}" if response.nil?
	end	
     response.code == 200 || response.code == 201
end