1
0
зеркало из https://github.com/glebtv/yookassa.git synced 2026-09-03 09:46:16 +03:00

Rewriting Client and changing interface

Этот коммит содержится в:
Ivan Shamatov
2021-11-01 23:12:07 +03:00
родитель ddcc7a3e6b
Коммит f3ff78fee1
13 изменённых файлов: 175 добавлений и 134 удалений

Просмотреть файл

@@ -1,22 +1,31 @@
# frozen_string_literal: true
require "http"
require "dry-struct"
require "forwardable"
require "yookassa/version"
require "yookassa/payment"
require "yookassa/refund"
require "yookassa/entity/payment"
require "yookassa/entity/refund"
require "yookassa/entity/error"
require "yookassa/config"
require "yookassa/http_helpers"
require "yookassa/client"
module Yookassa
def self.configure
yield(config)
end
ConfigError = Class.new(StandardError)
def self.config
@config ||= Config.new
class << self
extend Forwardable
def configure
yield(config)
end
def config
@config ||= Config.new
end
def client
raise ConfigError, "Specify `shop_id` and `api_key` settins in a `.configure` block" if @config.nil?
@client ||= Client.new(shop_id: @config.shop_id, api_key: @config.api_key)
end
def_delegators :client, :payments, :refunds
end
end

44
lib/yookassa/client.rb Обычный файл
Просмотреть файл

@@ -0,0 +1,44 @@
# frozen_string_literal: true
require "http"
require_relative "./payments"
require_relative "./refunds"
require_relative "./entity/error"
module Yookassa
class Client
API_URL = "https://api.yookassa.ru/v3/"
attr_reader :http
def initialize(shop_id:, api_key:)
@http = HTTP.basic_auth(user: shop_id, pass: api_key).headers(accept: "application/json")
end
def payments
@payments ||= Payments.new(self)
end
def refunds
@refunds ||= Refunds.new(self)
end
def get(endpoint, query: {})
api_call { http.get("#{API_URL}#{endpoint}", params: query) }
end
def post(endpoint, idempotency_key:, payload: {})
api_call { http.headers("Idempotence-Key" => idempotency_key).post("#{API_URL}#{endpoint}", json: payload) }
end
private
def api_call
response = yield if block_given?
body = JSON.parse(response.body.to_s, symbolize_names: true)
return body if response.status.success?
Entity::Error.new(**body)
end
end
end

Просмотреть файл

@@ -1,5 +1,7 @@
# frozen_string_literal: true
require_relative "./types"
module Yookassa
module Entity
class Error < Dry::Struct

Просмотреть файл

@@ -16,6 +16,8 @@ module Yookassa
# Payment ID in YooMoney.
attribute :id, Types::String
attribute? :idempotency_key, Types::String
# status [string, required]
# Payment status. Possible values: pending, waiting_for_capture, succeeded, and canceled.
# More about the life cycle of a payment https://yookassa.ru/en/developers/api#:~:text=life%20cycle%20of%20a%20payment%C2%A0

Просмотреть файл

@@ -7,6 +7,7 @@ module Yookassa
module Entity
class Refund < Dry::Struct
attribute :id, Types::String
attribute? :idempotency_key, Types::String
attribute :status, Types::String
attribute :payment_id, Types::String
attribute :created_at, Types::String

Просмотреть файл

@@ -1,29 +0,0 @@
# 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)
body = JSON.parse(response.body.to_s, symbolize_names: true)
return Entity::Error.new(**body) if response.status.client_error?
yield(body) if block_given?
end
def post(endpoint, idempotency_key:, payload: {})
response = client.headers("Idempotence-Key" => idempotency_key).post("#{API_URL}#{endpoint}", json: payload)
body = JSON.parse(response.body.to_s, symbolize_names: true)
return Entity::Error.new(**body) if response.status.client_error?
yield(body) if block_given?
end
def client
HTTP.basic_auth(user: shop_id, pass: api_key).headers(accept: "application/json")
end
end
end

Просмотреть файл

@@ -1,46 +0,0 @@
# frozen_string_literal: true
require_relative "http_helpers"
module Yookassa
class Payment
include HttpHelpers
attr_reader :shop_id, :api_key
def initialize(shop_id: Yookassa.config.shop_id, api_key: Yookassa.config.api_key)
@shop_id = shop_id
@api_key = api_key
end
def get_payment_info(payment_id:)
get("payments/#{payment_id}") do |response|
Entity::Payment.new(**response)
end
end
def create(payment:, idempotency_key: SecureRandom.hex(10))
post("payments", payload: payment, idempotency_key: idempotency_key) do |response|
Entity::Payment.new(**response)
end
end
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

35
lib/yookassa/payments.rb Обычный файл
Просмотреть файл

@@ -0,0 +1,35 @@
# frozen_string_literal: true
require_relative "./entity/payment"
module Yookassa
class Payments
def initialize(api)
@api = api
end
def find(payment_id:)
data = api.get("payments/#{payment_id}")
Entity::Payment.new(**data)
end
def create(payment:, idempotency_key: SecureRandom.hex(10))
data = api.post("payments", payload: payment, idempotency_key: idempotency_key)
Entity::Payment.new(**data.merge(idempotency_key: idempotency_key))
end
def capture(payment_id:, idempotency_key: SecureRandom.hex(10))
data = api.post("payments/#{payment_id}/capture", idempotency_key: idempotency_key)
Entity::Payment.new(**data.merge(idempotency_key: idempotency_key))
end
def cancel(payment_id:, idempotency_key: SecureRandom.hex(10))
data = api.post("payments/#{payment_id}/cancel", idempotency_key: idempotency_key)
Entity::Payment.new(**data.merge(idempotency_key: idempotency_key))
end
private
attr_reader :api
end
end

Просмотреть файл

@@ -1,28 +0,0 @@
# frozen_string_literal: true
require_relative "http_helpers"
module Yookassa
class Refund
include HttpHelpers
attr_reader :shop_id, :api_key
def initialize(shop_id: Yookassa.config.shop_id, api_key: Yookassa.config.api_key)
@shop_id = shop_id
@api_key = api_key
end
def get_refund_info(payment_id:)
get("refunds/#{payment_id}") do |response|
Entity::Refund.new(**response)
end
end
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

25
lib/yookassa/refunds.rb Обычный файл
Просмотреть файл

@@ -0,0 +1,25 @@
# frozen_string_literal: true
require_relative "./entity/refund"
module Yookassa
class Refunds
def initialize(api)
@api = api
end
def find(payment_id:)
data = api.get("refunds/#{payment_id}")
Entity::Refund.new(**data)
end
def create(payload:, idempotency_key: SecureRandom.hex(10))
data = api.post("refunds", payload: payload, idempotency_key: idempotency_key)
Entity::Refund.new(**data.merge(idempotency_key: idempotency_key))
end
private
attr_reader :api
end
end