From 288d5f278231cda2606041443aec967df65b0226 Mon Sep 17 00:00:00 2001 From: Wendy Smoak Date: Mon, 9 May 2016 08:40:52 -0400 Subject: [PATCH] Add resource for External Payments See: https://docs.chargify.com/api-payments --- examples/payments.rb | 24 +++++++++++++++++++ lib/chargify_api_ares.rb | 1 + lib/chargify_api_ares/resources/payment.rb | 7 ++++++ .../resources/subscription.rb | 8 +++++++ 4 files changed, 40 insertions(+) create mode 100644 examples/payments.rb create mode 100644 lib/chargify_api_ares/resources/payment.rb diff --git a/examples/payments.rb b/examples/payments.rb new file mode 100644 index 0000000..933e403 --- /dev/null +++ b/examples/payments.rb @@ -0,0 +1,24 @@ +$: << File.expand_path(File.dirname(__FILE__) + '/../lib') + +require "chargify_api_ares" + +Chargify.configure do |c| + c.subdomain = ENV['CHARGIFY_SUBDOMAIN'] + c.api_key = ENV['CHARGIFY_API_KEY'] +end + +# Find a Chargify Subscription +sub = Chargify::Subscription.find_by_customer_reference 'abc-123-def-456' + +# Record an external payment on the Chargify Subscription +pmt = sub.payment( + amount_in_cents: 2500, + memo: "Pre-payment for..." +) + +# Or, create the external payment directly: +pmt = Chargify::Payment.create( + subscription_id: sub.id, + amount_in_cents: 2500, + memo: "Pre-payment for..." +) diff --git a/lib/chargify_api_ares.rb b/lib/chargify_api_ares.rb index 87592fc..a0d33d1 100644 --- a/lib/chargify_api_ares.rb +++ b/lib/chargify_api_ares.rb @@ -12,6 +12,7 @@ require 'chargify_api_ares/resources/customer' require 'chargify_api_ares/resources/event' require 'chargify_api_ares/resources/migration' +require 'chargify_api_ares/resources/payment' require 'chargify_api_ares/resources/payment_profile' require 'chargify_api_ares/resources/product' require 'chargify_api_ares/resources/product_family' diff --git a/lib/chargify_api_ares/resources/payment.rb b/lib/chargify_api_ares/resources/payment.rb new file mode 100644 index 0000000..30b7f3c --- /dev/null +++ b/lib/chargify_api_ares/resources/payment.rb @@ -0,0 +1,7 @@ +module Chargify + class Payment < Base + include ResponseHelper + + self.prefix = '/subscriptions/:subscription_id/' + end +end diff --git a/lib/chargify_api_ares/resources/subscription.rb b/lib/chargify_api_ares/resources/subscription.rb index 538308a..d2b6d4a 100644 --- a/lib/chargify_api_ares/resources/subscription.rb +++ b/lib/chargify_api_ares/resources/subscription.rb @@ -74,6 +74,14 @@ def charge(attrs = {}) Chargify::Charge.create(attrs.merge(:subscription_id => self.id)) end + # Record an external payment on an existing subscription. + # This DOES NOT charge the customer's credit card. + # For more information, please see the API docs available + # at: https://docs.chargify.com/api-payments + def payment(attrs = {}) + Chargify::Payment.create(attrs.merge(:subscription_id => self.id)) + end + def credit(attrs = {}) attrs, options = extract_uniqueness_token(attrs) process_capturing_errors do