diff --git a/server/implementations/Catcrafts.Server-Http.cpp b/server/implementations/Catcrafts.Server-Http.cpp index ade1dfe..ccae6d0 100644 --- a/server/implementations/Catcrafts.Server-Http.cpp +++ b/server/implementations/Catcrafts.Server-Http.cpp @@ -326,14 +326,9 @@ HTTPResponse RenderPage(std::string_view target) { pay.address = instr->address; pay.amount = instr->amount; // Set only by bank-transfer rails, and what the renderer - // switches on. The structured reference is derived from the - // order token rather than carried by the rail, so the two - // forms the page prints cannot disagree with each other. + // switches on. pay.beneficiary = instr->beneficiary; pay.bic = instr->bic; - if (!instr->beneficiary.empty()) { - pay.structuredReference = CreditorReferenceFromToken(order->token); - } const std::int64_t now = std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()).count(); diff --git a/shared/interfaces/Catcrafts.Shared-Html.cppm b/shared/interfaces/Catcrafts.Shared-Html.cppm index c988d53..02c2c0b 100644 --- a/shared/interfaces/Catcrafts.Shared-Html.cppm +++ b/shared/interfaces/Catcrafts.Shared-Html.cppm @@ -162,6 +162,9 @@ export SafeHtml Url(std::string_view attrName, std::string_view href) { // protocol-handler scheme: the browser hands it to a wallet or does // nothing. No script context is reachable through it. || StartsWithNoCase(cleaned, "ethereum:") + // RFC 8905 bank-transfer links on the same page. The same kind of + // scheme: handed to a payment app, or nothing happens. + || StartsWithNoCase(cleaned, "payto:") // Site-relative, but NOT protocol-relative ("//evil.example" would // leave the origin while looking like a path). || (cleaned.size() >= 1 && cleaned[0] == '/' diff --git a/shared/interfaces/Catcrafts.Shared-Model.cppm b/shared/interfaces/Catcrafts.Shared-Model.cppm index 4379f14..a213459 100644 --- a/shared/interfaces/Catcrafts.Shared-Model.cppm +++ b/shared/interfaces/Catcrafts.Shared-Model.cppm @@ -380,11 +380,6 @@ export struct OrderCryptoPay { // bank holds. Printing a friendly trading name here would scare buyers off // at the last step, so this carries the bank's own spelling. std::string beneficiary; - // The structured ISO 11649 form of the reference. Quoted alongside the - // short one because the payer's bank validates its check digits and - // rejects a mistyped one before the money leaves — which is what makes - // unattended matching on our side safe. - std::string structuredReference; // Shown only when configured, and labelled as the field a payer sending // from OUTSIDE SEPA needs. An IBAN alone has been sufficient within SEPA // since 2016, so putting a BIC in front of a Dutch buyer is one more box diff --git a/shared/interfaces/Catcrafts.Shared-Views.cppm b/shared/interfaces/Catcrafts.Shared-Views.cppm index dd78377..088b84e 100644 --- a/shared/interfaces/Catcrafts.Shared-Views.cppm +++ b/shared/interfaces/Catcrafts.Shared-Views.cppm @@ -1414,6 +1414,45 @@ SafeHtml MoneyRow(std::string_view label, std::int64_t minor) { Escape(label), Escape(Money::FormatEuro(minor))); } +// RFC 3986 percent-encoding for one URI query value: unreserved characters +// stay, every other byte becomes %XX. Byte-wise on purpose, so a non-ASCII +// account-holder name goes out as its UTF-8 bytes. +std::string PercentEncode(std::string_view s) { + static constexpr std::string_view kHex = "0123456789ABCDEF"; + std::string out; + out.reserve(s.size() * 3); + for (const char ch : s) { + const auto c = static_cast(ch); + if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') + || c == '-' || c == '.' || c == '_' || c == '~') { + out += ch; + } else { + out += '%'; + out += kHex[c >> 4]; + out += kHex[c & 0x0F]; + } + } + return out; +} + +// The bank-transfer instructions as one RFC 8905 payto URI, which a payment +// app that handles the scheme opens with every field filled in. Built from +// the very values the manual details print, so the two cannot disagree. The +// IBAN is compacted because a configured one may carry display spaces, and +// the BIC is left out: only a payer outside SEPA needs it, and the plain +// payto://iban/ form is the one a handler is surest to parse. +std::string PaytoUri(std::string_view iban, std::int64_t totalMinor, + std::string_view receiver, std::string_view message) { + std::string compact; + for (const char c : iban) { + if (c >= 'a' && c <= 'z') compact += static_cast(c - 'a' + 'A'); + else if ((c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9')) compact += c; + } + return std::format("payto://iban/{}?amount=EUR:{}&receiver-name={}&message={}", + compact, Money::FormatMinor(totalMinor), + PercentEncode(receiver), PercentEncode(message)); +} + // The order status page. The token in the URL is the whole capability: no // account, no login — bookmark the page. Rendered server-side only, because // only the server knows the order; the shared fallback below covers the app. @@ -1548,8 +1587,13 @@ export RenderedPage RenderOrderStatus(const OrderView& o, std::string_view indic // Both live rails are self-hosted and so both land in this branch; // what separates them is which of these two blocks renders. if (!pay.beneficiary.empty()) { - // Three fields, in the order a banking app asks for them, so the - // buyer can work straight down the page instead of hunting. + // Two ways to pay, quickest first. The payto link hands every + // field below to the buyer's payment app in one go, so nothing is + // retyped; the manual details are the same values for everyone + // whose app does not take such links. + // + // The manual fields are in the order a banking app asks for them, + // so the buyer can work straight down the page instead of hunting. // // The name comes FIRST and is labelled as exact on purpose. Every // euro-area transfer is now name-checked against the IBAN, and a @@ -1557,33 +1601,33 @@ export RenderedPage RenderOrderStatus(const OrderView& o, std::string_view indic // moment of paying. Telling them why the spelling looks odd is // cheaper than losing the payment to a scary red banner. // - // Both reference forms are offered because banks disagree about - // where a reference goes: those with a dedicated payment-reference - // field validate the RF form's check digits and refuse a mistyped - // one before the money moves, which is the safer path; the rest - // only have a free-text description, where the short code is what - // a human will actually copy correctly. + // One reference, the short order code, in the description field + // every bank has. The matcher finds it anywhere in that text. + // Offering the structured RF form as well, for banks with a + // separate reference field, left buyers unsure which code went + // where. payBlock = Format( R"(
)" R"(

Pay by bank transfer

)" - R"(

Transfer {} to this account:)" + R"(

Open this link in your payment app:

)" + R"()" + R"(

Or do a manual transfer of {} to this account:

)" R"(
)" R"(
Account holder
{}
)" R"(
IBAN
{}
)" R"({})" - R"(
Payment reference
{}
)" + R"(
Description
{}
)" R"(
)" R"({})" - R"(

Please copy the the fields )" - R"(exactly as written above. If your bank )" - R"(has a separate field for a payment reference, use )" - R"({} there, it is checked for typing errors. )" - R"(Otherwise put {} in the description. )" - R"(Without the reference the payment cannot be matched to your )" - R"(order. If you send too little, transfer the rest the same )" - R"(way and the order confirms once the total arrives.

)" + R"(

Please copy the fields exactly as written )" + R"(above. Without the description the payment cannot be matched )" + R"(to your order. If you send too little, transfer the rest the )" + R"(same way and the order confirms once the total arrives.

)" R"({})" R"(
)", + Url("href", PaytoUri(pay.address, o.totalMinor, pay.beneficiary, + o.reference)), + Escape(Money::FormatEuro(o.totalMinor)), Escape(Money::FormatEuro(o.totalMinor)), Escape(pay.beneficiary), Escape(pay.address), @@ -1595,12 +1639,8 @@ export RenderedPage RenderOrderStatus(const OrderView& o, std::string_view indic R"(asks for it, usually outside Europe))" R"(
{}
)", Escape(pay.bic)), - Escape(pay.structuredReference.empty() ? o.reference - : pay.structuredReference), - indicativeLine, - Escape(pay.structuredReference.empty() ? o.reference - : pay.structuredReference), Escape(o.reference), + indicativeLine, windowLine); } else { payBlock = Format( diff --git a/styles/styles.css b/styles/styles.css index 7aa0797..4411f09 100644 --- a/styles/styles.css +++ b/styles/styles.css @@ -803,6 +803,8 @@ treatment it replaces, which read as the wrong category for the work. user-select: all; -webkit-user-select: all; } + /* The payto button, set apart from the manual-transfer option below it. */ + .order__paylink { margin-block-end: var(--s-1); } .order__chains { list-style: none; padding-left: 0; } .order__chains li { margin-block-end: var(--s-1); max-width: var(--measure); } .order__chains small { color: var(--text-faint); } diff --git a/tests/ShouldEscapeHtml/main.cpp b/tests/ShouldEscapeHtml/main.cpp index a49eacc..74ef76b 100644 --- a/tests/ShouldEscapeHtml/main.cpp +++ b/tests/ShouldEscapeHtml/main.cpp @@ -84,6 +84,11 @@ int main() { CheckEq(Url("href", "ethereum:0xAbC@8453/transfer?address=0xDeF&uint256=1000000"), " href=\"ethereum:0xAbC@8453/transfer?address=0xDeF&uint256=1000000\"", "url: ethereum: EIP-681 kept verbatim, ampersand escaped"); + // The bank rail's counterpart: an RFC 8905 payto link. Same two rules, + // and the percent-encoded name must come through untouched. + CheckEq(Url("href", "payto://iban/NL00TEST0123456789?amount=EUR:12.50&receiver-name=A%20B&message=CC-ABCDEF"), + " href=\"payto://iban/NL00TEST0123456789?amount=EUR:12.50&receiver-name=A%20B&message=CC-ABCDEF\"", + "url: payto: RFC 8905 kept verbatim, ampersand escaped"); // Empty href fails every allowlist branch — including the site-relative // one, which needs at least one character — so it lands on "#" rather // than emitting a link that resolves to the current page. diff --git a/tests/ShouldSettleBankTransfers/main.cpp b/tests/ShouldSettleBankTransfers/main.cpp index b56f670..4b9bff3 100644 --- a/tests/ShouldSettleBankTransfers/main.cpp +++ b/tests/ShouldSettleBankTransfers/main.cpp @@ -157,11 +157,17 @@ int main(int argc, char** argv) { srv.BodyHas(path, "Catcrafts E2E", "the beneficiary name is shown"); srv.BodyHas(path, kBic, "the BIC is shown when one is configured"); srv.BodyHas(path, order.reference, "the short reference is shown"); - // The structured form is what a payer's own bank check-digit-validates, - // so its absence would quietly remove the protection that makes - // unattended matching safe. - srv.BodyHas(path, "RF", "the structured ISO 11649 reference is shown"); srv.BodyHas(path, "€12.50", "the exact amount to transfer"); + // The payto link is a second copy of the same instructions, filled + // into the payer's app unread, so every field in it must be the one + // the manual details print. The message must be exactly the reference + // the matcher looks for, and the name must survive percent-encoding: + // the test name's space and comma are there to prove that. + srv.BodyHas(path, + std::format("href=\"payto://iban/{}?amount=EUR:12.50" + "&receiver-name=Catcrafts%20E2E%2C%20not%20a%20real%20account" + "&message={}\"", kIban, order.reference), + "a payto link carries the same IBAN, amount, name and reference"); // No hosted checkout exists, so nothing may invite the buyer to leave. srv.BodyLacks(path, "Resume payment", "a self-hosted rail offers no hosted checkout button");