зеркало из
https://github.com/glebtv/yookassa.git
synced 2026-09-03 17:55:51 +03:00
Preparing clients for partner api (#25)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
6072b2ae0f
Коммит
7f79ffa4b4
@@ -4,11 +4,11 @@ require "dry-struct"
|
||||
require "forwardable"
|
||||
require "yookassa/version"
|
||||
require "yookassa/config"
|
||||
require "yookassa/client"
|
||||
require "yookassa/payments"
|
||||
require "yookassa/refunds"
|
||||
require "yookassa/receipts"
|
||||
|
||||
module Yookassa
|
||||
ConfigError = Class.new(StandardError)
|
||||
|
||||
class << self
|
||||
extend Forwardable
|
||||
|
||||
@@ -20,18 +20,16 @@ module Yookassa
|
||||
@config ||= Config.new
|
||||
end
|
||||
|
||||
def client
|
||||
raise ConfigError, "Specify `shop_id` and `api_key` settings in a `.configure` block" if @config.nil?
|
||||
|
||||
@client ||= Client.new(shop_id: @config.shop_id, api_key: @config.api_key)
|
||||
def payments
|
||||
@payments ||= Payments.new
|
||||
end
|
||||
|
||||
def partner_api
|
||||
@partner_api ||= PartnerAPI.new
|
||||
def refunds
|
||||
@refunds ||= Refunds.new
|
||||
end
|
||||
|
||||
def_delegators :client, :payments, :refunds, :receipts
|
||||
|
||||
def_delegators :partner_api, :stores, :webhooks
|
||||
def receipts
|
||||
@receipts ||= Receipts.new
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,9 +1,6 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require "http"
|
||||
require_relative "./payments"
|
||||
require_relative "./refunds"
|
||||
require_relative "./receipts"
|
||||
require_relative "./entity/error"
|
||||
|
||||
module Yookassa
|
||||
@@ -12,21 +9,21 @@ module Yookassa
|
||||
|
||||
attr_reader :http
|
||||
|
||||
def initialize(shop_id:, api_key:)
|
||||
@http = HTTP.basic_auth(user: shop_id, pass: api_key).headers(accept: "application/json")
|
||||
def initialize(shop_id: Yookassa.config.shop_id, api_key: Yookassa.config.api_key, oauth_token: nil)
|
||||
@http = HTTP.headers(accept: "application/json")
|
||||
|
||||
if shop_id && api_key
|
||||
@http.basic_auth(user: shop_id, pass: api_key)
|
||||
elsif oauth_token
|
||||
@http.headers("Authorization" => "Bearer #{oauth_token}")
|
||||
else
|
||||
message = "Specify `shop_id` and `api_key` settings in a `.configure` block " \
|
||||
"or pass `oauth_token` to a client"
|
||||
raise ConfigError, message
|
||||
end
|
||||
end
|
||||
|
||||
def payments
|
||||
@payments ||= Payments.new(self)
|
||||
end
|
||||
|
||||
def refunds
|
||||
@refunds ||= Refunds.new(self)
|
||||
end
|
||||
|
||||
def receipts
|
||||
@receipts ||= Receipts.new(self)
|
||||
end
|
||||
private
|
||||
|
||||
def get(endpoint, query: {})
|
||||
api_call { http.get("#{API_URL}#{endpoint}", params: query) }
|
||||
@@ -36,8 +33,6 @@ module Yookassa
|
||||
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)
|
||||
|
||||
@@ -1,41 +1,34 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative "./client"
|
||||
require_relative "./entity/payment"
|
||||
require_relative "./entity/collection"
|
||||
|
||||
module Yookassa
|
||||
class Payments
|
||||
def initialize(api)
|
||||
@api = api
|
||||
end
|
||||
|
||||
class Payments < Client
|
||||
def find(payment_id:)
|
||||
data = api.get("payments/#{payment_id}")
|
||||
data = 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)
|
||||
data = 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)
|
||||
data = 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)
|
||||
data = post("payments/#{payment_id}/cancel", idempotency_key: idempotency_key)
|
||||
Entity::Payment.new(**data.merge(idempotency_key: idempotency_key))
|
||||
end
|
||||
|
||||
def list(filters: {})
|
||||
data = api.get("payments", query: filters)
|
||||
data = get("payments", query: filters)
|
||||
Entity::PaymentCollection.new(**data)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :api
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,31 +1,24 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative "./client"
|
||||
require_relative "./entity/receipt"
|
||||
require_relative "./entity/collection"
|
||||
|
||||
module Yookassa
|
||||
class Receipts
|
||||
def initialize(api)
|
||||
@api = api
|
||||
end
|
||||
|
||||
class Receipts < Client
|
||||
def find(receipt_id:)
|
||||
data = api.get("receipts/#{receipt_id}")
|
||||
data = get("receipts/#{receipt_id}")
|
||||
Entity::Receipt.new(**data)
|
||||
end
|
||||
|
||||
def create(payload:, idempotency_key: SecureRandom.hex(10))
|
||||
data = api.post("receipts", payload: payload, idempotency_key: idempotency_key)
|
||||
data = post("receipts", payload: payload, idempotency_key: idempotency_key)
|
||||
Entity::Receipt.new(**data.merge(idempotency_key: idempotency_key))
|
||||
end
|
||||
|
||||
def list(filters: {})
|
||||
data = api.get("receipts", query: filters)
|
||||
data = get("receipts", query: filters)
|
||||
Entity::ReceiptCollection.new(**data)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :api
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,31 +1,24 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative "./client"
|
||||
require_relative "./entity/refund"
|
||||
require_relative "./entity/collection"
|
||||
|
||||
module Yookassa
|
||||
class Refunds
|
||||
def initialize(api)
|
||||
@api = api
|
||||
end
|
||||
|
||||
class Refunds < Client
|
||||
def find(payment_id:)
|
||||
data = api.get("refunds/#{payment_id}")
|
||||
data = 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)
|
||||
data = post("refunds", payload: payload, idempotency_key: idempotency_key)
|
||||
Entity::Refund.new(**data.merge(idempotency_key: idempotency_key))
|
||||
end
|
||||
|
||||
def list(filters: {})
|
||||
data = api.get("refunds", query: filters)
|
||||
data = get("refunds", query: filters)
|
||||
Entity::RefundCollection.new(**data)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :api
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,20 +1,13 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative "./client"
|
||||
require_relative "./entity/store_info"
|
||||
|
||||
module Yookassa
|
||||
class Stores
|
||||
def initialize(partner_api)
|
||||
@partner_api = partner_api
|
||||
end
|
||||
|
||||
def me
|
||||
data = partner_api.get("me")
|
||||
class Stores < Client
|
||||
def info
|
||||
data = get("me")
|
||||
Entity::StoreInfo.new(**data)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :partner_api
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,31 +1,24 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
require_relative "./client"
|
||||
require_relative "./entity/webhook"
|
||||
require_relative "./entity/collection"
|
||||
|
||||
module Yookassa
|
||||
class Webhooks
|
||||
def initialize(partner_api)
|
||||
@partner_api = partner_api
|
||||
end
|
||||
|
||||
class Webhooks < Client
|
||||
def create(payload:, idempotency_key: SecureRandom.hex(10))
|
||||
data = partner_api.post("webhooks", payload: payload, idempotency_key: idempotency_key)
|
||||
data = post("webhooks", payload: payload, idempotency_key: idempotency_key)
|
||||
Entity::Webhook.new(**data.merge(idempotency_key: idempotency_key))
|
||||
end
|
||||
|
||||
def list
|
||||
data = partner_api.get("webhooks")
|
||||
data = get("webhooks")
|
||||
Entity::WebhookCollection.new(**data)
|
||||
end
|
||||
|
||||
def delete(webhook_id:)
|
||||
partner_api.delete("webhooks/#{webhook_id}")
|
||||
delete("webhooks/#{webhook_id}")
|
||||
true
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
attr_reader :partner_api
|
||||
end
|
||||
end
|
||||
|
||||
Ссылка в новой задаче
Block a user