Skip to content

Instantly share code, notes, and snippets.

@tranghaviet
Forked from briankung/00 - stripe.md
Created November 22, 2018 04:27

Revisions

  1. @briankung briankung revised this gist Aug 2, 2017. 1 changed file with 11 additions and 12 deletions.
    23 changes: 11 additions & 12 deletions 01 - decline cards and responses.md
    Original file line number Diff line number Diff line change
    @@ -4,18 +4,6 @@ From https://stripe.com/docs/testing#cards

    # CARD DETAILS BUCKET

    4100_0000_0000_0019

    {
    error: {
    message: "Your card was declined.",
    type: "card_error",
    code: "card_declined",
    decline_code: "fraudulent",
    charge: "ch_12345"
    }
    }

    4000_0000_0000_0127

    {
    @@ -115,6 +103,17 @@ From https://stripe.com/docs/testing#cards
    charge: "ch_12345"
    }
    }

    4100_0000_0000_0019

    {
    error: {
    message: "Your card was declined.",
    type: "card_error",
    code: "card_declined",
    decline_code: "fraudulent",
    charge: "ch_12345"
    }
    }
    # And basically everything else...
    ```
  2. @briankung briankung revised this gist Aug 2, 2017. 1 changed file with 42 additions and 32 deletions.
    74 changes: 42 additions & 32 deletions 01 - decline cards and responses.md
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,8 @@
    From https://stripe.com/docs/testing#cards

    ```ruby
    4000_0000_0000_0002

    {
    error: {
    message: "Your card was declined.",
    type: "card_error",
    code: "card_declined",
    decline_code: "generic_decline",
    charge: "ch_1AkHvOK3cnS75wRwvtyxdMOx"
    }
    }
    # CARD DETAILS BUCKET

    4100_0000_0000_0019

    @@ -21,7 +12,7 @@ From https://stripe.com/docs/testing#cards
    type: "card_error",
    code: "card_declined",
    decline_code: "fraudulent",
    charge: "ch_1AkHxGK3cnS75wRww0g2CMDr"
    charge: "ch_12345"
    }
    }

    @@ -33,10 +24,22 @@ From https://stripe.com/docs/testing#cards
    type: "card_error",
    param: "cvc",
    code: "incorrect_cvc",
    charge: "ch_1AkHyOK3cnS75wRwqd7Dn0nK"
    charge: "ch_12345"
    }
    }

    4000_0000_0000_0101

    {
    error: {
    message: "Your card's security code is incorrect.",
    type: "card_error",
    param: "cvc",
    code: "incorrect_cvc",
    charge: "ch_12345"
    }
    }

    4000_0000_0000_0069

    {
    @@ -45,32 +48,36 @@ From https://stripe.com/docs/testing#cards
    type: "card_error",
    param: "exp_month",
    code: "expired_card",
    charge: "ch_1AkHzHK3cnS75wRwCXa8FqGw"
    charge: "ch_12345"
    }
    }

    4000_0000_0000_0119
    4242_4242_4242_4241

    {
    error: {
    message: "An error occurred while processing your card. Try again in a little bit.",
    message: "Your card number is incorrect.",
    type: "card_error",
    code: "processing_error",
    charge: "ch_1AkHzaK3cnS75wRwRmIoqq1i"
    param: "number",
    code: "incorrect_number"
    }
    }

    4242_4242_4242_4241
    # PROCESSING ISSUES

    4000_0000_0000_0119

    {
    error: {
    message: "Your card number is incorrect.",
    message: "An error occurred while processing your card. Try again in a little bit.",
    type: "card_error",
    param: "number",
    code: "incorrect_number"
    code: "processing_error",
    charge: "ch_12345"
    }
    }

    # ADDRESS ISSUES

    4000_0000_0000_0010

    {
    @@ -95,16 +102,19 @@ From https://stripe.com/docs/testing#cards
    }
    }

    # GENERIC DECLINE

    4000_0000_0000_0002

    {
    error: {
    message: "Your card was declined.",
    type: "card_error",
    code: "card_declined",
    decline_code: "generic_decline",
    charge: "ch_12345"
    }
    }

    4000_0000_0000_0101

    {
    error: {
    message: "Your card's security code is incorrect.",
    type: "card_error",
    param: "cvc",
    code: "incorrect_cvc",
    charge: "ch_12345"
    }
    }
    # And basically everything else...
    ```
  3. @briankung briankung revised this gist Aug 2, 2017. 1 changed file with 25 additions and 0 deletions.
    25 changes: 25 additions & 0 deletions 01 - decline cards and responses.md
    Original file line number Diff line number Diff line change
    @@ -82,4 +82,29 @@ From https://stripe.com/docs/testing#cards
    charge: "ch_12345"
    }
    }

    4000_0000_0000_0036

    {
    error: {
    message: "The zip code you supplied failed validation.",
    type: "card_error",
    param: "address_zip",
    code: "incorrect_zip",
    charge: "ch_12345"
    }
    }


    4000_0000_0000_0101

    {
    error: {
    message: "Your card's security code is incorrect.",
    type: "card_error",
    param: "cvc",
    code: "incorrect_cvc",
    charge: "ch_12345"
    }
    }
    ```
  4. @briankung briankung revised this gist Aug 2, 2017. 2 changed files with 24 additions and 10 deletions.
    12 changes: 12 additions & 0 deletions 01 - decline cards and responses.md
    Original file line number Diff line number Diff line change
    @@ -70,4 +70,16 @@ From https://stripe.com/docs/testing#cards
    code: "incorrect_number"
    }
    }

    4000_0000_0000_0010

    {
    error: {
    message: "The zip code you supplied failed validation.",
    type: "card_error",
    param: "address_zip",
    code: "incorrect_zip",
    charge: "ch_12345"
    }
    }
    ```
    22 changes: 12 additions & 10 deletions 02 - test charge code.rb
    Original file line number Diff line number Diff line change
    @@ -1,15 +1,17 @@
    def create_auth_charge(number, description = nil)
    def test_charge
    Stripe::Charge.create(
    amount: 50,
    currency: "usd",
    capture: false,
    source: {
    exp_month: '12',
    exp_year: '21',
    number: number,
    object: "card",
    cvc: '123',
    },
    description: "Test charge #{Time.zone.now}#{' - ' + description if description}",
    source: @source,
    description: "Test charge",
    )
    end
    end

    @source = {
    exp_month: '12',
    exp_year: '21',
    number: '4000000000000010',
    object: "card",
    cvc: '123',
    }
  5. @briankung briankung revised this gist Aug 2, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions 02 - test charge code.rb
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    def create_auth_charge(number)
    def create_auth_charge(number, description = nil)
    Stripe::Charge.create(
    amount: 50,
    currency: "usd",
    @@ -10,6 +10,6 @@ def create_auth_charge(number)
    object: "card",
    cvc: '123',
    },
    description: "Test authorization charge",
    description: "Test charge #{Time.zone.now}#{' - ' + description if description}",
    )
    end
  6. @briankung briankung revised this gist Aug 2, 2017. 1 changed file with 15 additions and 0 deletions.
    15 changes: 15 additions & 0 deletions 02 - test charge code.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    def create_auth_charge(number)
    Stripe::Charge.create(
    amount: 50,
    currency: "usd",
    capture: false,
    source: {
    exp_month: '12',
    exp_year: '21',
    number: number,
    object: "card",
    cvc: '123',
    },
    description: "Test authorization charge",
    )
    end
  7. @briankung briankung renamed this gist Aug 2, 2017. 1 changed file with 4 additions and 0 deletions.
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,6 @@
    From https://stripe.com/docs/testing#cards

    ```ruby
    4000_0000_0000_0002

    {
    @@ -67,3 +70,4 @@
    code: "incorrect_number"
    }
    }
    ```
  8. @briankung briankung revised this gist Jul 27, 2017. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions 00 - stripe.md
    Original file line number Diff line number Diff line change
    @@ -122,6 +122,8 @@ Stripe::Subscription.create(

    ## ...with discounts

    NOTE! This should be changed to use invoice items

    A Discount is the application of a Coupon to a Subscription or a Charge.

    Create a Coupon
    @@ -164,6 +166,8 @@ Stripe::Charge.create(

    ## Pay Early / Some now, some later

    NOTE! This should be changed to use invoice items

    ```ruby
    # Accept early payment

  9. @briankung briankung revised this gist Jul 27, 2017. 2 changed files with 0 additions and 0 deletions.
    File renamed without changes.
  10. @briankung briankung renamed this gist Jul 27, 2017. 1 changed file with 0 additions and 0 deletions.
  11. @briankung briankung revised this gist Jul 27, 2017. 1 changed file with 69 additions and 0 deletions.
    69 changes: 69 additions & 0 deletions Decline cards and responses.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,69 @@
    4000_0000_0000_0002

    {
    error: {
    message: "Your card was declined.",
    type: "card_error",
    code: "card_declined",
    decline_code: "generic_decline",
    charge: "ch_1AkHvOK3cnS75wRwvtyxdMOx"
    }
    }

    4100_0000_0000_0019

    {
    error: {
    message: "Your card was declined.",
    type: "card_error",
    code: "card_declined",
    decline_code: "fraudulent",
    charge: "ch_1AkHxGK3cnS75wRww0g2CMDr"
    }
    }

    4000_0000_0000_0127

    {
    error: {
    message: "Your card's security code is incorrect.",
    type: "card_error",
    param: "cvc",
    code: "incorrect_cvc",
    charge: "ch_1AkHyOK3cnS75wRwqd7Dn0nK"
    }
    }

    4000_0000_0000_0069

    {
    error: {
    message: "Your card has expired.",
    type: "card_error",
    param: "exp_month",
    code: "expired_card",
    charge: "ch_1AkHzHK3cnS75wRwCXa8FqGw"
    }
    }

    4000_0000_0000_0119

    {
    error: {
    message: "An error occurred while processing your card. Try again in a little bit.",
    type: "card_error",
    code: "processing_error",
    charge: "ch_1AkHzaK3cnS75wRwRmIoqq1i"
    }
    }

    4242_4242_4242_4241

    {
    error: {
    message: "Your card number is incorrect.",
    type: "card_error",
    param: "number",
    code: "incorrect_number"
    }
    }
  12. @briankung briankung revised this gist Jul 7, 2017. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion stripe.md
    Original file line number Diff line number Diff line change
    @@ -177,7 +177,7 @@ Stripe::Charge.create(
    :description => "Charge for early payment to subscription"
    )

    # Apply equivalent discount
    # Create equivalent coupon

    coupon = Stripe::Coupon.create(
    :amount_off => early_payment_amount,
    @@ -186,6 +186,8 @@ coupon = Stripe::Coupon.create(
    :id => "ONETIME#{early_payment_amount}OFF"
    )

    # Apply coupon to subscription via coupon

    subscription = Stripe::Subscription.retrieve("sub_Aydt4F9jHlfI1a")

    subscription.coupon = coupon.id
  13. @briankung briankung revised this gist Jul 7, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion stripe.md
    Original file line number Diff line number Diff line change
    @@ -162,7 +162,7 @@ Stripe::Charge.create(
    #=> Stripe::CardError: (Status 402) (Request req_Az0QuH7K9P0XC6) Your card was declined.
    ```

    ## Pay Early
    ## Pay Early / Some now, some later

    ```ruby
    # Accept early payment
  14. @briankung briankung revised this gist Jul 7, 2017. 1 changed file with 30 additions and 0 deletions.
    30 changes: 30 additions & 0 deletions stripe.md
    Original file line number Diff line number Diff line change
    @@ -160,4 +160,34 @@ Stripe::Charge.create(
    )

    #=> Stripe::CardError: (Status 402) (Request req_Az0QuH7K9P0XC6) Your card was declined.
    ```

    ## Pay Early

    ```ruby
    # Accept early payment

    early_payment_amount = 2000

    Stripe::Charge.create(
    :amount => early_payment_amount,
    :currency => "usd",
    :capture => false,
    :source => "tok_visa",
    :description => "Charge for early payment to subscription"
    )

    # Apply equivalent discount

    coupon = Stripe::Coupon.create(
    :amount_off => early_payment_amount,
    :currency => 'USD',
    :duration => 'once',
    :id => "ONETIME#{early_payment_amount}OFF"
    )

    subscription = Stripe::Subscription.retrieve("sub_Aydt4F9jHlfI1a")

    subscription.coupon = coupon.id
    subscription.save
    ```
  15. @briankung briankung revised this gist Jul 7, 2017. 1 changed file with 14 additions and 0 deletions.
    14 changes: 14 additions & 0 deletions stripe.md
    Original file line number Diff line number Diff line change
    @@ -147,3 +147,17 @@ Stripe::Subscription.create(
    :coupon => "25OFF"
    )
    ```

    ## Unauthorized Charges

    ```ruby
    Stripe::Charge.create(
    :amount => 2000,
    :currency => "usd",
    :capture => false,
    :source => "tok_chargeDeclined",
    :description => "Charge for aubrey.anderson@example.com"
    )

    #=> Stripe::CardError: (Status 402) (Request req_Az0QuH7K9P0XC6) Your card was declined.
    ```
  16. @briankung briankung revised this gist Jul 7, 2017. 1 changed file with 9 additions and 10 deletions.
    19 changes: 9 additions & 10 deletions stripe.md
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@

    https://stripe.com/docs/api/ruby#create_customer

    ```
    ```ruby
    require "stripe"
    Stripe.api_key = "sk_test_hExNrRPAXSQNiQjGdp5AXu8y"

    @@ -30,7 +30,7 @@ Ruby:

    https://stripe.com/docs/api#create_source

    ```
    ```ruby
    require "stripe"
    Stripe.api_key = "sk_test_hExNrRPAXSQNiQjGdp5AXu8y"

    @@ -63,7 +63,7 @@ customer.sources.create(

    https://stripe.com/docs/api#create_charge

    ```
    ```ruby
    Stripe::Charge.create(
    :amount => 2000,
    :currency => "usd",
    @@ -74,7 +74,7 @@ Stripe::Charge.create(

    ## ...without capturing funds

    ```
    ```ruby
    Stripe::Charge.create(
    :amount => 2000,
    :currency => "usd",
    @@ -86,7 +86,7 @@ Stripe::Charge.create(

    Then capture with:

    ```
    ```ruby
    ch = Stripe::Charge.retrieve("ch_1Acfm8K3cnS75wRwzYmREQKL")
    ch.capture
    ```
    @@ -97,7 +97,7 @@ Create a plan

    https://stripe.com/docs/api#create_plan

    ```
    ```ruby
    Stripe::Plan.create(
    :amount => 5000,
    :interval => "month",
    @@ -113,8 +113,7 @@ https://stripe.com/docs/api#create_subscription

    Requires a customer and a plan.

    ```
    ```ruby
    Stripe::Subscription.create(
    :customer => "cus_AyZ0IsC62Eng98",
    :plan => "Test Plan 01"
    @@ -129,7 +128,7 @@ Create a Coupon

    https://stripe.com/docs/api#create_coupon

    ```
    ```ruby
    Stripe::Coupon.create(
    :amount_off => 2500,
    :currency => 'USD',
    @@ -141,7 +140,7 @@ Stripe::Coupon.create(

    Create a Discounted Subscription

    ```
    ```ruby
    Stripe::Subscription.create(
    :customer => "cus_AyZ0IsC62Eng98",
    :plan => "Test Plan 01",
  17. @briankung briankung created this gist Jul 7, 2017.
    150 changes: 150 additions & 0 deletions stripe.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,150 @@
    # Stripe

    - https://stripe.com/docs
    - https://stripe.com/docs/api

    ## Creating a User

    https://stripe.com/docs/api/ruby#create_customer

    ```
    require "stripe"
    Stripe.api_key = "sk_test_hExNrRPAXSQNiQjGdp5AXu8y"
    Stripe::Customer.create # Or...
    Stripe::Customer.create(
    :email => "sofia.martin@example.com",
    :source => "tok_1AcfGbK3cnS75wRw1Dp7Efwq" # Payment source if one exists
    )
    ```

    ## Adding a Payment Method


    - [Checkout](https://stripe.com/checkout) - Premade checkout form
    - [Elements](https://stripe.com/docs/elements) - composable checkout form elements
    - [SDK](https://stripe.com/docs/api/ruby) (Ruby)

    Ruby:

    https://stripe.com/docs/api#create_source

    ```
    require "stripe"
    Stripe.api_key = "sk_test_hExNrRPAXSQNiQjGdp5AXu8y"
    Stripe::Source.create(
    :type => "card",
    :amount => 1000,
    :currency => 'usd',
    :owner => {
    :email => 'jenny.rosen@example.com',
    },
    )
    # Or...
    customer = Stripe::Customer.retrieve("cus_AyZ0IsC62Eng98")
    customer.sources.create(source: "tok_1AcfGbK3cnS75wRw1Dp7Efwq")
    # Or...
    customer.sources.create(
    :object => 'card',
    :exp_month => 8,
    :exp_year => 2018,
    :number => 4111_1111_1111_1111
    )
    ```

    ## Creating a single charge

    https://stripe.com/docs/api#create_charge

    ```
    Stripe::Charge.create(
    :amount => 2000,
    :currency => "usd",
    :source => "tok_1AcfGbK3cnS75wRw1Dp7Efwq", # Payment source or customer required
    :description => "Charge for aubrey.anderson@example.com"
    )
    ```

    ## ...without capturing funds

    ```
    Stripe::Charge.create(
    :amount => 2000,
    :currency => "usd",
    :capture => false, # Defaults to true
    :source => "tok_1AcfGbK3cnS75wRw1Dp7Efwq", # Payment source or customer required
    :description => "Charge for aubrey.anderson@example.com"
    )
    ```

    Then capture with:

    ```
    ch = Stripe::Charge.retrieve("ch_1Acfm8K3cnS75wRwzYmREQKL")
    ch.capture
    ```

    ## Creating a recurring charge

    Create a plan

    https://stripe.com/docs/api#create_plan

    ```
    Stripe::Plan.create(
    :amount => 5000,
    :interval => "month",
    :name => "Silver beginner",
    :currency => "usd",
    :id => "silver-beginner"
    )
    ```

    Create a subscription:

    https://stripe.com/docs/api#create_subscription

    Requires a customer and a plan.

    ```
    Stripe::Subscription.create(
    :customer => "cus_AyZ0IsC62Eng98",
    :plan => "Test Plan 01"
    )
    ```

    ## ...with discounts

    A Discount is the application of a Coupon to a Subscription or a Charge.

    Create a Coupon

    https://stripe.com/docs/api#create_coupon

    ```
    Stripe::Coupon.create(
    :amount_off => 2500,
    :currency => 'USD',
    :duration => 'repeating',
    :duration_in_months => 3,
    :id => '25OFF'
    )
    ```

    Create a Discounted Subscription

    ```
    Stripe::Subscription.create(
    :customer => "cus_AyZ0IsC62Eng98",
    :plan => "Test Plan 01",
    :coupon => "25OFF"
    )
    ```