Created
October 16, 2018 18:24
-
-
Save patriciojofre/03464fa01b3267a6da0cabc5c99ec4b7 to your computer and use it in GitHub Desktop.
Chargebee Webhook test
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| ChargeBee::WebhookTesting.sample_notification("PaymentSucceeded", your_subscription_id) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module ChargeBee | |
| class WebhookTesting | |
| def self.sample_notification(kind, subscription_id, new_plan = nil) | |
| case kind | |
| when "SubscriptionCanceled" | |
| subscription_canceled_hook(subscription_id) | |
| when "SubscriptionChanged" | |
| subscription_changed_hook(subscription_id, new_plan) | |
| when "PaymentSucceeded" | |
| payment_succeeded_hook(subscription_id) | |
| when "PaymentFailed" | |
| payment_failed_hook(subscription_id) | |
| when "PaymentRefunded" | |
| payment_refunded_hook(subscription_id) | |
| when "PaymentFailedDue" | |
| payment_failed_due_hook(subscription_id) | |
| else | |
| raise "Unknown kind of webhook" | |
| end | |
| end | |
| def self.subscription_canceled_hook(subscription_id) | |
| @event_id = event_id | |
| @webhook_id = webhook_id | |
| @subscription = get_subscription(subscription_id).as_json["values"] | |
| @customer = get_customer(@subscription["customer_id"]).as_json["values"] | |
| @response = { | |
| "id"=>@event_id, | |
| "occurred_at"=>Time.now.to_i, | |
| "source"=>"chargewebhookmock", | |
| "object"=>"event", | |
| "api_version"=>"v2", | |
| "content"=>{ | |
| "subscription"=>@subscription, | |
| "customer"=>@customer | |
| }, | |
| "event_type"=>"subscription_cancelled", | |
| "webhook_status"=>"succeeded", | |
| "webhooks"=>[{ | |
| "id"=>@webhook_id, | |
| "webhook_status"=>"succeeded", | |
| "object"=>"webhook" | |
| }] | |
| } | |
| @response | |
| end | |
| def self.subscription_changed_hook(subscription_id, new_plan) | |
| @event_id = event_id | |
| @webhook_id = webhook_id | |
| ChargeBee::Subscription.update(subscription_id, {:plan_id => new_plan}) | |
| @subscription = get_subscription(subscription_id).as_json["values"] | |
| @customer = get_customer(@subscription["customer_id"]).as_json["values"] | |
| @card = ChargeBee::Card.retrieve(@subscription["customer_id"]).card.as_json["values"] | |
| @response = { | |
| "id"=>@event_id, | |
| "occurred_at"=>Time.now.to_i, | |
| "source"=>"chargewebhookmock", | |
| "object"=>"event", | |
| "api_version"=>"v2", | |
| "content"=>{ | |
| "subscription"=>@subscription, | |
| "customer"=>@customer, | |
| "card"=>@card, | |
| }, | |
| "event_type"=>"subscription_changed", | |
| "webhook_status"=>"succeeded", | |
| "webhooks"=>[{ | |
| "id"=>@webhook_id, | |
| "webhook_status"=>"succeeded", | |
| "object"=>"webhook" | |
| }] | |
| } | |
| @response | |
| end | |
| def self.payment_succeeded_hook(subscription_id) | |
| @event_id = event_id | |
| @webhook_id = webhook_id | |
| @subscription = get_subscription(subscription_id).as_json["values"] | |
| @customer = get_customer(@subscription["customer_id"]).as_json["values"] | |
| @card = ChargeBee::Card.retrieve(@subscription["customer_id"]).card.as_json["values"] | |
| @invoice = ChargeBee::Invoice.create({ | |
| :customer_id => @customer["id"], | |
| :charges => [{ | |
| :amount => @subscription["plan_unit_price"], | |
| :description => "test charge" | |
| }] | |
| }).invoice.as_json["values"] | |
| @transaction_id = @invoice["linked_payments"].first["txn_id"] | |
| @transaction = ChargeBee::Transaction.retrieve(@transaction_id).transaction.as_json["values"] | |
| @response = { | |
| "id"=>@event_id, | |
| "occurred_at"=>Time.now.to_i, | |
| "source"=>"chargewebhookmock", | |
| "object"=>"event", | |
| "api_version"=>"v2", | |
| "content"=>{ | |
| "transaction"=>@transaction, | |
| "invoice"=>@invoice, | |
| "customer"=>@customer, | |
| "subscription"=>@subscription, | |
| "card"=>@card, | |
| }, | |
| "event_type"=>"payment_succeeded", | |
| "webhook_status"=>"succeeded", | |
| "webhooks"=>[{ | |
| "id"=>@webhook_id, | |
| "webhook_status"=>"succeeded", | |
| "object"=>"webhook" | |
| }] | |
| } | |
| @response | |
| end | |
| def self.payment_failed_hook(subscription_id) | |
| @event_id = event_id | |
| @webhook_id = webhook_id | |
| @subscription = get_subscription(subscription_id).as_json["values"] | |
| @customer = get_customer(@subscription["customer_id"]).as_json["values"] | |
| @card = ChargeBee::Card.retrieve(@subscription["customer_id"]).card.as_json["values"] | |
| @invoice = ChargeBee::Invoice.create({ | |
| :customer_id => @customer["id"], | |
| :charges => [{ | |
| :amount => @subscription["plan_unit_price"], | |
| :description => "test charge" | |
| }] | |
| }).invoice.as_json["values"] | |
| @transaction_id = @invoice["linked_payments"].first["txn_id"] | |
| @transaction = ChargeBee::Transaction.retrieve(@transaction_id).transaction.as_json["values"] | |
| @failed_invoice_opts = { | |
| "status"=>"payment_due", | |
| "due_date"=>Time.now.to_i | |
| } | |
| @failed_transaction_opts = { | |
| "status"=>"failure", | |
| "error_code"=>"3001", | |
| "error_text"=>"Insufficient funds" | |
| } | |
| @response = { | |
| "id"=>@event_id, | |
| "occurred_at"=>Time.now.to_i, | |
| "source"=>"chargewebhookmock", | |
| "object"=>"event", | |
| "api_version"=>"v2", | |
| "content"=>{ | |
| "transaction"=>@transaction.merge!(@failed_transaction_opts), | |
| "invoice"=>@invoice.merge!(@failed_invoice_opts), | |
| "customer"=>@customer, | |
| "subscription"=>@subscription, | |
| "card"=>@card, | |
| }, | |
| "event_type"=>"payment_failed", | |
| "webhook_status"=>"succeeded", | |
| "webhooks"=>[{ | |
| "id"=>@webhook_id, | |
| "webhook_status"=>"succeeded", | |
| "object"=>"webhook" | |
| }] | |
| } | |
| @response | |
| end | |
| def self.payment_failed_due_hook(subscription_id) | |
| @event_id = event_id | |
| @webhook_id = webhook_id | |
| @subscription = get_subscription(subscription_id).as_json["values"] | |
| @customer = get_customer(@subscription["customer_id"]).as_json["values"] | |
| @card = ChargeBee::Card.retrieve(@subscription["customer_id"]).card.as_json["values"] | |
| @plan_price = @subscription["plan_unit_price"] > 0 ? @subscription["plan_unit_price"] : 100 | |
| @invoice_opts = { | |
| :customer_id => @customer["id"], | |
| :charges => [{ | |
| :amount => @plan_price, | |
| :description => "test charge" | |
| }] | |
| } | |
| @invoice = ChargeBee::Invoice.create(@invoice_opts).invoice.as_json["values"] | |
| @transaction_id = @invoice["linked_payments"].first["txn_id"] | |
| @transaction = ChargeBee::Transaction.retrieve(@transaction_id).transaction.as_json["values"] | |
| @exhausted_opts = { | |
| "status"=>"payment_due", | |
| "due_date"=>Time.now.to_i, | |
| "dunning_status"=>"exhausted" | |
| } | |
| @failed_transaction_opts = { | |
| "status"=>"failure", | |
| "error_code"=>"3001", | |
| "error_text"=>"Insufficient funds" | |
| } | |
| @response = { | |
| "id"=>@event_id, | |
| "occurred_at"=>Time.now.to_i, | |
| "source"=>"chargewebhookmock", | |
| "object"=>"event", | |
| "api_version"=>"v2", | |
| "content"=>{ | |
| "transaction"=>@transaction.merge!(@failed_transaction_opts), | |
| "invoice"=>@invoice.merge!(@exhausted_opts), | |
| "customer"=>@customer, | |
| "subscription"=>@subscription, | |
| "card"=>@card, | |
| }, | |
| "event_type"=>"payment_failed", | |
| "webhook_status"=>"succeeded", | |
| "webhooks"=>[{ | |
| "id"=>@webhook_id, | |
| "webhook_status"=>"succeeded", | |
| "object"=>"webhook" | |
| }] | |
| } | |
| @response | |
| end | |
| def self.payment_refunded_hook(subscription_id) | |
| @event_id = event_id | |
| @webhook_id = webhook_id | |
| @subscription = get_subscription(subscription_id).as_json["values"] | |
| @customer = get_customer(@subscription["customer_id"]).as_json["values"] | |
| @card = ChargeBee::Card.retrieve(@subscription["customer_id"]).card.as_json["values"] | |
| @plan_price = @subscription["plan_unit_price"] > 0 ? @subscription["plan_unit_price"] : 100 | |
| @invoice_opts = { | |
| :customer_id => @customer["id"], | |
| :charges => [{ | |
| :amount => @plan_price, | |
| :description => "Test charge" | |
| }] | |
| } | |
| @invoice = ChargeBee::Invoice.create(@invoice_opts) | |
| @refund_opts = { | |
| :comment => "Refunding a test charge.", | |
| :transaction => { | |
| :amount => 100, | |
| :payment_method => "other", | |
| :date => Time.now.to_i | |
| } | |
| } | |
| @credit_note = ChargeBee::Invoice.record_refund(@invoice.invoice.id, @refund_opts) | |
| @response = { | |
| "id"=>@event_id, | |
| "occurred_at"=>Time.now.to_i, | |
| "source"=>"chargewebhookmock", | |
| "object"=>"event", | |
| "api_version"=>"v2", | |
| "content"=>{ | |
| "transaction"=>@credit_note.transaction.as_json["values"], | |
| "invoice"=>@invoice.invoice.as_json["values"], | |
| "credit_note"=>@credit_note.credit_note.as_json["values"], | |
| "customer"=>@customer, | |
| "subscription"=>@subscription, | |
| "card"=>@card, | |
| }, | |
| "event_type"=>"payment_refunded", | |
| "webhook_status"=>"succeeded", | |
| "webhooks"=>[{ | |
| "id"=>@webhook_id, | |
| "webhook_status"=>"succeeded", | |
| "object"=>"webhook" | |
| }] | |
| } | |
| @response | |
| end | |
| def self.get_subscription(id) | |
| @subscription = ChargeBee::Subscription.retrieve(id).subscription | |
| end | |
| def self.get_customer(id) | |
| ChargeBee::Customer.retrieve(id).customer | |
| end | |
| def self.event_id | |
| "ev_#{SecureRandom.hex(8)}" | |
| end | |
| def self.webhook_id | |
| "whv2_#{SecureRandom.hex(9)}" | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment