Skip to content

Payments

Sammwy edited this page Nov 29, 2021 · 2 revisions

Here is a guide on how to create and process payments.

Item Scheme

When making a transaction, the items must have the following format:

{
  // Unique product identificator (optional)
  "sku": String,
  // Product name
  "name": String,
  // Product price (i.e 25.00)
  "price": String,
  // Purchase currency (i.e USD)
  "currency": String,
  // Quantity of the same item to buy
  "quantity": Number
}

Payment Scheme

When creating a new payment order, the following scheme must be followed:

{
  // Payment method (i.e paypal, stripe, etc)
  "method": String,
  // Payment intent (i.e sale)
  "intent": String,
  // Purchase description
  "description": String,
  // Item array
  "items": IItem[],
  // Url where the user will be sent after purchase
  "return_url": String,
  // Url where the user will be sent if the purchase fails or is canceled
  "cancel_url": String
}

Create payment

When creating a purchase it will return an object similar to the payment schema with the exception that now we will have 3 new values (status, id and link)

const payment = await payflux.createPayment({...payment scheme});
console.log(payment.url);

Once we generate the payment, we must send the user to the link generated to make the transaction. After that, the user will be sent to the return link with query parameters that have useful information about the transaction.

Execute payment

To confirm if a purchase is successful we can execute the payment and obtain the status value.

To do this we must give the executePayment function an object that contains the payment method and the query parameters obtained from the redirect url.

const confirm = await payflux.executePayment({
    method: 'paypal',
    query: {
      // Obtained from: http://{redirect_url}/?paymentId=...&payerId=...
      paymentId: '',
      payerId: '',
    },
  });

This function will return an object of type Payment (specified above)

Clone this wiki locally