зеркало из
https://github.com/glebtv/yookassa.git
synced 2026-08-28 15:16:18 +03:00
Preparing clients for partner api (#25)
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
6072b2ae0f
Коммит
7f79ffa4b4
@@ -44,8 +44,8 @@ There are some cases, when you need to connect to different Yookassa accounts (s
|
||||
If that is not your case, and you still have multiple shop_ids and api_keys, and need to handle all of them under one application, then you need to instantiate clients inline
|
||||
|
||||
```ruby
|
||||
client1 = Yookassa::Client.new(shop_id: 'shop_1', api_key: '123')
|
||||
client2 = Yookassa::Client.new(shop_id: 'shop_2', api_key: '456')
|
||||
client1 = Yookassa::Payments.new(shop_id: 'shop_1', api_key: '123')
|
||||
client2 = Yookassa::Payments.new(shop_id: 'shop_2', api_key: '456')
|
||||
```
|
||||
|
||||
### Making Payments
|
||||
@@ -68,8 +68,8 @@ payment = Yookassa.payments.create(payment: payload)
|
||||
|
||||
# or
|
||||
|
||||
client = Yookassa::Client.new(shop_id: 'shop_1', api_key: '123')
|
||||
payment = client.payments.create(payment: payload)
|
||||
payments = Yookassa::Payments.new(shop_id: 'shop_1', api_key: '123')
|
||||
payment = payments.create(payment: payload)
|
||||
```
|
||||
|
||||
#### Other payment requests
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -2,10 +2,9 @@
|
||||
|
||||
RSpec.describe Yookassa::Payments do
|
||||
let(:config) { { shop_id: "SHOP_ID", api_key: "API_KEY" } }
|
||||
let(:client) { Yookassa::Client.new(**config) }
|
||||
let(:payment) { described_class.new(**config) }
|
||||
let(:idempotency_key) { SecureRandom.hex(1) }
|
||||
|
||||
let(:payment) { client.payments }
|
||||
let(:body) { File.read("spec/fixtures/payment_response.json") }
|
||||
|
||||
before { stub_request(:any, //).to_return(body: body, headers: { "Content-Type" => "application/json" }) }
|
||||
|
||||
@@ -2,9 +2,8 @@
|
||||
|
||||
RSpec.describe Yookassa::Receipts do
|
||||
let(:config) { { shop_id: "SHOP_ID", api_key: "API_KEY" } }
|
||||
let(:client) { Yookassa::Client.new(**config) }
|
||||
let(:receipt) { described_class.new(**config) }
|
||||
let(:idempotency_key) { SecureRandom.hex(1) }
|
||||
let(:receipt) { client.receipts }
|
||||
let(:body) { File.read("spec/fixtures/receipt_response.json") }
|
||||
|
||||
before { stub_request(:any, //).to_return(body: body, headers: { "Content-Type" => "application/json" }) }
|
||||
|
||||
@@ -2,9 +2,8 @@
|
||||
|
||||
RSpec.describe Yookassa::Refunds do
|
||||
let(:config) { { shop_id: "SHOP_ID", api_key: "API_KEY" } }
|
||||
let(:client) { Yookassa::Client.new(**config) }
|
||||
let(:refund) { described_class.new(**config) }
|
||||
let(:idempotency_key) { SecureRandom.hex(1) }
|
||||
let(:refund) { client.refunds }
|
||||
let(:body) { File.read("spec/fixtures/refund_response.json") }
|
||||
|
||||
before { stub_request(:any, //).to_return(body: body, headers: { "Content-Type" => "application/json" }) }
|
||||
|
||||
@@ -19,23 +19,6 @@ RSpec.describe Yookassa do
|
||||
end
|
||||
end
|
||||
|
||||
describe ".client" do
|
||||
context "when no settings are provided" do
|
||||
before { Yookassa.instance_variable_set(:@config, nil) }
|
||||
|
||||
it "raises an error" do
|
||||
expect { Yookassa.client }.to raise_error(Yookassa::ConfigError)
|
||||
end
|
||||
end
|
||||
|
||||
context "when instance configured" do
|
||||
it "creates and stores client" do
|
||||
expect(Yookassa.client).to be_a(Yookassa::Client)
|
||||
expect(Yookassa.client).to eq(Yookassa.client)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
describe ".payments" do
|
||||
it "delegates request to client and creates an instance" do
|
||||
expect(Yookassa.payments).to be_a(Yookassa::Payments)
|
||||
|
||||
Ссылка в новой задаче
Block a user