0.5.34 • Published 3 months ago

mainstack-payments v0.5.34

Weekly downloads
-
License
ISC
Repository
-
Last release
3 months ago

Installation

To install the Mainstack Payments library, you need to run the following command in your terminal:

yarn add mainstack-payments

Usage

To use the Mainstack Payments library, you need to import the library into your project, and then pass a config object to paymentConfig prop.

import { MainstackPayments } from "mainstack-payments";

export default function Home() {
  const config = {};
  return (
    <Box>
      <MainstackPayment paymentConfig={config} />
    </Box>
  );
}

You might also want to import the MainstackPaymentsProvider in your root file if you see some styling on elements not looking appropraite.

const MainstackPaymentsProvider = dynamic(
  () =>
    import("mainstack-payments").then((mod) => mod.MainstackPaymentsProvider),
  {
    ssr: false,
  }
);

function MyApp({ Component, pageProps }: AppProps) {
  return (
     <ChakraProvider theme={theme}>
          <MainstackPaymentsProvider>
          </MainstackPaymentsProvider>
     </ChakraProvider>
  )
}

The config object should contain the following properties:

  • amount*: The amount for the product to be paid

  • accountId*: The account ID of the user making the payment

  • currency*: The currency to be used for the payment

  • transactionFeesSlug*: The slug of the transaction fees to be applied to the payment usually determined by the backend, e.g 'store-pro-plan'

  • userAllowsWalletPayment*: A boolean value that states if the merchant has allowed wallet payment

  • userAllowsCardPayment*: A boolean value that states if the merchant has allowed card payment

  • userAllowsCryptoPayment*: A boolean value that states if the merchant has allowed crypto wallet payments.

  • metadata*: An object that contains additional information about the payment. See more on this here

  • baseUrl*: The base URL of the legacy API. This API is what is used primarily to make payments. It is used initialise, charge and verify payment.

  • hasDiscountCode: A optional boolean to show the discount code input. Default is false.

  • mainstackProductId: A User's ID for a particular product, e.g store_id, bookings_id etc.

  • STRIPE_SECRET_KEY: Stripe secret key for stripe payments, usually stored in an env.

  • STRIPE_SECRET_KEY_GBP: Stripe secret key for stripe payments in GBP, usually stored in an env.

  • applyTrancheFee: Boolean that is passed to the transaction fees endpoint and dictates if a tranch fee should be applied for transaction fees.This is specifically for Pay In Tranches payments. Default is false.

  • shouldApplyTax: A boolean that determines if tax fees should be calculated and collected. Default is false.

  • ip: This is for the user's ip address to determine their tax fee. Only needed if you want to collect tax fees.

  • paymentOptions: An array of payment options that should be enabled for the user can select from. The options are 'paystack', 'startbutton', 'wallet', 'stripe', 'crypto'

  • paymentRedirectUrl: The URL to redirect to after a successful payment with Paystack, Startbutton or Stripe UK payments (which may require 3DS). More on this can be found here

  • cryptoRedirectUrl: The URL to redirect to after a successful crypto wallet payment. This is different to paymentRedirectUrl as you are fully in control of this page and should ideally be the receipt page. The following url query parameters will be added to the url from the payment library: reference : payment reference generated by mainstack, payment_intent : generated by stripe. This is required if userAllowsCryptoPayment is true.

  • customizations: This is an object that takes values that adjust layout, styling or display of elements on the library. See full description here.

  • paymentModel: string that specifies if the payment model is 'ips' or 'default'

metadata

The metadata object should contain the following properties:

  • -productName: The name of the product being paid for

  • -type: The type of product being paid for

  • -account_id: The account ID of the user making the payment

  • -user_id: The user ID of the user making the payment

  • -product_id: The product ID of the product being paid for

  • -mainstack_product_type: What Category of Mainstack Product is being paid for, accepted values are 'store', 'editor', 'bookings', 'mediakit', 'invoicing', 'hosted_courses'

  • [key:string] : Any other key-value pair that you want to add to the metadata object

Customizations

  • theme_color: custom color the the Pay button

  • button_label: custom label for the Pay button

  • font_family: To set the font family for all the payment library texts and inputs

  • hideDetails: Boolean that hide the summary details section of the payment library. Default is false.

  • hideBackButton : Boolean to remove the back button that is next to the Pay button.

  • isSingleColumn : This converts the payment library layout from 2 columns to a single column. Not that this happens by default on mobile screens.

  • padding : This can be used to adjust the surrounding padding of the payment library.

  • showDefaultInputFields This is an object used to determine what default input fields will be shown on the checkout form. They include:

    • email : For the Email input. Default is true.

    • fullname: For the Name input. Default is true.

    • phone: For the PhoneNumber input. Default is false.

The paymentRedirectUrl is the URL to redirect to after a successful payment with Paystack, Startbutton or Stripe UK. This page will be created by you and should contain the following code:

import { useEffect } from "react";

const PostMessagePage = () => {
  useEffect(() => {
    (() => {
      window.parent.postMessage({ type: "Payment Completed" }, "*");
    })();
  }, []);

  return <div></div>;
};

export default PostMessagePage;

The aim of this is to send a postMessage to the iframe that loads the payment option to let them know that payment is complete and they can close the iframe and verify the payment.

Extra Props

Other Props that can be passed to the MainstackPayments component are:

  • summaryTitle: This takes a string or ReactNode and is used to replace the title of the payment summary section, the default is always the Product name.

  • checkoutTitle: This takes a string or ReactNode and is used to replace the title of the checkout form section, the default is always "Checkout Details"

  • customForm: This takes a ReactNode used to add a custom form to the payment page.

  • defaultFormValues: This is an object with fullname and email keys. Their values are passed to their respective inputs as default values.

  • onPaymentComplete: This is a callback function that is called when the payment is completed. It contains a payload that will include the amount, default input values, and reference of the payment, as well as some other optional data.

  • onGoBack: This is a callback function that is called when the user clicks the back button on the payment page.

  • onInitializePayment: This is an async function is called just as the payment is about to be initialized and is useful for triggering any actions before the payment is initialized, like validating your custom form or making an api call to validate something with the user's name or email before proceeding with payment. It contains an argument that has the values for the default form inputs, and must return a promise that resolves an object with a terminate boolean property. The payment initialization will terminate immediately the promise resolves depending on what the value of terminate is.

      <MainstackPayments
              paymentConfig={config}
              onGoBack={goBack}
              onPaymentComplete={(e) => onPaymentSuccess(e)}
              customForm={
                <>{customForm}</>
              }
              onInitializePayment={onInitPayment}
       />

Happy Hacking !!!

0.5.11

7 months ago

0.5.18

6 months ago

0.5.19

6 months ago

0.5.16

7 months ago

0.5.17

6 months ago

0.5.14

7 months ago

0.5.15

7 months ago

0.5.12

7 months ago

0.5.13

7 months ago

0.5.32

3 months ago

0.5.33

3 months ago

0.5.30

4 months ago

0.5.31

4 months ago

0.5.34

3 months ago

0.5.21

5 months ago

0.5.22

5 months ago

0.5.20

5 months ago

0.5.29

4 months ago

0.5.27

4 months ago

0.5.28

4 months ago

0.5.25

4 months ago

0.5.26

4 months ago

0.5.23

5 months ago

0.5.24

4 months ago

0.5.10

8 months ago

0.5.9

8 months ago

0.5.8

8 months ago

0.5.7

8 months ago

0.4.9

8 months ago

0.4.8

8 months ago

0.5.4

8 months ago

0.3.6

9 months ago

0.5.3

8 months ago

0.3.5

9 months ago

0.5.6

8 months ago

0.3.8

9 months ago

0.5.5

8 months ago

0.3.7

9 months ago

0.5.0

8 months ago

0.5.2

8 months ago

0.3.4

9 months ago

0.5.1

8 months ago

0.3.3

10 months ago

0.3.9

9 months ago

0.4.5

9 months ago

0.4.4

9 months ago

0.4.7

9 months ago

0.4.6

9 months ago

0.4.1

9 months ago

0.4.0

9 months ago

0.4.3

9 months ago

0.4.2

9 months ago

0.3.2

10 months ago

0.3.1

10 months ago

0.3.0

10 months ago

0.2.9

10 months ago

0.2.8

10 months ago

0.2.7

10 months ago

0.2.6

11 months ago

0.2.5

11 months ago

0.2.4

11 months ago

0.2.3

11 months ago

0.2.2

11 months ago

0.2.1

11 months ago

0.2.0

11 months ago

0.1.9

11 months ago

0.1.8

11 months ago

0.1.7

11 months ago

0.1.6

11 months ago

0.1.5

11 months ago

0.1.4

11 months ago

0.1.3

11 months ago

0.1.2

11 months ago

0.1.1

11 months ago

0.1.0

11 months ago

0.0.9

11 months ago

0.0.8

11 months ago

0.0.7

11 months ago

0.0.6

11 months ago

0.0.5

11 months ago

0.0.4

11 months ago

0.0.3

11 months ago

0.0.2

12 months ago

0.0.1

12 months ago

0.0.0

12 months ago