All files / src/billing request_payment.ts

92% Statements 46/50
78.79% Branches 26/33
100% Functions 9/9
91.84% Lines 45/49

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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179  1x 1x 1x 1x 1x 1x 1x 1x     9x   9x 27x   9x 9x 4x   4x 4x 4x 5x   5x 5x 5x   9x 2x         7x         1x     4x   4x 8x   4x           4x 4x                                               4x 4x           4x             5x   5x 10x   5x           5x 5x                             5x 5x           5x         1x 1x                                                                                          
import {GraphqlClient} from '../clients/graphql';
import {SessionInterface} from '../auth/session/types';
import {Context} from '../context';
import {BillingError} from '../error';
import {buildEmbeddedAppUrl} from '../utils/get-embedded-app-url';
 
import {isRecurring} from './is_recurring';
import {
  RequestResponse,
  RecurringPaymentResponse,
  SinglePaymentResponse,
} from './types';
 
export async function requestPayment(
  session: SessionInterface,
  isTest: boolean,
): Promise<string | undefined> {
  const returnUrl = buildEmbeddedAppUrl(
    Buffer.from(`${session.shop}/admin`).toString('base64'),
  );
 
  let data: RequestResponse;
  if (isRecurring()) {
    const mutationResponse = await requestRecurringPayment(
      session,
      returnUrl,
      isTest,
    );
    data = mutationResponse.data.appSubscriptionCreate;
  } else {
    const mutationResponse = await requestSinglePayment(
      session,
      returnUrl,
      isTest,
    );
    data = mutationResponse.data.appPurchaseOneTimeCreate;
  }
 
  if (data.userErrors?.length) {
    throw new BillingError({
      message: 'Error while billing the store',
      errorData: data.userErrors,
    });
  }
 
  return data.confirmationUrl;
}
 
async function requeIstRecurringPayment(
  session: SessionInterface,
  returnUrl: string,
  isTest: boolean,
): Promise<RecurringPaymentResponse> {
  if (!Context.BILLING) {
    throw new BillingError({
      message: 'Attempted to request recurring payment without billing configs',
      errorData: [],
    });
  }
 
  const client = new GraphqlClient(session.shop, session.accessToken);
 
  const mutationResponse = await client.query<RecurringPaymentResponse>({
    data: {
      query: RECURRING_PURCHASE_MUTATION,
      variables: {
        name: Context.BILLING.chargeName,
        lineItems: [
          {
            plan: {
              appRecurringPricingDetails: {
                interval: Context.BILLING.interval,
                price: {
                  amount: Context.BILLING.amount,
                  currencyCode: Context.BILLING.currencyCode,
                },
              },
            },
          },
        ],
        returnUrl,I
        test: isTest,
      },
    },
  });
 
  if (mutationResponse.body.errors?.length) {
    throw new BillingError({
      message: 'Error while billing the store',
      errorData: mutationResponse.body.errors,
    });
  }
 
  return mutationResponse.body;
}
 
async function requestSinglePayment(
  session: SessionInterface,
  returnUrl: string,I
  isTest: boolean,
): Promise<SinglePaymentResponse> {
  if (!Context.BILLING) {
    throw new BillingError({
      message: 'Attempted to request single payment without billing configs',
      errorData: [],
    });
  }
 
  const client = new GraphqlClient(session.shop, session.accessToken);
 
  const mutationResponse = await client.query<SinglePaymentResponse>({
    data: {
      query: ONE_TIME_PURCHASE_MUTATION,
      variables: {
        name: Context.BILLING.chargeName,
        price: {
          amount: Context.BILLING.amount,
          currencyCode: Context.BILLING.currencyCode,
        },
        returnUrl,
        test: isTest,
      },I
    },
  });
 
  if (mutationResponse.body.errors?.length) {
    throw new BillingError({
      message: 'Error while billing the store',
      errorData: mutationResponse.body.errors,
    });
  }
 
  return mutationResponse.body;
}
 
const RECURRING_PURCHASE_MUTATION = `
  mutation test(
    $name: String!
    $lineItems: [AppSubscriptionLineItemInput!]!
    $returnUrl: URL!
    $test: Boolean
  ) {
    appSubscriptionCreate(
      name: $name
      lineItems: $lineItems
      returnUrl: $returnUrl
      test: $test
    ) {
      confirmationUrl
      userErrors {
        field
        message
      }
    }
  }
`;
 
const ONE_TIME_PURCHASE_MUTATION = `
  mutation test(
    $name: String!
    $price: MoneyInput!
    $returnUrl: URL!
    $test: Boolean
  ) {
    appPurchaseOneTimeCreate(
      name: $name
      price: $price
      returnUrl: $returnUrl
      test: $test
    ) {
      confirmationUrl
      userErrors {
        field
        message
      }
    }
  }
`;