Skip to content

Instantly share code, notes, and snippets.

@confact
Created April 9, 2020 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save confact/8555cc8b626b583987e894e679af70d6 to your computer and use it in GitHub Desktop.
Save confact/8555cc8b626b583987e894e679af70d6 to your computer and use it in GitHub Desktop.
invalid memory access
# https://stripe.com/docs/api/customers/object
struct Stripe::Customer
include JSON::Serializable
struct TaxInfo
include JSON::Serializable
enum Type
Vat
end
getter tax_id : String
getter type : Type
end
struct TaxInfoVerification
include JSON::Serializable
enum Status
Unverified
Pending
Verified
end
@[JSON::Field(converter: Enum::StringConverter(Stripe::Customer::TaxInfoVerification::Status))]
getter status : Status
getter verified_name : String?
end
getter id : String
getter account_balance : Int32?
getter deleted : Bool? = false
@[JSON::Field(converter: Time::EpochConverter)]
getter created : Time?
getter currency : String?
getter default_source : String?
getter delinquent : Bool?
getter description : String?
getter email : String?
getter invoice_prefix : String?
getter livemode : Bool?
getter metadata : Hash(String, String)?
getter shipping : Shipping?
getter sources : List(PaymentMethods::Card | PaymentMethods::BankAccount)?
# getter subscriptions : List?
getter tax_info : TaxInfo?
getter tax_info_verification : TaxInfoVerification?
end
require "../../spec_helper"
describe Stripe::Customer do
it "create customer" do
WebMock.stub(:post, "https://api.stripe.com/v1/customers")
.to_return(status: 200, body: File.read("spec/support/create_customer.json"), headers: {"Content-Type" => "application/json"})
customer = Stripe::Customer.create(email: "test@ex.com", description: "test")
customer.id.should eq("abcdefghijklmnop")
customer.sources.not_nil!.data.first.id.should eq("card_1234567890ABCDEFghijklmn")
end
it "retrieve customer" do
WebMock.stub(:get, "https://api.stripe.com/v1/customers/asddad")
.to_return(status: 200, body: File.read("spec/support/retrieve_customer.json"), headers: {"Content-Type" => "application/json"})
customer = Stripe::Customer.retrieve("asddad")
customer.id.should eq("cus_H0TJWPA6bGxoTk")
end
it "list customer" do
WebMock.stub(:get, "https://api.stripe.com/v1/customers")
.to_return(status: 200, body: File.read("spec/support/list_customers.json"), headers: {"Content-Type" => "application/json"})
customers = Stripe::Customer.list
customers.data.first.id.should eq("cus_H4637xPCSXL0I7")
end
it "update customer" do
WebMock.stub(:post, "https://api.stripe.com/v1/customers/assaas")
.to_return(status: 200, body: File.read("spec/support/update_customer.json"), headers: {"Content-Type" => "application/json"})
customer = Stripe::Customer.update(customer: "assaas", email: "test@ex.com")
customer.id.should eq("cus_H0TJWPA6bGxoTk")
end
it "delete customer" do
WebMock.stub(:delete, "https://api.stripe.com/v1/customers/assaas")
.to_return(status: 200, body: File.read("spec/support/delete_customer.json"), headers: {"Content-Type" => "application/json"})
customer = Stripe::Customer.delete("assaas")
customer.deleted.should eq(true)
end
end
struct Stripe::Customer
def self.list(
email : String? = nil,
created : Hash(String, Int32)? = nil,
limit : Int32? = nil,
starting_after : String? = nil,
ending_before : String? = nil
) : List(Customer)
io = IO::Memory.new
builder = ParamsBuilder.new(io)
{% for x in %w(email created limit starting_after ending_before) %}
builder.add({{x}}, {{x.id}}) unless {{x.id}}.nil?
{% end %}
response = Stripe.client.get("/v1/customers", form: io.to_s)
if response.status_code == 200
return List(Customer).from_json(response.body)
else
raise Error.from_json(response.body, "error")
end
end
end
{
"object": "list",
"url": "/v1/customers",
"has_more": false,
"data": [
{
"id": "cus_H4637xPCSXL0I7",
"object": "customer",
"address": null,
"balance": 0,
"created": 1586429006,
"currency": "sek",
"default_source": null,
"delinquent": false,
"description": null,
"discount": null,
"email": null,
"invoice_prefix": "9591E49",
"invoice_settings": {
"custom_fields": null,
"default_payment_method": null,
"footer": null
},
"livemode": false,
"metadata": {},
"name": null,
"next_invoice_sequence": 1,
"phone": null,
"preferred_locales": [],
"shipping": null,
"sources": {
"object": "list",
"data": [],
"has_more": false,
"url": "/v1/customers/cus_H4637xPCSXL0I7/sources"
},
"subscriptions": {
"object": "list",
"data": [],
"has_more": false,
"url": "/v1/customers/cus_H4637xPCSXL0I7/subscriptions"
},
"tax_exempt": "none",
"tax_ids": {
"object": "list",
"data": [],
"has_more": false,
"url": "/v1/customers/cus_H4637xPCSXL0I7/tax_ids"
}
}
]
}
struct Stripe::Subscription
def self.list(
customer : String? = nil,
plan : String? = nil,
status : String? | Stripe::Subscription::Status? = nil,
limit : Int32? = nil,
starting_after : String? = nil,
ending_before : String? = nil
) : List(Stripe::Subscription)
io = IO::Memory.new
builder = ParamsBuilder.new(io)
{% for x in %w(customer plan status limit starting_after ending_before) %}
builder.add({{x}}, {{x.id}}) unless {{x.id}}.nil?
{% end %}
response = Stripe.client.get("/v1/subscriptions", form: io.to_s)
if response.status_code == 200
return List(Stripe::Subscription).from_json(response.body)
else
raise Error.from_json(response.body, "error")
end
end
end
{
"object": "list",
"url": "/v1/subscriptions",
"has_more": false,
"data": [
{
"id": "su_1GVxr04XsdaddaBSSq4mVJi1",
"object": "subscription",
"application_fee_percent": null,
"billing_cycle_anchor": 1586429006,
"billing_thresholds": null,
"cancel_at": null,
"cancel_at_period_end": false,
"canceled_at": null,
"collection_method": "charge_automatically",
"created": 1586429006,
"current_period_end": 1589021006,
"current_period_start": 1586429006,
"customer": "cus_4QHS5k9YC2TcST",
"days_until_due": null,
"default_payment_method": null,
"default_source": null,
"default_tax_rates": [],
"discount": null,
"ended_at": null,
"items": {
"object": "list",
"data": [
{
"id": "si_H463uUMNSfAloa",
"object": "subscription_item",
"billing_thresholds": null,
"created": 1586429006,
"metadata": {},
"plan": {
"id": "gold",
"object": "plan",
"active": true,
"aggregate_usage": null,
"amount": 2000,
"amount_decimal": "2000",
"billing_scheme": "per_unit",
"created": 1405649712,
"currency": "sek",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {},
"nickname": null,
"product": "prod_BV3Pw9g1e51LLV",
"tiers": null,
"tiers_mode": null,
"transform_usage": null,
"trial_period_days": null,
"usage_type": "licensed"
},
"quantity": 1,
"subscription": "sub_H463oymDiBzocW",
"tax_rates": []
}
],
"has_more": false,
"url": "/v1/subscription_items?subscription=sub_H463oymDiBzocW"
},
"latest_invoice": null,
"livemode": false,
"metadata": {},
"next_pending_invoice_item_invoice": null,
"pause_collection": null,
"pending_invoice_item_interval": null,
"pending_setup_intent": null,
"pending_update": null,
"plan": {
"id": "gold",
"object": "plan",
"active": true,
"aggregate_usage": null,
"amount": 2000,
"amount_decimal": "2000",
"billing_scheme": "per_unit",
"created": 1405649712,
"currency": "sek",
"interval": "month",
"interval_count": 1,
"livemode": false,
"metadata": {},
"nickname": null,
"product": "prod_BV3Pw9g1e51LLV",
"tiers": null,
"tiers_mode": null,
"transform_usage": null,
"trial_period_days": null,
"usage_type": "licensed"
},
"quantity": 1,
"schedule": null,
"start_date": 1586429006,
"status": "active",
"tax_percent": null,
"trial_end": null,
"trial_start": null
}
]
}
# https://stripe.com/docs/api/subscriptions/object
struct Stripe::Subscription
include JSON::Serializable
enum Status
Incomplete
IncompleteExpired
Trialing
Active
PastDue
Canceled
Unpaid
end
struct Item
include JSON::Serializable
getter id : String
getter billing_thresholds : Hash(String, Int32)?
getter plan : Stripe::Plan?
@[JSON::Field(converter: Time::EpochConverter)]
getter created : Time?
getter quantity : Int32
getter tax_rates : Array(Stripe::TaxRate)?
end
getter id : String
getter customer : String? | Stripe::Customer?
getter application_fee_percent : Float32?
getter billing : String?
@[JSON::Field(converter: Time::EpochConverter)]
getter billing_cycle_anchor : Time?
getter billing_thresholds : Hash(String, Int32 | Bool)?
@[JSON::Field(converter: Time::EpochConverter)]
getter cancel_at : Time?
getter cancel_at_period_end : Bool
@[JSON::Field(converter: Time::EpochConverter)]
getter canceled_at : Time?
getter collection_method : String?
@[JSON::Field(converter: Time::EpochConverter)]
getter created : Time?
@[JSON::Field(converter: Time::EpochConverter)]
getter current_period_end : Time
@[JSON::Field(converter: Time::EpochConverter)]
getter current_period_start : Time
getter days_until_due : Int32?
getter default_payment_method : String? | Stripe::PaymentMethod?
getter default_source : String?
getter default_tax_rates : Array(Stripe::TaxRate)?
getter livemode : Bool?
getter latest_invoice : String? | Stripe::Invoice?
getter pending_setup_intent : String? | Stripe::SetupIntent?
getter quantity : Int32
getter schedule : String?
getter plan : Stripe::Plan?
@[JSON::Field(converter: Time::EpochConverter)]
getter start : Time?
@[JSON::Field(converter: Time::EpochConverter)]
getter start_date : Time?
@[JSON::Field(converter: Enum::StringConverter(Stripe::Subscription::Status))]
getter status : Status
getter items : List(Item)?
end
require "../../spec_helper"
describe Stripe::Subscription do
it "create subscription" do
WebMock.stub(:post, "https://api.stripe.com/v1/subscriptions")
.to_return(status: 200, body: File.read("spec/support/create_subscription.json"), headers: {"Content-Type" => "application/json"})
subscription = Stripe::Subscription.create(customer: "sdsadaada", plan: "monthly")
subscription.id.should eq("sub_H0TJvm2aKdakJ7")
end
it "retrieve subscription" do
WebMock.stub(:get, "https://api.stripe.com/v1/subscriptions/asddad")
.to_return(status: 200, body: File.read("spec/support/retrieve_subscription.json"), headers: {"Content-Type" => "application/json"})
subscription = Stripe::Subscription.retrieve("asddad")
subscription.id.should eq("sub_H0TJvm2aKdakJ7")
end
it "listing subscriptions" do
WebMock.stub(:get, "https://api.stripe.com/v1/subscriptions")
.to_return(status: 200, body: File.read("spec/support/list_subscriptions.json"), headers: {"Content-Type" => "application/json"})
subscriptions = Stripe::Subscription.list
subscriptions.data.first.id.should eq("cus_H4637xPCSXL0I7")
end
it "update subscription" do
WebMock.stub(:post, "https://api.stripe.com/v1/subscriptions/assaas")
.to_return(status: 200, body: File.read("spec/support/update_subscription.json"), headers: {"Content-Type" => "application/json"})
subscription = Stripe::Subscription.update(subscription: "assaas", cancel_at_period_end: true)
subscription.id.should eq("sub_H0TJvm2aKdakJ7")
end
it "delete subscription" do
WebMock.stub(:delete, "https://api.stripe.com/v1/subscriptions/assaas")
.to_return(status: 200, body: File.read("spec/support/delete_subscription.json"), headers: {"Content-Type" => "application/json"})
subscription = Stripe::Subscription.delete("assaas")
subscription.status.should eq(Stripe::Subscription::Status::Canceled)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment