зеркало из
https://github.com/glebtv/yookassa.git
synced 2026-09-07 19:35:51 +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
|
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
|
```ruby
|
||||||
client1 = Yookassa::Client.new(shop_id: 'shop_1', api_key: '123')
|
client1 = Yookassa::Payments.new(shop_id: 'shop_1', api_key: '123')
|
||||||
client2 = Yookassa::Client.new(shop_id: 'shop_2', api_key: '456')
|
client2 = Yookassa::Payments.new(shop_id: 'shop_2', api_key: '456')
|
||||||
```
|
```
|
||||||
|
|
||||||
### Making Payments
|
### Making Payments
|
||||||
@@ -68,8 +68,8 @@ payment = Yookassa.payments.create(payment: payload)
|
|||||||
|
|
||||||
# or
|
# or
|
||||||
|
|
||||||
client = Yookassa::Client.new(shop_id: 'shop_1', api_key: '123')
|
payments = Yookassa::Payments.new(shop_id: 'shop_1', api_key: '123')
|
||||||
payment = client.payments.create(payment: payload)
|
payment = payments.create(payment: payload)
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Other payment requests
|
#### Other payment requests
|
||||||
|
|||||||
@@ -4,11 +4,11 @@ require "dry-struct"
|
|||||||
require "forwardable"
|
require "forwardable"
|
||||||
require "yookassa/version"
|
require "yookassa/version"
|
||||||
require "yookassa/config"
|
require "yookassa/config"
|
||||||
require "yookassa/client"
|
require "yookassa/payments"
|
||||||
|
require "yookassa/refunds"
|
||||||
|
require "yookassa/receipts"
|
||||||
|
|
||||||
module Yookassa
|
module Yookassa
|
||||||
ConfigError = Class.new(StandardError)
|
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
extend Forwardable
|
extend Forwardable
|
||||||
|
|
||||||
@@ -20,18 +20,16 @@ module Yookassa
|
|||||||
@config ||= Config.new
|
@config ||= Config.new
|
||||||
end
|
end
|
||||||
|
|
||||||
def client
|
def payments
|
||||||
raise ConfigError, "Specify `shop_id` and `api_key` settings in a `.configure` block" if @config.nil?
|
@payments ||= Payments.new
|
||||||
|
|
||||||
@client ||= Client.new(shop_id: @config.shop_id, api_key: @config.api_key)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def partner_api
|
def refunds
|
||||||
@partner_api ||= PartnerAPI.new
|
@refunds ||= Refunds.new
|
||||||
end
|
end
|
||||||
|
|
||||||
def_delegators :client, :payments, :refunds, :receipts
|
def receipts
|
||||||
|
@receipts ||= Receipts.new
|
||||||
def_delegators :partner_api, :stores, :webhooks
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,9 +1,6 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require "http"
|
require "http"
|
||||||
require_relative "./payments"
|
|
||||||
require_relative "./refunds"
|
|
||||||
require_relative "./receipts"
|
|
||||||
require_relative "./entity/error"
|
require_relative "./entity/error"
|
||||||
|
|
||||||
module Yookassa
|
module Yookassa
|
||||||
@@ -12,21 +9,21 @@ module Yookassa
|
|||||||
|
|
||||||
attr_reader :http
|
attr_reader :http
|
||||||
|
|
||||||
def initialize(shop_id:, api_key:)
|
def initialize(shop_id: Yookassa.config.shop_id, api_key: Yookassa.config.api_key, oauth_token: nil)
|
||||||
@http = HTTP.basic_auth(user: shop_id, pass: api_key).headers(accept: "application/json")
|
@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
|
end
|
||||||
|
|
||||||
def payments
|
private
|
||||||
@payments ||= Payments.new(self)
|
|
||||||
end
|
|
||||||
|
|
||||||
def refunds
|
|
||||||
@refunds ||= Refunds.new(self)
|
|
||||||
end
|
|
||||||
|
|
||||||
def receipts
|
|
||||||
@receipts ||= Receipts.new(self)
|
|
||||||
end
|
|
||||||
|
|
||||||
def get(endpoint, query: {})
|
def get(endpoint, query: {})
|
||||||
api_call { http.get("#{API_URL}#{endpoint}", params: 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) }
|
api_call { http.headers("Idempotence-Key" => idempotency_key).post("#{API_URL}#{endpoint}", json: payload) }
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
def api_call
|
def api_call
|
||||||
response = yield if block_given?
|
response = yield if block_given?
|
||||||
body = JSON.parse(response.body.to_s, symbolize_names: true)
|
body = JSON.parse(response.body.to_s, symbolize_names: true)
|
||||||
|
|||||||
@@ -1,41 +1,34 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require_relative "./client"
|
||||||
require_relative "./entity/payment"
|
require_relative "./entity/payment"
|
||||||
require_relative "./entity/collection"
|
require_relative "./entity/collection"
|
||||||
|
|
||||||
module Yookassa
|
module Yookassa
|
||||||
class Payments
|
class Payments < Client
|
||||||
def initialize(api)
|
|
||||||
@api = api
|
|
||||||
end
|
|
||||||
|
|
||||||
def find(payment_id:)
|
def find(payment_id:)
|
||||||
data = api.get("payments/#{payment_id}")
|
data = get("payments/#{payment_id}")
|
||||||
Entity::Payment.new(**data)
|
Entity::Payment.new(**data)
|
||||||
end
|
end
|
||||||
|
|
||||||
def create(payment:, idempotency_key: SecureRandom.hex(10))
|
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))
|
Entity::Payment.new(**data.merge(idempotency_key: idempotency_key))
|
||||||
end
|
end
|
||||||
|
|
||||||
def capture(payment_id:, idempotency_key: SecureRandom.hex(10))
|
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))
|
Entity::Payment.new(**data.merge(idempotency_key: idempotency_key))
|
||||||
end
|
end
|
||||||
|
|
||||||
def cancel(payment_id:, idempotency_key: SecureRandom.hex(10))
|
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))
|
Entity::Payment.new(**data.merge(idempotency_key: idempotency_key))
|
||||||
end
|
end
|
||||||
|
|
||||||
def list(filters: {})
|
def list(filters: {})
|
||||||
data = api.get("payments", query: filters)
|
data = get("payments", query: filters)
|
||||||
Entity::PaymentCollection.new(**data)
|
Entity::PaymentCollection.new(**data)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
attr_reader :api
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,31 +1,24 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require_relative "./client"
|
||||||
require_relative "./entity/receipt"
|
require_relative "./entity/receipt"
|
||||||
require_relative "./entity/collection"
|
require_relative "./entity/collection"
|
||||||
|
|
||||||
module Yookassa
|
module Yookassa
|
||||||
class Receipts
|
class Receipts < Client
|
||||||
def initialize(api)
|
|
||||||
@api = api
|
|
||||||
end
|
|
||||||
|
|
||||||
def find(receipt_id:)
|
def find(receipt_id:)
|
||||||
data = api.get("receipts/#{receipt_id}")
|
data = get("receipts/#{receipt_id}")
|
||||||
Entity::Receipt.new(**data)
|
Entity::Receipt.new(**data)
|
||||||
end
|
end
|
||||||
|
|
||||||
def create(payload:, idempotency_key: SecureRandom.hex(10))
|
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))
|
Entity::Receipt.new(**data.merge(idempotency_key: idempotency_key))
|
||||||
end
|
end
|
||||||
|
|
||||||
def list(filters: {})
|
def list(filters: {})
|
||||||
data = api.get("receipts", query: filters)
|
data = get("receipts", query: filters)
|
||||||
Entity::ReceiptCollection.new(**data)
|
Entity::ReceiptCollection.new(**data)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
attr_reader :api
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,31 +1,24 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require_relative "./client"
|
||||||
require_relative "./entity/refund"
|
require_relative "./entity/refund"
|
||||||
require_relative "./entity/collection"
|
require_relative "./entity/collection"
|
||||||
|
|
||||||
module Yookassa
|
module Yookassa
|
||||||
class Refunds
|
class Refunds < Client
|
||||||
def initialize(api)
|
|
||||||
@api = api
|
|
||||||
end
|
|
||||||
|
|
||||||
def find(payment_id:)
|
def find(payment_id:)
|
||||||
data = api.get("refunds/#{payment_id}")
|
data = get("refunds/#{payment_id}")
|
||||||
Entity::Refund.new(**data)
|
Entity::Refund.new(**data)
|
||||||
end
|
end
|
||||||
|
|
||||||
def create(payload:, idempotency_key: SecureRandom.hex(10))
|
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))
|
Entity::Refund.new(**data.merge(idempotency_key: idempotency_key))
|
||||||
end
|
end
|
||||||
|
|
||||||
def list(filters: {})
|
def list(filters: {})
|
||||||
data = api.get("refunds", query: filters)
|
data = get("refunds", query: filters)
|
||||||
Entity::RefundCollection.new(**data)
|
Entity::RefundCollection.new(**data)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
attr_reader :api
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,20 +1,13 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require_relative "./client"
|
||||||
require_relative "./entity/store_info"
|
require_relative "./entity/store_info"
|
||||||
|
|
||||||
module Yookassa
|
module Yookassa
|
||||||
class Stores
|
class Stores < Client
|
||||||
def initialize(partner_api)
|
def info
|
||||||
@partner_api = partner_api
|
data = get("me")
|
||||||
end
|
|
||||||
|
|
||||||
def me
|
|
||||||
data = partner_api.get("me")
|
|
||||||
Entity::StoreInfo.new(**data)
|
Entity::StoreInfo.new(**data)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
attr_reader :partner_api
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -1,31 +1,24 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
require_relative "./client"
|
||||||
require_relative "./entity/webhook"
|
require_relative "./entity/webhook"
|
||||||
require_relative "./entity/collection"
|
require_relative "./entity/collection"
|
||||||
|
|
||||||
module Yookassa
|
module Yookassa
|
||||||
class Webhooks
|
class Webhooks < Client
|
||||||
def initialize(partner_api)
|
|
||||||
@partner_api = partner_api
|
|
||||||
end
|
|
||||||
|
|
||||||
def create(payload:, idempotency_key: SecureRandom.hex(10))
|
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))
|
Entity::Webhook.new(**data.merge(idempotency_key: idempotency_key))
|
||||||
end
|
end
|
||||||
|
|
||||||
def list
|
def list
|
||||||
data = partner_api.get("webhooks")
|
data = get("webhooks")
|
||||||
Entity::WebhookCollection.new(**data)
|
Entity::WebhookCollection.new(**data)
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete(webhook_id:)
|
def delete(webhook_id:)
|
||||||
partner_api.delete("webhooks/#{webhook_id}")
|
delete("webhooks/#{webhook_id}")
|
||||||
true
|
true
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
attr_reader :partner_api
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -2,10 +2,9 @@
|
|||||||
|
|
||||||
RSpec.describe Yookassa::Payments do
|
RSpec.describe Yookassa::Payments do
|
||||||
let(:config) { { shop_id: "SHOP_ID", api_key: "API_KEY" } }
|
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(:idempotency_key) { SecureRandom.hex(1) }
|
||||||
|
|
||||||
let(:payment) { client.payments }
|
|
||||||
let(:body) { File.read("spec/fixtures/payment_response.json") }
|
let(:body) { File.read("spec/fixtures/payment_response.json") }
|
||||||
|
|
||||||
before { stub_request(:any, //).to_return(body: body, headers: { "Content-Type" => "application/json" }) }
|
before { stub_request(:any, //).to_return(body: body, headers: { "Content-Type" => "application/json" }) }
|
||||||
|
|||||||
@@ -2,9 +2,8 @@
|
|||||||
|
|
||||||
RSpec.describe Yookassa::Receipts do
|
RSpec.describe Yookassa::Receipts do
|
||||||
let(:config) { { shop_id: "SHOP_ID", api_key: "API_KEY" } }
|
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(:idempotency_key) { SecureRandom.hex(1) }
|
||||||
let(:receipt) { client.receipts }
|
|
||||||
let(:body) { File.read("spec/fixtures/receipt_response.json") }
|
let(:body) { File.read("spec/fixtures/receipt_response.json") }
|
||||||
|
|
||||||
before { stub_request(:any, //).to_return(body: body, headers: { "Content-Type" => "application/json" }) }
|
before { stub_request(:any, //).to_return(body: body, headers: { "Content-Type" => "application/json" }) }
|
||||||
|
|||||||
@@ -2,9 +2,8 @@
|
|||||||
|
|
||||||
RSpec.describe Yookassa::Refunds do
|
RSpec.describe Yookassa::Refunds do
|
||||||
let(:config) { { shop_id: "SHOP_ID", api_key: "API_KEY" } }
|
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(:idempotency_key) { SecureRandom.hex(1) }
|
||||||
let(:refund) { client.refunds }
|
|
||||||
let(:body) { File.read("spec/fixtures/refund_response.json") }
|
let(:body) { File.read("spec/fixtures/refund_response.json") }
|
||||||
|
|
||||||
before { stub_request(:any, //).to_return(body: body, headers: { "Content-Type" => "application/json" }) }
|
before { stub_request(:any, //).to_return(body: body, headers: { "Content-Type" => "application/json" }) }
|
||||||
|
|||||||
@@ -19,23 +19,6 @@ RSpec.describe Yookassa do
|
|||||||
end
|
end
|
||||||
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
|
describe ".payments" do
|
||||||
it "delegates request to client and creates an instance" do
|
it "delegates request to client and creates an instance" do
|
||||||
expect(Yookassa.payments).to be_a(Yookassa::Payments)
|
expect(Yookassa.payments).to be_a(Yookassa::Payments)
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user