<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Paul Guilbert on Medium]]></title>
        <description><![CDATA[Stories by Paul Guilbert on Medium]]></description>
        <link>https://medium.com/@pguilbert?source=rss-f09533155ef2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*BhAmhwa6sAGSjNQEUy4tdg.jpeg</url>
            <title>Stories by Paul Guilbert on Medium</title>
            <link>https://medium.com/@pguilbert?source=rss-f09533155ef2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 13:06:44 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pguilbert/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Payment at Back Market: one form to rule them all]]></title>
            <link>https://engineering.backmarket.com/payment-at-back-market-one-form-to-rule-them-all-e34b1a7cf044?source=rss-f09533155ef2------2</link>
            <guid isPermaLink="false">https://medium.com/p/e34b1a7cf044</guid>
            <category><![CDATA[tech]]></category>
            <category><![CDATA[apple-pay]]></category>
            <category><![CDATA[frontend]]></category>
            <category><![CDATA[payments]]></category>
            <dc:creator><![CDATA[Paul Guilbert]]></dc:creator>
            <pubDate>Wed, 19 Apr 2023 14:17:23 GMT</pubDate>
            <atom:updated>2023-04-19T14:17:23.235Z</atom:updated>
            <content:encoded><![CDATA[<h4>Discover how we successfully integrated 15 different payment methods into a single form.</h4><figure><img alt="Edition of the Lord of the Ring poster, with the message “one form to rule them all”." src="https://cdn-images-1.medium.com/max/1024/0*oiwQd3BmbjrTx8-Q" /></figure><p>Let’s face it — you have just spent hours of research on your next reborn tech item, and now found it on <a href="https://www.backmarket.com/">Back Market</a>. With little time to spare, you find yourself on the Back Market payment page, impatient to complete your purchase and have it shipped to you. Whether you prefer to pay in installments using Affirm, skip entering your card with PayPal, use your French “<em>carte bancaire</em>”, or even pay with cash at a Japanese convenience store, we’ve got you covered.</p><p>In fact, on Back Market, we have integrated a total of 15 different payment methods across 17 countries into one single form page.</p><p>In this post, I will detail our front-end approach to consolidate those payment methods into a unified form component, with the goal of maintaining simplicity and allowing for future expansion.</p><h3>One single form component</h3><p>As front-end developers in the payment team at Back Market, one of our objectives is to maintain a payment component that can be easily integrated by the team in charge of the whole checkout experience (from the cart page to the confirmation email you receive after purchase). For that, the interface of the component is simple: it takes the purchase amount and the country as input and returns a clear signal of success, cancellation, or error as output.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*ojoSQ5O6uAU_wyAc" /></figure><p>As you can see, we do two things with this component: Show you the list of payment methods and enable you to pay with the chosen payment methods.</p><h4>Step 1: List the available payment methods</h4><p>The initial task of the payment component is to obtain the list of payment methods that are available. This is done via a web service that determines the availability based on contextual parameters, such as the current country, the seller’s capabilities, and the purchase amount.</p><p>In addition to this first filter, a second one allows us to conduct A/B testing experiments or a progressive rollout. This allows us to enable, disable, or re-order payment methods for a predetermined percentage of our audience. Therefore, we can easily compare the performances of different methods or introduce a new payment method to a small percentage of users.</p><p>Finally, we must filter the retrieved payment methods based on the capabilities of the user’s device. For example, if you are using an Android or an iOS device without Apple Pay configured, you will not see the Apple Pay option.</p><figure><img alt="A flowchart summarizing the 3 filters: Filter methods unavailable for this cart; Filter out methods that are affected by AB tests or a progressive rollout; Filter out methods unavailable for this device" src="https://cdn-images-1.medium.com/max/1024/0*xIv-MOeAHHT7G1uR" /><figcaption>Process for determining the list of payment methods to display within the payment form.</figcaption></figure><h4>Step 2: Choose and pay</h4><p>This is the form part of the payment component. Once the user selects a payment method they will be presented with a UI specific to the method and they will be able to proceed by validating the form.</p><p>To ensure simplicity, all the specific logic for different payment methods is hidden behind a common interface, which follows the <a href="https://refactoring.guru/design-patterns/adapter">adapter design pattern</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*nzJqD_VNsw9OeGwd" /></figure><p>In fact, an adapter is essentially an HTML form with a specific submit handler. We use a small HTML trick to render a common submit button outside of the adapter (you can find more information about the button “form” attributes in <a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attributes">The Button element — HTML: HyperText Markup Language | MDN</a>).</p><p>Here is a simplified code version of our “PaymentForm” component:</p><pre>// Payment form Vue template <br>&lt;component<br>    :is=&quot;adapter&quot;<br>    :formId=&quot;formId&quot;<br>    :amount=&quot;amount&quot;<br>    :country=&quot;country&quot;<br>    @error=&quot;onError&quot;<br>    @success=&quot;onSuccess&quot;<br>/&gt;<br><br>&lt;button :form=&quot;formId&quot;&gt;Confirm and pay&lt;/button&gt;<br><br>// Payment form Vue script <br>adapter() {<br>    switch(selectedPaymentMethod) {<br>        case &#39;card&#39;:<br>            return CardAdapter<br>        case &#39;paypal&#39;:<br>            return PaypalAdapter<br>        ...<br>    }<br>}</pre><p>As you may have noticed, the same ‘formId’ is used regardless of the selected payment method. Below, you can see what it looks like on the card adapter’s side, where the actual submission logic is located.</p><pre>// Credit card adapter<br><br>&lt;form onSubmit=&quot;handleSubmit&quot;:id=&quot;formId&quot;&gt;<br>    &lt;input type=&quot;text&quot; name=&quot;cc-number&quot; /&gt;<br>    &lt;input type=&quot;text&quot; name=&quot;cc-exp&quot; /&gt;<br>    ...<br>&lt;/form&gt;<br><br>//... <br><br>async handleSubmit() {<br>    try {<br>        await submitCreditCard()<br>        this.$emit(&#39;success&#39;)<br>    } catch (e) {<br>        this.$emit(&#39;error&#39;, e)<br>    }<br>}</pre><p>This modular design allows us to bundle each payment method’s code separately and serve only the available payment methods to the client. This approach brings performance benefits since certain methods may need to load their own libraries, which can be heavy in some cases.</p><p>One direct consequence of this design is that we have one common form component for all countries where Back Market is used. When a country needs a local payment method, we just need to develop a new adapter and configure it without worrying about the global bundle size of the component.</p><p>Another advantage of our modular design is that it enables us to independently test each payment method. This means that we can identify and fix any issues with a specific payment method without affecting the other methods. This is important because it allows us to maintain the highest level of reliability for our payment system.</p><h3>One shared library</h3><p>Now that we have an understanding of how the payment form works, let’s dive into what we are using to create each of the 15 payment methods. Those methods are obviously different. For example, you don’t pay with PayPal with the same interface as a credit card. And for the same reason, the technical underlying of those methods contains a lot of differences. Nevertheless, we do employ a few shared bricks to construct the payment experience.</p><p>For that, we built a “payment library” that regroups all the utilities we need as a front-end developer to create/maintain the different methods adapter. Here is a quick overview of the key components of our shared payment library:</p><ul><li><strong>Third-party library loader</strong>: simplify the loading of methods with heavy dependencies, including fetch, retry, and error management</li><li><strong>Fraud prevention</strong>: assesses the risk of fraud on the front end side using mainly browser fingerprint computation</li><li><strong>Testing utilities</strong>: ensures the reliability of the payment system</li><li><strong>Monitoring and alerting</strong>: unifies the logs sent by each method adapter</li><li><strong>Redirection utilities</strong>: handles 3DS redirection or external payment flows</li></ul><p>Now, let’s dive deeper into three of these components: testing, monitoring, and redirection.</p><p><strong>Testing utilities</strong> play a crucial role in ensuring the reliability of our payment system. We deliver three different types of tests:</p><ul><li><strong>Unit tests</strong> for each Vue component and function to ensure that each unit of the system is functioning correctly</li><li><strong>Integration tests</strong> to check that each payment method adapter works as a whole</li><li><strong>End-to-end tests</strong> to validate the system’s reliability in real-world scenarios, mimicking actual user behavior</li></ul><p>To this end, the payment library contains shared mocks and testing tools to simulate the behavior of external systems and services, ensuring that the payment system can handle different scenarios and conditions. As a result, for every third-party library that we use, we have developed a mock version of it. For instance, here is part of the mock for the Apple Pay library:</p><pre>const USER_EVENTS = {<br>  AUTHORIZE: &#39;AUTHORIZE&#39;,<br>  CANCEL: &#39;CANCEL&#39;,<br>}<br><br>const controllerBus = mitt()<br><br>export class ApplePaySessionMock {<br>  static STATUS_SUCCESS = 0<br>  static STATUS_FAILURE = 1<br><br>  static canMakePaymentsWithActiveCard = jest.fn()<br>  static constructorSpy = jest.fn()<br><br>  static install() {<br>    this.canMakePaymentsWithActiveCard.mockClear()<br>    this.canMakePaymentsWithActiveCard.mockResolvedValue(true)<br>    this.constructorSpy.mockClear()<br>    this.constructorSpy.mockImplementation(() =&gt; {})<br>    window.ApplePaySession = this<br>  }<br><br>  static uninstall() {<br>    delete window.ApplePaySession<br>    this.canMakePaymentsWithActiveCard.mockClear()<br>    this.constructorSpy.mockClear()<br>    Object.values(USER_EVENTS).forEach((eventType) =&gt;<br>      controllerBus.off(eventType),<br>    )<br>  }<br><br>  static async triggerUserAuthorize(<br>    authorizedEvent = applePayAuthorizedEventBuilder(),<br>  ) {<br>    controllerBus.emit(USER_EVENTS.AUTHORIZE, authorizedEvent)<br>    await flushPromises()<br>  }<br><br>  static async triggerUserCancel() {<br>    controllerBus.emit(USER_EVENTS.CANCEL)<br>    await flushPromises()<br>  }<br><br>  constructor(...args) {<br>    this.constructor.constructorSpy(...args)<br>    this.oncancel = () =&gt; {}<br>    this.onpaymentauthorized = () =&gt; {}<br><br>    controllerBus.on(USER_EVENTS.AUTHORIZE, (event) =&gt; {<br>      this.onpaymentauthorized(event)<br>    })<br><br>    controllerBus.on(USER_EVENTS.CANCEL, () =&gt; {<br>      this.oncancel()<br>    })<br>  }<br><br>  begin() {<br>    window.setTimeout(() =&gt; {<br>      this.onvalidatemerchant()<br>    }, 0)<br>  }<br>}</pre><p>We also provide a suite of tools that enables other teams to conduct integration tests of payment within their utilization flows. The goal is to ensure that the payment form integration is working as expected without delegating the burden of integrating and maintaining a suite of tests to the other team.</p><p>Bellow a simplified usage of our “PaymentTest” utility tools:</p><pre>tconst paymentTest = new PaymentTest(testOptions)<br><br>paymentTest.expect.toBeOnThePaymentPage();<br>paymentTest.expect.toHavePaymentMethod(CARD);<br><br>await paymentTest.form.selectPaymentMethod(CARD);<br>await paymentTest.form.fill(SANDBOX_CARDS.AMERICAN_EXPRESS);<br>await paymentTest.form.submit();<br><br>paymentTest.expect.toBeRedirectedToTheConfirmationPage()<br>paymentTest.expect.toHaveASuccessfulPayment();</pre><p><strong>Redirection utils</strong> is a part of the payment library that handles 3DS redirection or external payment flows hosted by the payment provider. Nothing fancy here. The only specificity is that we must also manage the redirection via the HTTP POST method:</p><pre>type Method = &#39;GET&#39; | &#39;POST&#39;;<br>type PaymentRedirection {<br>  method: Method;<br>  href: string;<br>  data?: Data;<br>}<br><br>export function proceedPaymentRedirection(<br>  { method, href, data }: PaymentRedirection,<br>) {<br>  switch (method) {<br>    case &#39;GET&#39;:<br>      // If method is GET, redirect using the vue router<br>      $navigation.push(href);<br>      break;<br><br>    case &#39;POST&#39;:<br>      // If method is POST, create a form element with hidden input elements for the data,<br>      // add it to the DOM, submit it, and then remove it from the DOM.<br>      processPostRedirection(href, data);<br>      break;<br><br>    default:<br>      throw new Error(`Unknown redirection type: ${method}`);<br>  }<br>}<br><br>function processPostRedirection(href: string, data?: Data): void {<br>  const form = document.createElement(&#39;form&#39;);<br>  form.method = METHODS.POST;<br>  form.action = href;<br><br>  Object.entries(data || {}).forEach(([name, value]) =&gt; {<br>    const input = document.createElement(&#39;input&#39;);<br>    input.type = &#39;hidden&#39;;<br>    input.name = name;<br>    input.value = String(value);<br>    form.appendChild(input);<br>  });<br><br>  document.body.appendChild(form);<br>  form.submit();<br>  form.remove();<br>}</pre><p><strong>Monitoring and alerting</strong> require careful consideration when it comes to logging specific events. While it’s important to track particular events from payment methods, our primary goal is to gain a comprehensive overview of how our payment system is performing overall. To achieve this, we have a unified logging scheme that goes beyond the differences between individual methods.</p><p>For example, we force ourselves to use the following event types, no matter the method:</p><pre>&quot;FORM_SUBMIT_START&quot; <br>&quot;FORM_SUBMIT_SUCCESS&quot; <br>&quot;FORM_SUBMIT_CANCEL&quot; <br>&quot;FORM_SUBMIT_ERROR&quot;<br>&quot;RESULT_SUCCESS&quot;<br>&quot;RESULT_FAILURE&quot;</pre><p>Obviously, the content of the logs may differ, but thanks to the log harmonization, we can have a unified dashboard that can be easily adapted to include new payment methods.</p><figure><img alt="An extract of our form dashboard, each column is based on one event type" src="https://cdn-images-1.medium.com/max/1024/0*TlAnN3BQdkoJTDSn" /><figcaption>An extract of our form dashboard, each column is based on one event type</figcaption></figure><h3>Conclusion</h3><p>In conclusion, Back Market’s front-end approach consolidating different payment methods into a unified form component result from a modular design and a shared payment library.</p><p>This approach not only allows for easy integration of the payment component by other teams, but it also simplifies the process of adding new payment methods to the form. By using a common interface, the specific logic for each payment method is hidden behind an adapter, which follows the adapter design pattern. This design enables testing of each payment method independently and ensures the highest level of reliability for Back Market’s payment system.</p><p>As we integrate new payment methods, we are actively maintaining a payment library with the goal of making each adapter as generic as possible. Testing and monitoring take a huge place in this library as we want to ensure the good health of the payment system. We are also excited about expanding the library in the future, such as using the recently introduced <a href="https://www.w3.org/TR/payment-request/">Payment Request API</a>.</p><p>Thank you for taking the time to read this post! Don’t hesitate to leave a comment 💬 or clap👏 if you liked it!</p><p><strong>If you want to join our Bureau of Technology or any other Back Market department, </strong><a href="https://jobs.lever.co/backmarket"><strong>take a look here, we’re hiring</strong></a><strong>! 🦄 — </strong><a href="http://www.backmarket.com/">www.backmarket.com</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e34b1a7cf044" width="1" height="1" alt=""><hr><p><a href="https://engineering.backmarket.com/payment-at-back-market-one-form-to-rule-them-all-e34b1a7cf044">Payment at Back Market: one form to rule them all</a> was originally published in <a href="https://engineering.backmarket.com/">Back Market Blog</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>