All files / src/services/api/checkout gateway.ts

100% Statements 6/6
50% Branches 1/2
100% Functions 3/3
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79                                                                                            1x           3x     3x               1x 109x                   3x        
import axios from "axios";
import { useMutation } from "react-query";
import { TypeCheckoutInfo } from "@pages/MerchantCheckout/types";
 
export type GatewayPayment = {
  amount: number; // Required, Amount in USD cents (e.g., $1,500.00), MAX 2500000
  paymethod: {
    // "id": "GS_1GyQkCA6hH3UEq", // Optional,
    //if already have an existing payment method, need to start with GS_
    // if does not have, fill the below parameter to create a new payment method
    card: {
      name: string;
      number: string;
      cvv: string;
      exp_year: number;
      exp_month: number;
    };
    billing_address: {
      line1: string;
      line2: string;
      city: string;
      state: string;
      zip: string;
      country: string;
    };
  };
  customer: {
    //"id": "GS_98765zyxwv", // If eixsting customer
    // If its new customer, please use this params below
    email: string;
    phone: string;
    first_name: string;
    last_name: string;
    company_name?: string;
  };
  // There's 2 possible values for this field
  // customer or merchant
  // Specify which party should cover the service fee.
  // If left unspecified then `merchant` will be used.
  fee_payer: "merchant";
  // When set to `true`, the API will send a reciept
  // to the customer's email address.
  send_receipt: boolean;
  external_reference: string;
};
 
const gatewayPayment = (
  payment: GatewayPayment,
  environment: Exclude<TypeCheckoutInfo["environment"], "">,
  headers?: { [key: string]: string },
) => {
  const url =
    environment === "production"
      ? process.env.VITE_DEVELOPER_API_URL
      : process.env.VITE_SANDBOX_API_URL;
  return axios({
    url: `${url}/payments`,
    method: "POST",
    data: payment,
    headers: headers,
  });
};
 
export const gatewayPaymentMutation = () => {
  return useMutation(
    ({
      payment,
      headers,
      environment,
    }: {
      payment: GatewayPayment;
      headers?: { [key: string]: string };
      environment: Exclude<TypeCheckoutInfo["environment"], "">;
    }) => {
      return gatewayPayment(payment, environment, headers);
    },
  );
};