
Security News
npm ‘is’ Package Hijacked in Expanding Supply Chain Attack
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
react-payment-request-api
Advanced tools
React high order component that expose the payment request api
React high order component to drive payment request widget on react applications 💳.
Payment request api is supported on Chrome for desktop v. ^60.0, Chrome for Android and Android Webview v. ^53.0, Microsoft Edge v. ^15.0.
You can find a working demo here. Be sure to use a supported browser.
npm install react-payment-request-api --save
Consume the UI component in the hight order component button.js
:
import React from "react";
import paymentRequest from 'react-payment-request-api';
const Button = ({ show, isSupported }) => isSupported
? <button onClick={show}>Pay now!</button>
: <span>Payment request not supported</span>;
export default paymentRequest<OwnProps>()(Button);
Pass the configuration to the high order component smartComponent.js
:
import React from "react";
import Button from "./button";
const SmartComponent = (config) =>
<Button config={config} foo="bar" />;
export default SmartComponent;
It takes a configuration prop that define how the native widget should behave and any other property you want to pass to the UI component. It spread all the properties a part from the configuration to the enhanced UI component. The UI component will also receive other props that will help improving the experience allowing complete control on the renderer and on the action handler.
Chrome v. 53.0 - 55.00 was implementing a draft of the standard for Payment Request. Since then, there have been some evolutions on how to define paymentMethods
in the configuration. Specifically, the payment methos should now be defined as:
{
supportedMethods: ['basic-card'],
data: {
supportedNetworks: ['visa', 'mastercard'],
},
}
The library de-normalize the configuration to be able to support the old standard that looks like:
{
supportedMethods: ['visa', 'mastercard']
}
Yes, with version 1.0 we have changed the interface allowing the user to inject the configuration from the parent component. In this way it does not matter which flux implementation you are using. At the same time, we are preserving the high order component pattern so you have complete control on the renderer and on the action handler.
Yes, you don't need to install any typescript dependecies because types come with the library. It export PaymentRequestParams
(injected configuration) and PaymentRequestInterface
(UI component extended props) typescript interfaces. All the examples are written in typescript.
Your wrapped component will be decorated with this injected props:
Parameter | Type | Description |
---|---|---|
isSupported | boolean | True if the payment request api is supported by the browser. |
show | function: () => PaymentRequest | It will begin the process when the merchant site want to create a new PaymentRequest. |
abort | function: () => void | You can intentionally abort a PaymentRequest by calling the abort method after show has been called. |
Configuration of the high order component:
Parameter | Type | Description |
---|---|---|
methodData | object | Required payment method data. |
details | object | Required information about transaction. |
options | object | Optional parameter for things like shipping, etc. |
onShowSuccess | Promise based callback: (result, resolve, reject) | The handler will be executed after the filling of the form is successfull. You should post your payment request and then resolve or reject the promise. |
onShowFail | Promise based callback: (error) | The handler will be executed if the filling of the form is not successfull (like when the user dismiss the form). |
onShippingAddressChange | Promise based callback: (request, resolve, reject) | The handler will be executed if the shipping address has change. You can change the request and then resolve the promise. |
onShippingOptionChange | Promise based callback: (request, resolve, reject) | The handler will be executed if the shipping option has change. You can change the request and then resolve the promise. |
See the LICENSE file for license rights and limitations (MIT).
FAQs
React high order component that expose the payment request api
The npm package react-payment-request-api receives a total of 22 weekly downloads. As such, react-payment-request-api popularity was classified as not popular.
We found that react-payment-request-api demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The ongoing npm phishing campaign escalates as attackers hijack the popular 'is' package, embedding malware in multiple versions.
Security News
A critical flaw in the popular npm form-data package could allow HTTP parameter pollution, affecting millions of projects until patched versions are adopted.
Security News
Bun 1.2.19 introduces isolated installs for smoother monorepo workflows, along with performance boosts, new tooling, and key compatibility fixes.