Use real currency for Taler payments unless using demo backend.

This commit is contained in:
Maikel Linke
2026-03-23 12:00:54 +11:00
parent 1e6de5e251
commit fe0c6a4deb
3 changed files with 33 additions and 3 deletions

View File

@@ -18,6 +18,9 @@ module Spree
# - backend_url: https://backend.demo.taler.net/instances/sandbox
# - api_key: sandbox
class Taler < PaymentMethod
# Demo backend instances will use the KUDOS currency.
DEMO_PREFIX = "https://backend.demo.taler.net/instances"
preference :backend_url, :string
preference :api_key, :password
@@ -96,7 +99,7 @@ module Spree
def create_taler_order(payment)
# We are ignoring currency for now so that we can test with the
# current demo backend only working with the KUDOS currency.
taler_amount = "KUDOS:#{payment.amount}"
taler_amount = "#{currency(payment)}:#{payment.amount}"
urls = Rails.application.routes.url_helpers
fulfillment_url = urls.payment_gateways_confirm_taler_url(payment_id: payment.id)
taler_order.create(
@@ -113,6 +116,12 @@ module Spree
id:,
)
end
def currency(payment)
return "KUDOS" if preferred_backend_url.starts_with?(DEMO_PREFIX)
payment.order.currency
end
end
end
end

View File

@@ -12,8 +12,8 @@ RSpec.describe Spree::PaymentMethod::Taler do
let(:backend_url) { "https://backend.demo.taler.net/instances/sandbox" }
let(:token_url) { "#{backend_url}/private/token" }
describe "#external_payment_url", vcr: true do
it "creates an order reference and retrieves a URL to pay at" do
describe "#external_payment_url" do
it "creates an order reference and retrieves a URL to pay at", vcr: true do
order = create(:order_ready_for_confirmation, payment_method: taler)
url = subject.external_payment_url(order:)
@@ -23,6 +23,26 @@ RSpec.describe Spree::PaymentMethod::Taler do
payment = order.payments.last.reload
expect(payment.response_code).to match "20...[0-9A-Z-]{17}$"
end
it "creates the Taler order with the right currency" do
order = create(:order_ready_for_confirmation, payment_method: taler)
backend_url = "https://taler.example.com"
token_url = "https://taler.example.com/private/token"
order_url = "https://taler.example.com/private/orders"
taler = Spree::PaymentMethod::Taler.new(
preferred_backend_url: "https://taler.example.com",
preferred_api_key: "sandbox",
)
stub_request(:post, token_url).to_return(body: { token: "1234" }.to_json)
stub_request(:post, order_url)
.with(body: /"amount":"AUD:10.0"/)
.to_return(body: { order_id: "one" }.to_json)
url = taler.external_payment_url(order:)
expect(url).to eq "#{backend_url}/orders/one"
end
end
describe "#purchase" do

View File

@@ -370,6 +370,7 @@ RSpec.describe "As a consumer, I want to checkout my order" do
Spree::PaymentMethod::Taler.create!(
name: "Taler",
environment: "test",
preferred_backend_url: "https://taler.example.com/",
distributors: [distributor]
)
end