• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

stripe / stripe-ruby / #5867

18 Apr 2024 09:24PM UTC coverage: 92.724% (-4.8%) from 97.485%
#5867

push

github

ramya-stripe
Bump version to 11.2.0

10067 of 10857 relevant lines covered (92.72%)

258.85 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.59
/lib/stripe/resources/payment_method.rb
1
# File generated from our OpenAPI spec
2
# frozen_string_literal: true
3

4
module Stripe
1✔
5
  # PaymentMethod objects represent your customer's payment instruments.
6
  # You can use them with [PaymentIntents](https://stripe.com/docs/payments/payment-intents) to collect payments or save them to
7
  # Customer objects to store instrument details for future payments.
8
  #
9
  # Related guides: [Payment Methods](https://stripe.com/docs/payments/payment-methods) and [More Payment Scenarios](https://stripe.com/docs/payments/more-payment-scenarios).
10
  class PaymentMethod < APIResource
1✔
11
    extend Stripe::APIOperations::Create
1✔
12
    extend Stripe::APIOperations::List
1✔
13
    include Stripe::APIOperations::Save
1✔
14

15
    OBJECT_NAME = "payment_method"
1✔
16
    def self.object_name
1✔
17
      "payment_method"
29✔
18
    end
19

20
    # Attaches a PaymentMethod object to a Customer.
21
    #
22
    # To attach a new PaymentMethod to a customer for future payments, we recommend you use a [SetupIntent](https://stripe.com/docs/api/setup_intents)
23
    # or a PaymentIntent with [setup_future_usage](https://stripe.com/docs/api/payment_intents/create#create_payment_intent-setup_future_usage).
24
    # These approaches will perform any necessary steps to set up the PaymentMethod for future payments. Using the /v1/payment_methods/:id/attach
25
    # endpoint without first using a SetupIntent or PaymentIntent with setup_future_usage does not optimize the PaymentMethod for
26
    # future use, which makes later declines and payment friction more likely.
27
    # See [Optimizing cards for future payments](https://stripe.com/docs/payments/payment-intents#future-usage) for more information about setting up
28
    # future payments.
29
    #
30
    # To use this PaymentMethod as the default for invoice or subscription payments,
31
    # set [invoice_settings.default_payment_method](https://stripe.com/docs/api/customers/update#update_customer-invoice_settings-default_payment_method),
32
    # on the Customer to the PaymentMethod's ID.
33
    def attach(params = {}, opts = {})
1✔
34
      request_stripe_object(
1✔
35
        method: :post,
36
        path: format("/v1/payment_methods/%<payment_method>s/attach", { payment_method: CGI.escape(self["id"]) }),
×
37
        params: params,
38
        opts: opts
39
      )
40
    end
41

42
    # Attaches a PaymentMethod object to a Customer.
43
    #
44
    # To attach a new PaymentMethod to a customer for future payments, we recommend you use a [SetupIntent](https://stripe.com/docs/api/setup_intents)
45
    # or a PaymentIntent with [setup_future_usage](https://stripe.com/docs/api/payment_intents/create#create_payment_intent-setup_future_usage).
46
    # These approaches will perform any necessary steps to set up the PaymentMethod for future payments. Using the /v1/payment_methods/:id/attach
47
    # endpoint without first using a SetupIntent or PaymentIntent with setup_future_usage does not optimize the PaymentMethod for
48
    # future use, which makes later declines and payment friction more likely.
49
    # See [Optimizing cards for future payments](https://stripe.com/docs/payments/payment-intents#future-usage) for more information about setting up
50
    # future payments.
51
    #
52
    # To use this PaymentMethod as the default for invoice or subscription payments,
53
    # set [invoice_settings.default_payment_method](https://stripe.com/docs/api/customers/update#update_customer-invoice_settings-default_payment_method),
54
    # on the Customer to the PaymentMethod's ID.
55
    def self.attach(payment_method, params = {}, opts = {})
1✔
56
      request_stripe_object(
2✔
57
        method: :post,
58
        path: format("/v1/payment_methods/%<payment_method>s/attach", { payment_method: CGI.escape(payment_method) }),
1✔
59
        params: params,
60
        opts: opts
61
      )
62
    end
63

64
    # Creates a PaymentMethod object. Read the [Stripe.js reference](https://stripe.com/docs/stripe-js/reference#stripe-create-payment-method) to learn how to create PaymentMethods via Stripe.js.
65
    #
66
    # Instead of creating a PaymentMethod directly, we recommend using the [PaymentIntents API to accept a payment immediately or the <a href="/docs/payments/save-and-reuse">SetupIntent](https://stripe.com/docs/payments/accept-a-payment) API to collect payment method details ahead of a future payment.
67
    def self.create(params = {}, opts = {})
1✔
68
      request_stripe_object(method: :post, path: "/v1/payment_methods", params: params, opts: opts)
2✔
69
    end
70

71
    # Detaches a PaymentMethod object from a Customer. After a PaymentMethod is detached, it can no longer be used for a payment or re-attached to a Customer.
72
    def detach(params = {}, opts = {})
1✔
73
      request_stripe_object(
1✔
74
        method: :post,
75
        path: format("/v1/payment_methods/%<payment_method>s/detach", { payment_method: CGI.escape(self["id"]) }),
×
76
        params: params,
77
        opts: opts
78
      )
79
    end
80

81
    # Detaches a PaymentMethod object from a Customer. After a PaymentMethod is detached, it can no longer be used for a payment or re-attached to a Customer.
82
    def self.detach(payment_method, params = {}, opts = {})
1✔
83
      request_stripe_object(
2✔
84
        method: :post,
85
        path: format("/v1/payment_methods/%<payment_method>s/detach", { payment_method: CGI.escape(payment_method) }),
1✔
86
        params: params,
87
        opts: opts
88
      )
89
    end
90

91
    # Returns a list of PaymentMethods for Treasury flows. If you want to list the PaymentMethods attached to a Customer for payments, you should use the [List a Customer's PaymentMethods](https://stripe.com/docs/api/payment_methods/customer_list) API instead.
92
    def self.list(filters = {}, opts = {})
1✔
93
      request_stripe_object(method: :get, path: "/v1/payment_methods", params: filters, opts: opts)
2✔
94
    end
95

96
    # Updates a PaymentMethod object. A PaymentMethod must be attached a customer to be updated.
97
    def self.update(id, params = {}, opts = {})
1✔
98
      request_stripe_object(
2✔
99
        method: :post,
100
        path: format("/v1/payment_methods/%<id>s", { id: CGI.escape(id) }),
1✔
101
        params: params,
102
        opts: opts
103
      )
104
    end
105
  end
106
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc