зеркало из
https://github.com/glebtv/yookassa.git
synced 2026-09-08 20:05:50 +03:00
add payment
Этот коммит содержится в:
@@ -1,7 +1,9 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'evil/client'
|
require 'evil/client'
|
||||||
|
require 'pry'
|
||||||
|
|
||||||
require 'yandex-checkout/version'
|
require_relative 'yandex-checkout/version'
|
||||||
require 'yandex-checkout/payment'
|
require_relative 'yandex-checkout/payment'
|
||||||
require 'yandex-checkout/refund'
|
require_relative 'yandex-checkout/refund'
|
||||||
|
require_relative 'yandex-checkout/response'
|
||||||
|
|||||||
@@ -9,53 +9,83 @@ module YandexCheckout
|
|||||||
security { basic_auth shop_id, api_key }
|
security { basic_auth shop_id, api_key }
|
||||||
|
|
||||||
# http_method { :get }
|
# http_method { :get }
|
||||||
scope :payments do
|
# scope :payments do
|
||||||
operation :status do
|
# operation :status do
|
||||||
option :payment_id, proc(&:to_s)
|
# option :payment_id, proc(&:to_s)
|
||||||
|
|
||||||
http_method :get
|
# http_method :get
|
||||||
path { "payments/#{payment_id}" }
|
# path { "payments/#{payment_id}" }
|
||||||
|
|
||||||
response 200 do |_status, _headers, body|
|
# response 200 do |_status, _headers, body|
|
||||||
p JSON.parse(*body)
|
# p JSON.parse(*body)
|
||||||
end
|
# end
|
||||||
|
|
||||||
# Parses json response, wraps it into model with [#error] and raises
|
# # Parses json response, wraps it into model with [#error] and raises
|
||||||
# an exception where [ResponseError#response] contains the model instance
|
# # an exception where [ResponseError#response] contains the model instance
|
||||||
|
|
||||||
response(400, 422) do |(status, *)|
|
# response(400, 422) do |(status, *)|
|
||||||
raise "#{status}: Record invalid"
|
# raise "#{status}: Record invalid"
|
||||||
end
|
# end
|
||||||
|
|
||||||
response(404) do |(_status, *)|
|
# response(404) do |(_status, *)|
|
||||||
p 'Record Not Found'
|
# p 'Record Not Found'
|
||||||
# raise "#{status}: Record not found"
|
# # raise "#{status}: Record not found"
|
||||||
end
|
# end
|
||||||
|
# end
|
||||||
|
|
||||||
|
operation :get_payment_info do
|
||||||
|
option :payment_id, proc(&:to_s)
|
||||||
|
|
||||||
|
http_method :get
|
||||||
|
path { "payments/#{payment_id}" }
|
||||||
|
|
||||||
|
# response 200 do |_status, _headers, body|
|
||||||
|
# p JSON.parse(*body)
|
||||||
|
# end
|
||||||
|
response(200) { |*res| p Response.build(*res) }
|
||||||
|
|
||||||
|
# Parses json response, wraps it into model with [#error] and raises
|
||||||
|
# an exception where [ResponseError#response] contains the model instance
|
||||||
|
|
||||||
|
response(400, 422) do |(status, *)|
|
||||||
|
raise "#{status}: Record invalid"
|
||||||
end
|
end
|
||||||
|
|
||||||
operation :create do
|
response(404) do |(_status, *)|
|
||||||
option :payment
|
p 'Record Not Found'
|
||||||
option :idempotency_key, optional: true
|
# raise "#{status}: Record not found"
|
||||||
|
|
||||||
http_method :post
|
|
||||||
|
|
||||||
path { 'payments' }
|
|
||||||
# query { { params: params } }
|
|
||||||
format 'json'
|
|
||||||
headers 'Idempotence-Key' => '244afbdc-000f-5000-a000-12526186ce10'
|
|
||||||
body { payment }
|
|
||||||
|
|
||||||
response 200 do |_status, _headers, body|
|
|
||||||
p JSON.parse(*body)
|
|
||||||
end
|
|
||||||
|
|
||||||
# Parses json response, wraps it into model with [#error] and raises
|
|
||||||
# an exception where [ResponseError#response] contains the model instance
|
|
||||||
|
|
||||||
response(400, 422) do |(status, *)|
|
|
||||||
raise "#{status}: Record invalid"
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
operation :create do
|
||||||
|
option :payment
|
||||||
|
option :idempotency_key, optional: true
|
||||||
|
|
||||||
|
http_method :post
|
||||||
|
|
||||||
|
path { 'payments' }
|
||||||
|
# query { { params: params } }
|
||||||
|
format 'json'
|
||||||
|
headers 'Idempotence-Key' => '244afbdc-000f-5000-a000-12526186ce10'
|
||||||
|
body { payment }
|
||||||
|
|
||||||
|
response 200 do |_status, _headers, body|
|
||||||
|
p JSON.parse(*body)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Parses json response, wraps it into model with [#error] and raises
|
||||||
|
# an exception where [ResponseError#response] contains the model instance
|
||||||
|
|
||||||
|
response(400, 422) do |(status, *)|
|
||||||
|
raise "#{status}: Record invalid"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
operation :capture do
|
||||||
|
end
|
||||||
|
|
||||||
|
operation :cancel do
|
||||||
|
end
|
||||||
|
# end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -2,6 +2,52 @@
|
|||||||
|
|
||||||
module YandexCheckout
|
module YandexCheckout
|
||||||
class Refund < Evil::Client
|
class Refund < Evil::Client
|
||||||
|
operation :get_refund_info do
|
||||||
|
option :payment_id, proc(&:to_s)
|
||||||
|
|
||||||
|
http_method :get
|
||||||
|
path { "payments/#{payment_id}" }
|
||||||
|
|
||||||
|
response 200 do |_status, _headers, body|
|
||||||
|
p JSON.parse(*body)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Parses json response, wraps it into model with [#error] and raises
|
||||||
|
# an exception where [ResponseError#response] contains the model instance
|
||||||
|
|
||||||
|
response(400, 422) do |(status, *)|
|
||||||
|
raise "#{status}: Record invalid"
|
||||||
|
end
|
||||||
|
|
||||||
|
response(404) do |(_status, *)|
|
||||||
|
p 'Record Not Found'
|
||||||
|
# raise "#{status}: Record not found"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
|
||||||
|
operation :create do
|
||||||
|
option :payment
|
||||||
|
option :idempotency_key, optional: true
|
||||||
|
|
||||||
|
http_method :post
|
||||||
|
|
||||||
|
path { 'payments' }
|
||||||
|
# query { { params: params } }
|
||||||
|
format 'json'
|
||||||
|
headers 'Idempotence-Key' => '244afbdc-000f-5000-a000-12526186ce10'
|
||||||
|
body { payment }
|
||||||
|
|
||||||
|
response 200 do |_status, _headers, body|
|
||||||
|
p JSON.parse(*body)
|
||||||
|
end
|
||||||
|
|
||||||
|
# Parses json response, wraps it into model with [#error] and raises
|
||||||
|
# an exception where [ResponseError#response] contains the model instance
|
||||||
|
|
||||||
|
response(400, 422) do |(status, *)|
|
||||||
|
raise "#{status}: Record invalid"
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
22
lib/yandex-checkout/response.rb
Обычный файл
22
lib/yandex-checkout/response.rb
Обычный файл
@@ -0,0 +1,22 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module YandexCheckout
|
||||||
|
class Response
|
||||||
|
extend Dry::Initializer
|
||||||
|
option :id, proc(&:to_s)
|
||||||
|
option :status, proc(&:to_s), default: proc { nil }
|
||||||
|
option :test
|
||||||
|
|
||||||
|
class << self
|
||||||
|
def build(*res)
|
||||||
|
body = res.last
|
||||||
|
binding.pry
|
||||||
|
new JSON.parse(body.first)
|
||||||
|
end
|
||||||
|
|
||||||
|
def new(opts)
|
||||||
|
super opts.each_with_object({}) { |(key, val), obj| obj[key.to_sym] = val }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,3 +1,5 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module YandexCheckout
|
module YandexCheckout
|
||||||
VERSION = '0.1.0'
|
VERSION = '0.1.0'
|
||||||
end
|
end
|
||||||
|
|||||||
14
spec/fixtures/payment.json
поставляемый
Обычный файл
14
spec/fixtures/payment.json
поставляемый
Обычный файл
@@ -0,0 +1,14 @@
|
|||||||
|
{
|
||||||
|
"amount": {
|
||||||
|
"value": "10.00",
|
||||||
|
"currency": "RUB"
|
||||||
|
},
|
||||||
|
"payment_method_data": {
|
||||||
|
"type": "bank_card"
|
||||||
|
},
|
||||||
|
"confirmation": {
|
||||||
|
"type": "redirect",
|
||||||
|
"return_url": "https://www.merchant-website.com/return_url"
|
||||||
|
},
|
||||||
|
"description": "Заказ №72"
|
||||||
|
}
|
||||||
29
spec/yandex-checkout/payment_spec.rb
Обычный файл
29
spec/yandex-checkout/payment_spec.rb
Обычный файл
@@ -0,0 +1,29 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
RSpec.describe YandexCheckout::Payment do
|
||||||
|
describe '#make_payment' do
|
||||||
|
let(:settings) { { shop_id: 'SHOP_ID', api_key: 'API_KEY' } }
|
||||||
|
let(:idempotence_key) { 123 }
|
||||||
|
let(:payment) { described_class.new(settings) }
|
||||||
|
let(:params) { { payment: File.read('spec/fixtures/payment.json') } }
|
||||||
|
let(:answer) { { result: 'accepted' } }
|
||||||
|
|
||||||
|
before { stub_request(:any, //).to_return(body: answer.to_json) }
|
||||||
|
subject { payment.create(params) }
|
||||||
|
|
||||||
|
context 'using ssl via POST:' do
|
||||||
|
let(:url) { 'https://payment.yandex.net/api/v3/payments' }
|
||||||
|
|
||||||
|
it 'sends a request' do
|
||||||
|
# binding.pry
|
||||||
|
subject
|
||||||
|
expect(a_request(:post, url)).to have_been_made
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'returns success' do
|
||||||
|
expect(subject).to be_kind_of YandexCheckout::Response
|
||||||
|
expect(subject.result).to eq 'accepted'
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@@ -1,9 +1,5 @@
|
|||||||
RSpec.describe YandexCheckout do
|
RSpec.describe YandexCheckout do
|
||||||
it "has a version number" do
|
it 'has a version number' do
|
||||||
expect(YandexCheckout::VERSION).not_to be nil
|
expect(YandexCheckout::VERSION).not_to be nil
|
||||||
end
|
end
|
||||||
|
|
||||||
it "does something useful" do
|
|
||||||
expect(false).to eq(true)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|||||||
Ссылка в новой задаче
Block a user