зеркало из
https://github.com/glebtv/yookassa.git
synced 2026-09-04 18:15:49 +03:00
Completele replace evil client with http, saving initial interface and backward compatibility
Этот коммит содержится в:
@@ -1,7 +1,7 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "evil/client"
|
||||
|
||||
require "http"
|
||||
require "dry/initializer"
|
||||
require "yookassa/version"
|
||||
require "yookassa/payment"
|
||||
require "yookassa/refund"
|
||||
@@ -12,6 +12,7 @@ require "yookassa/entity/payment"
|
||||
require "yookassa/entity/refund"
|
||||
require "yookassa/error"
|
||||
require "yookassa/config"
|
||||
require "yookassa/http_helpers"
|
||||
|
||||
module Yookassa
|
||||
def self.configure
|
||||
|
||||
27
lib/yookassa/http_helpers.rb
Обычный файл
27
lib/yookassa/http_helpers.rb
Обычный файл
@@ -0,0 +1,27 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module Yookassa
|
||||
module HttpHelpers
|
||||
API_URL = "https://api.yookassa.ru/v3/"
|
||||
|
||||
def get(endpoint, query: {})
|
||||
response = client.get("#{API_URL}#{endpoint}", params: query)
|
||||
|
||||
return Error.new(response.parse) if response.status.client_error?
|
||||
|
||||
yield(response.parse) if block_given?
|
||||
end
|
||||
|
||||
def post(endpoint, idempotency_key:, payload: {})
|
||||
response = client.headers("Idempotence-Key" => idempotency_key).post("#{API_URL}#{endpoint}", json: payload)
|
||||
|
||||
return Error.new(response.parse) if response.status.client_error?
|
||||
|
||||
yield(response.parse) if block_given?
|
||||
end
|
||||
|
||||
def client
|
||||
HTTP.basic_auth(user: shop_id, pass: api_key).headers(accept: "application/json")
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -1,65 +1,46 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative "http_helpers"
|
||||
|
||||
module Yookassa
|
||||
class Payment < Evil::Client
|
||||
option :shop_id, proc(&:to_s), default: proc { Yookassa.config.shop_id }
|
||||
option :api_key, proc(&:to_s), default: proc { Yookassa.config.api_key }
|
||||
class Payment
|
||||
include HttpHelpers
|
||||
|
||||
path { "https://api.yookassa.ru/v3/payments" }
|
||||
security { basic_auth shop_id, api_key }
|
||||
attr_reader :shop_id, :api_key
|
||||
|
||||
operation :get_payment_info do
|
||||
option :payment_id, proc(&:to_s)
|
||||
|
||||
http_method :get
|
||||
path { "/#{payment_id}" }
|
||||
|
||||
response(200) { |*res| Entity::Payment.build(*res) }
|
||||
response(400, 404) { |*res| Error.build(*res) }
|
||||
def initialize(shop_id: Yookassa.config.shop_id, api_key: Yookassa.config.api_key)
|
||||
@shop_id = shop_id
|
||||
@api_key = api_key
|
||||
end
|
||||
|
||||
operation :create do
|
||||
option :payment
|
||||
option :idempotency_key, proc(&:to_s)
|
||||
|
||||
http_method :post
|
||||
|
||||
format "json"
|
||||
headers { { "Idempotence-Key" => idempotency_key } }
|
||||
body { payment }
|
||||
|
||||
response(200) { |*res| Entity::Payment.build(*res) }
|
||||
response(400) { |*res| Error.build(*res) }
|
||||
def get_payment_info(payment_id:)
|
||||
get("payments/#{payment_id}") do |response|
|
||||
Entity::Payment.new(response)
|
||||
end
|
||||
end
|
||||
|
||||
operation :capture do
|
||||
option :payment_id, proc(&:to_s)
|
||||
option :idempotency_key, optional: true
|
||||
|
||||
http_method :post
|
||||
|
||||
path { "/#{payment_id}/capture" }
|
||||
|
||||
format "json"
|
||||
headers { { "Idempotence-Key" => idempotency_key } }
|
||||
|
||||
response(200) { |*res| Entity::Payment.build(*res) }
|
||||
response(400) { |*res| Error.build(*res) }
|
||||
def create(payment:, idempotency_key: SecureRandom.hex(10))
|
||||
post("payments", payload: payment, idempotency_key: idempotency_key) do |response|
|
||||
Entity::Payment.new(response)
|
||||
end
|
||||
end
|
||||
|
||||
operation :cancel do
|
||||
option :payment_id, proc(&:to_s)
|
||||
option :idempotency_key, optional: true
|
||||
|
||||
http_method :post
|
||||
|
||||
path { "/#{payment_id}/cancel" }
|
||||
|
||||
format "json"
|
||||
headers { { "Idempotence-Key" => idempotency_key } }
|
||||
|
||||
response(200) { |*res| Entity::Payment.build(*res) }
|
||||
response(400) { |*res| Error.build(*res) }
|
||||
def capture(payment_id:, idempotency_key: SecureRandom.hex(10))
|
||||
post("payments/#{payment_id}/capture", idempotency_key: idempotency_key) do |response|
|
||||
Entity::Payment.new(response)
|
||||
end
|
||||
end
|
||||
|
||||
def cancel(payment_id:, idempotency_key: SecureRandom.hex(10))
|
||||
post("payments/#{payment_id}/cancel", idempotency_key: idempotency_key) do |response|
|
||||
Entity::Payment.new(response)
|
||||
end
|
||||
end
|
||||
|
||||
# def self.list(shop_id: Yookassa.config.shop_id, api_key: Yookassa.config.api_key)
|
||||
# get("payments", shop_id: shop_id, api_key: api_key) do |resp|
|
||||
# resp['items'].map { Entity::Payment.new(_1) }
|
||||
# end
|
||||
# end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,35 +1,28 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative "http_helpers"
|
||||
|
||||
module Yookassa
|
||||
class Refund < Evil::Client
|
||||
option :shop_id, proc(&:to_s), default: proc { Yookassa.config.shop_id }
|
||||
option :api_key, proc(&:to_s), default: proc { Yookassa.config.api_key }
|
||||
class Refund
|
||||
include HttpHelpers
|
||||
|
||||
path { "https://api.yookassa.ru/v3/refunds" }
|
||||
security { basic_auth shop_id, api_key }
|
||||
attr_reader :shop_id, :api_key
|
||||
|
||||
operation :get_refund_info do
|
||||
option :payment_id, proc(&:to_s)
|
||||
|
||||
http_method :get
|
||||
path { "/#{payment_id}" }
|
||||
|
||||
response(200) { |*res| Entity::Refund.build(*res) }
|
||||
response(400, 404) { |*res| Error.build(*res) }
|
||||
def initialize(shop_id: Yookassa.config.shop_id, api_key: Yookassa.config.api_key)
|
||||
@shop_id = shop_id
|
||||
@api_key = api_key
|
||||
end
|
||||
|
||||
operation :create do
|
||||
option :payload
|
||||
option :idempotency_key, proc(&:to_s)
|
||||
def get_refund_info(payment_id:)
|
||||
get("refunds/#{payment_id}") do |response|
|
||||
Entity::Refund.new(response)
|
||||
end
|
||||
end
|
||||
|
||||
http_method :post
|
||||
|
||||
format "json"
|
||||
headers { { "Idempotence-Key" => idempotency_key } }
|
||||
body { payload }
|
||||
|
||||
response(200) { |*res| Entity::Refund.build(*res) }
|
||||
response(400, 404) { |*res| Error.build(*res) }
|
||||
def create(payload:, idempotency_key: SecureRandom.hex(10))
|
||||
post("refunds", payload: payload, idempotency_key: idempotency_key) do |response|
|
||||
Entity::Refund.new(response)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Ссылка в новой задаче
Block a user