From 70950a6d9a53fe772cf68b8c01bc62473b390074 Mon Sep 17 00:00:00 2001 From: William Rudenmalm Date: Sun, 20 Dec 2015 23:10:28 +0100 Subject: [PATCH] Add method for chaning payment source --- lib/stripe/subscriptions.ex | 15 +++++++++++++++ test/stripe/subscription_test.exs | 14 ++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/lib/stripe/subscriptions.ex b/lib/stripe/subscriptions.ex index 7cc2e8bc..7f4a3350 100644 --- a/lib/stripe/subscriptions.ex +++ b/lib/stripe/subscriptions.ex @@ -123,6 +123,21 @@ defmodule Stripe.Subscriptions do end end + @doc """ + Changes the payment source for a subscription. + + #Example + ``` + source = [object: "card", number: "4111111111111111", exp_month: 01, exp_year: 2018, cvc: 123] + Stripe.Subscriptions.change_payment_source("customer_id", "subscription_id", source) + ``` + """ + def change_payment_source(customer_id, sub_id, source) do + data = [source: source] + Stripe.make_request(:post, "#{@endpoint}/#{customer_id}/subscriptions/#{sub_id}", data) + |> Stripe.Util.handle_stripe_response + end + @max_fetch_size 100 @doc """ List all subscriptions. diff --git a/test/stripe/subscription_test.exs b/test/stripe/subscription_test.exs index 961fea4e..4185da82 100644 --- a/test/stripe/subscription_test.exs +++ b/test/stripe/subscription_test.exs @@ -76,6 +76,20 @@ defmodule Stripe.SubscriptionTest do end end + @tag disable: false + test "Change creditcards works", %{customer: c, sub2: sub2} do + source = [ + object: "card", + number: "4012888888881881", + exp_year: "20", + exp_month: "12", + ] + case Stripe.Subscriptions.change_payment_source(c.id, sub2.id, source) do + {:ok, res} -> + assert res[:status] == "active" + {:error, err} -> flunk err + end + end @tag disabled: false test "Cancel all works", %{customer: customer, sub1: _, sub2: _} do Stripe.Subscriptions.cancel_all customer.id