Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ require "securerandom"
client = SumUp::Client.new(api_key: ENV.fetch("SUMUP_API_KEY"))

checkout = client.checkouts.create(
checkout_reference: SecureRandom.uuid,
amount: 19.99,
currency: "EUR",
merchant_code: ENV.fetch("SUMUP_MERCHANT_CODE"),
description: "Order 1234",
hosted_checkout: {enabled: true}
body: {
checkout_reference: SecureRandom.uuid,
amount: 19.99,
currency: "EUR",
merchant_code: ENV.fetch("SUMUP_MERCHANT_CODE"),
description: "Order 1234",
hosted_checkout: {enabled: true}
}
)

puts checkout.id
Expand All @@ -54,7 +56,7 @@ Send the customer to `hosted_checkout_url` to pay. Retrieve the checkout on your
server to verify its status before fulfilling the order. Keep credentials on your
server. Sandbox merchants use the same API URL as live merchants.

Request bodies accept keyword arguments, a Hash, or a model from `SumUp::Types`.
Pass request bodies through `body:` as a Hash or a model from `SumUp::Types`.
Response fields are available as Ruby methods or through `object["field_name"]`;
`to_h` preserves unknown fields and explicit nulls. RBS signatures are included.

Expand All @@ -67,8 +69,10 @@ Use a paired reader and the credentials required by the
result = client.readers.create_checkout(
merchant_code: ENV.fetch("SUMUP_MERCHANT_CODE"),
reader_id: ENV.fetch("SUMUP_READER_ID"),
total_amount: {currency: "EUR", minor_unit: 2, value: 1999},
description: "Order 1234"
body: {
total_amount: {currency: "EUR", minor_unit: 2, value: 1999},
description: "Order 1234"
}
)

puts result.data.client_transaction_id
Expand Down
7 changes: 6 additions & 1 deletion codegen/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,12 @@ func TestCurrentSpec(t *testing.T) {
t.Fatal(err)
}
files := generatedFiles(t, g)
contains(t, files["sig/sumup/resources/readers.rbs"], "total_amount: Types::CreateReaderCheckoutRequestTotalAmount | Hash[Symbol | String, untyped]")
contains(t, files["sig/sumup/resources/readers.rbs"], "body: Types::CreateReaderCheckoutRequest | Hash[Symbol | String, untyped]")
refundSignature := files["sig/sumup/resources/transactions.rbs"]
contains(t, refundSignature, "?body: Types::RefundTransactionRequest | Hash[Symbol | String, untyped]")
if strings.Contains(refundSignature, "?amount:") {
t.Fatal("refund signature accepts body keywords")
}
expected := []string{}
for _, path := range asObject(spec.get("paths")) {
for _, entry := range asObject(path.value) {
Expand Down
43 changes: 7 additions & 36 deletions codegen/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ func (g *generator) renderResource(tag string, entries []operation) string {
args = append(args, "body: UNSET")
}
args = append(args, "options: nil")
if op.body != nil {
args = append(args, "**body_fields")
}
for _, withResponse := range []bool{false, true} {
name := op.name
if withResponse {
Expand Down Expand Up @@ -72,20 +69,17 @@ func (g *generator) renderResource(tag string, entries []operation) string {
if len(headers) > 0 {
fmt.Fprintf(&b, " options = merge_header_options(options, {%s})\n", strings.Join(headers, ", "))
}
if op.body != nil {
b.WriteString(" payload = request_body(body, body_fields)\n")
if op.bodyRequired {
if len(asArray(g.merged(op.body).get("required"))) == 0 {
b.WriteString(" payload = {} if payload.equal?(UNSET)\n")
} else {
b.WriteString(" raise ArgumentError, \"request body is required\" if payload.equal?(UNSET)\n")
}
if op.body != nil && op.bodyRequired {
if len(asArray(g.merged(op.body).get("required"))) == 0 {
b.WriteString(" body = {} if body.equal?(UNSET)\n")
} else {
b.WriteString(" raise ArgumentError, \"request body is required\" if body.equal?(UNSET)\n")
}
}
fmt.Fprintf(&b, " @client.request(method: %s, path: \"%s\",\n", quote(op.verb), path)
body := ""
if op.body != nil {
body = "body: payload, "
body = "body: body, "
}
fmt.Fprintf(&b, " query: {%s}, %soptions: options,\n", strings.Join(query, ", "), body)
types := []string{}
Expand Down Expand Up @@ -129,7 +123,6 @@ func (g *generator) renderResourceRBS(tag string, entries []operation) string {
typ := g.describe(asObject(p.get("schema")), constant(op.id)+constant(asString(p.get("name"))))
args = append(args, optional+parameterName(p)+": "+typ.rbs(false))
}
withoutBody := slices.Clone(args)
if op.body != nil {
optional := ""
if !op.bodyRequired {
Expand All @@ -138,36 +131,14 @@ func (g *generator) renderResourceRBS(tag string, entries []operation) string {
args = append(args, optional+"body: "+op.bodyType.rbs(true))
}
args = append(args, "?options: request_options")
variants := [][]string{args}
if op.body != nil && g.isObject(op.body) {
bodyArgs := append(withoutBody, "?options: request_options")
name := constant(op.id) + "Request"
if op.bodyType.kind == "model" {
name = op.bodyType.name
}
required := stringsOf(g.merged(op.body).get("required"))
for _, p := range g.properties(op.body) {
optional := "?"
if slices.Contains(required, p.key) {
optional = ""
}
bodyArgs = append(bodyArgs, optional+fieldName(p.key)+": "+g.describe(asObject(p.value), name+constant(p.key)).rbs(true))
}
variants = append(variants, bodyArgs)
}
for _, withResponse := range []bool{false, true} {
name := op.name
result := op.responseType.rbs(false)
if withResponse {
name += "_with_response"
result = "Response[" + result + "]"
}
fmt.Fprintf(&b, " def %s: ", name)
rendered := []string{}
for _, v := range variants {
rendered = append(rendered, "("+strings.Join(v, ", ")+") -> "+result)
}
b.WriteString(strings.Join(rendered, "\n | ") + "\n")
fmt.Fprintf(&b, " def %s: (%s) -> %s\n", name, strings.Join(args, ", "), result)
}
}
return b.String() + " end\n end\nend\n"
Expand Down
6 changes: 4 additions & 2 deletions examples/card_reader_checkout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
result = client.readers.create_checkout(
merchant_code: ENV.fetch("SUMUP_MERCHANT_CODE"),
reader_id: ENV.fetch("SUMUP_READER_ID"),
total_amount: {currency: "EUR", minor_unit: 2, value: 1999},
description: "Ruby SDK example"
body: {
total_amount: {currency: "EUR", minor_unit: 2, value: 1999},
description: "Ruby SDK example"
}
)

puts "Reader checkout started: #{result.data.client_transaction_id}"
Expand Down
14 changes: 8 additions & 6 deletions examples/checkout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

client = SumUp::Client.new(api_key: ENV.fetch("SUMUP_API_KEY"))
checkout = client.checkouts.create(
checkout_reference: SecureRandom.uuid,
amount: 19.99,
currency: "EUR",
merchant_code: ENV.fetch("SUMUP_MERCHANT_CODE"),
description: "Ruby SDK example",
hosted_checkout: {enabled: true}
body: {
checkout_reference: SecureRandom.uuid,
amount: 19.99,
currency: "EUR",
merchant_code: ENV.fetch("SUMUP_MERCHANT_CODE"),
description: "Ruby SDK example",
hosted_checkout: {enabled: true}
}
)

puts "Checkout: #{checkout.id}"
Expand Down
6 changes: 3 additions & 3 deletions lib/sumup/pagination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ def each(**arguments, &block)
each_token_page(:list, arguments) { |page| page.items.each(&block) }
end

def search_each(**arguments, &block)
def search_each(merchant_code:, body: UNSET, options: nil, &block)
arguments = {merchant_code: merchant_code, body: body, options: options}
return enum_for(__method__, **arguments) unless block
each_token_page(:search, arguments) { |page| page.items.each(&block) }
end
Expand All @@ -19,11 +20,10 @@ def search_each(**arguments, &block)
def each_token_page(method, arguments)
arguments = arguments.dup
if method == :search
payload = request_body(arguments.delete(:body) { UNSET }, arguments.except(:merchant_code, :options))
payload = arguments[:body]
payload = payload.to_h if payload.is_a?(Model)
payload = {} if payload.equal?(UNSET)
raise ArgumentError, "search body must be a Hash or Model" unless payload.is_a?(Hash)
arguments = arguments.slice(:merchant_code, :options)
arguments[:body] = payload.transform_keys(&:to_s)
end
seen = Set.new
Expand Down
8 changes: 0 additions & 8 deletions lib/sumup/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,6 @@ def path_segment(value)
URI.encode_www_form_component(value.to_s).gsub("+", "%20")
end

def request_body(body, fields)
unless body.equal?(UNSET)
raise ArgumentError, "pass body or body keywords, not both" unless fields.empty?
return body
end
fields.empty? ? UNSET : fields
end

def merge_header_options(options, parameters)
options = RequestOptions.new(**options) if options.is_a?(Hash)
options ||= RequestOptions.new
Expand Down
52 changes: 22 additions & 30 deletions lib/sumup/resources/checkouts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,17 @@ def list_available_payment_methods_with_response(merchant_code:, amount: UNSET,
#
# Complete the payment through [Hosted Checkout](https://developer.sumup.com/online-payments/checkouts/hosted-checkout/) or the [Payment Widget](https://developer.sumup.com/online-payments/checkouts/card-widget).
# @return [Types::Checkout]
def create(body: UNSET, options: nil, **body_fields)
payload = request_body(body, body_fields)
raise ArgumentError, "request body is required" if payload.equal?(UNSET)
def create(body: UNSET, options: nil)
raise ArgumentError, "request body is required" if body.equal?(UNSET)
@client.request(method: "POST", path: "/v0.1/checkouts",
query: {}, body: payload, options: options,
query: {}, body: body, options: options,
response_type: {201 => [:model, "Checkout"]}, with_response: false)
end

def create_with_response(body: UNSET, options: nil, **body_fields)
payload = request_body(body, body_fields)
raise ArgumentError, "request body is required" if payload.equal?(UNSET)
def create_with_response(body: UNSET, options: nil)
raise ArgumentError, "request body is required" if body.equal?(UNSET)
@client.request(method: "POST", path: "/v0.1/checkouts",
query: {}, body: payload, options: options,
query: {}, body: body, options: options,
response_type: {201 => [:model, "Checkout"]}, with_response: true)
end

Expand Down Expand Up @@ -74,19 +72,17 @@ def get_with_response(checkout_id:, options: nil)
#
# This request changes the checkout details; it does not charge a payment instrument. Process the checkout separately to attempt a payment.
# @return [Types::Checkout]
def update(checkout_id:, body: UNSET, options: nil, **body_fields)
payload = request_body(body, body_fields)
payload = {} if payload.equal?(UNSET)
def update(checkout_id:, body: UNSET, options: nil)
body = {} if body.equal?(UNSET)
@client.request(method: "PATCH", path: "/v0.1/checkouts/#{path_segment(checkout_id)}",
query: {}, body: payload, options: options,
query: {}, body: body, options: options,
response_type: {200 => [:model, "Checkout"]}, with_response: false)
end

def update_with_response(checkout_id:, body: UNSET, options: nil, **body_fields)
payload = request_body(body, body_fields)
payload = {} if payload.equal?(UNSET)
def update_with_response(checkout_id:, body: UNSET, options: nil)
body = {} if body.equal?(UNSET)
@client.request(method: "PATCH", path: "/v0.1/checkouts/#{path_segment(checkout_id)}",
query: {}, body: payload, options: options,
query: {}, body: body, options: options,
response_type: {200 => [:model, "Checkout"]}, with_response: true)
end

Expand All @@ -100,19 +96,17 @@ def update_with_response(checkout_id:, body: UNSET, options: nil, **body_fields)
#
# Retrieve the checkout afterwards to confirm its payment status. Acceptance of the processing request does not by itself mean the checkout is paid.
# @return [(Types::CheckoutSuccess | Types::CheckoutAccepted)]
def process(checkout_id:, body: UNSET, options: nil, **body_fields)
payload = request_body(body, body_fields)
raise ArgumentError, "request body is required" if payload.equal?(UNSET)
def process(checkout_id:, body: UNSET, options: nil)
raise ArgumentError, "request body is required" if body.equal?(UNSET)
@client.request(method: "PUT", path: "/v0.1/checkouts/#{path_segment(checkout_id)}",
query: {}, body: payload, options: options,
query: {}, body: body, options: options,
response_type: {200 => [:model, "CheckoutSuccess"], 202 => [:model, "CheckoutAccepted"]}, with_response: false)
end

def process_with_response(checkout_id:, body: UNSET, options: nil, **body_fields)
payload = request_body(body, body_fields)
raise ArgumentError, "request body is required" if payload.equal?(UNSET)
def process_with_response(checkout_id:, body: UNSET, options: nil)
raise ArgumentError, "request body is required" if body.equal?(UNSET)
@client.request(method: "PUT", path: "/v0.1/checkouts/#{path_segment(checkout_id)}",
query: {}, body: payload, options: options,
query: {}, body: body, options: options,
response_type: {200 => [:model, "CheckoutSuccess"], 202 => [:model, "CheckoutAccepted"]}, with_response: true)
end

Expand All @@ -137,17 +131,15 @@ def deactivate_with_response(checkout_id:, options: nil)
# SumUp validates the merchant session request and returns the Apple Pay
# session object that your frontend should pass to Apple's JavaScript API.
# @return [Hash[String, untyped]]
def create_apple_pay_session(checkout_id:, body: UNSET, options: nil, **body_fields)
payload = request_body(body, body_fields)
def create_apple_pay_session(checkout_id:, body: UNSET, options: nil)
@client.request(method: "PUT", path: "/v0.2/checkouts/#{path_segment(checkout_id)}/apple-pay-session",
query: {}, body: payload, options: options,
query: {}, body: body, options: options,
response_type: {200 => [:map, :any]}, with_response: false)
end

def create_apple_pay_session_with_response(checkout_id:, body: UNSET, options: nil, **body_fields)
payload = request_body(body, body_fields)
def create_apple_pay_session_with_response(checkout_id:, body: UNSET, options: nil)
@client.request(method: "PUT", path: "/v0.2/checkouts/#{path_segment(checkout_id)}/apple-pay-session",
query: {}, body: payload, options: options,
query: {}, body: body, options: options,
response_type: {200 => [:map, :any]}, with_response: true)
end
end
Expand Down
28 changes: 12 additions & 16 deletions lib/sumup/resources/customers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,17 @@ module Resources
class Customers < Resource
# Creates a customer using the `customer_id` you supply. Choose an identifier that maps to the payer in your own system and reuse it when retrieving the customer or associating checkouts and saved payment instruments with them.
# @return [Types::Customer]
def create(body: UNSET, options: nil, **body_fields)
payload = request_body(body, body_fields)
raise ArgumentError, "request body is required" if payload.equal?(UNSET)
def create(body: UNSET, options: nil)
raise ArgumentError, "request body is required" if body.equal?(UNSET)
@client.request(method: "POST", path: "/v0.1/customers",
query: {}, body: payload, options: options,
query: {}, body: body, options: options,
response_type: {201 => [:model, "Customer"]}, with_response: false)
end

def create_with_response(body: UNSET, options: nil, **body_fields)
payload = request_body(body, body_fields)
raise ArgumentError, "request body is required" if payload.equal?(UNSET)
def create_with_response(body: UNSET, options: nil)
raise ArgumentError, "request body is required" if body.equal?(UNSET)
@client.request(method: "POST", path: "/v0.1/customers",
query: {}, body: payload, options: options,
query: {}, body: body, options: options,
response_type: {201 => [:model, "Customer"]}, with_response: true)
end

Expand All @@ -41,19 +39,17 @@ def get_with_response(customer_id:, options: nil)
#
# The request only overwrites the parameters included in the request, all other parameters will remain with their initially assigned values.
# @return [Types::Customer]
def update(customer_id:, body: UNSET, options: nil, **body_fields)
payload = request_body(body, body_fields)
payload = {} if payload.equal?(UNSET)
def update(customer_id:, body: UNSET, options: nil)
body = {} if body.equal?(UNSET)
@client.request(method: "PUT", path: "/v0.1/customers/#{path_segment(customer_id)}",
query: {}, body: payload, options: options,
query: {}, body: body, options: options,
response_type: {200 => [:model, "Customer"]}, with_response: false)
end

def update_with_response(customer_id:, body: UNSET, options: nil, **body_fields)
payload = request_body(body, body_fields)
payload = {} if payload.equal?(UNSET)
def update_with_response(customer_id:, body: UNSET, options: nil)
body = {} if body.equal?(UNSET)
@client.request(method: "PUT", path: "/v0.1/customers/#{path_segment(customer_id)}",
query: {}, body: payload, options: options,
query: {}, body: body, options: options,
response_type: {200 => [:model, "Customer"]}, with_response: true)
end

Expand Down
Loading
Loading