Skip to content

Commit

Permalink
Remove client order create (#8)
Browse files Browse the repository at this point in the history
* chore: add bncode to headers in orders api calls

* chore: prettier formatting

* chore: remove unused client-side order creation

* chore: cleanup unused vars

---------

Co-authored-by: Jennifer <jscheinhorn@paypal.com>
  • Loading branch information
jscheinhorn and jscheinhorn committed Mar 27, 2023
1 parent 7f919ac commit 188e31d
Show file tree
Hide file tree
Showing 5 changed files with 3 additions and 185 deletions.
24 changes: 0 additions & 24 deletions src/__tests__/applepay.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,6 @@ global.btoa = btoa;
global.atob = atob;

describe("applepay", () => {
describe("Order", () => {
it("Creates Order", async () => {
const applepay = Applepay();

const { id, status } = await applepay.createOrder({
intent: "CAPTURE",
purchase_units: [
{
amount: {
currency_code: "USD",
value: "1.00",
},
payee: {
merchant_id: "2V9L63AM2BYKC",
},
},
],
});

expect(id).toBeTruthy();
expect(status).toBe("CREATED");
});
});

describe("Config", () => {
it("GetAppelPayConfig", async () => {
const applepay = Applepay();
Expand Down
68 changes: 1 addition & 67 deletions src/__tests__/util.test.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
/* @flow */

import {
getMerchantDomain,
getCurrency,
getCreateOrderPayLoad,
mapGetConfigResponse,
} from "../util";
import { getMerchantDomain, getCurrency, mapGetConfigResponse } from "../util";

global.window = Object.create(window);

Expand Down Expand Up @@ -44,67 +39,6 @@ describe("util", () => {
});
});

describe("getCreateOrderPayLoad", () => {
it("should create order with capture Intent and default payee Information", () => {
const requestPayLoad = {
purchase_units: [
{
amount: {
currency_code: "USD",
value: "0.99",
},
},
],
};
expect(getCreateOrderPayLoad(requestPayLoad)).toStrictEqual({
intent: "CAPTURE",
purchase_units: [
{
amount: {
currency_code: "USD",
value: "0.99",
},
payee: {
merchant_id: "2V9L63AM2BYKC",
},
},
],
payer: undefined,
application_context: undefined,
});
});

it("should create order with passed in information", () => {
const requestPayLoad = {
intent: "AUTHORIZE",
purchase_units: [
{
amount: {
currency_code: "USD",
value: "0.99",
},
},
],
};
expect(getCreateOrderPayLoad(requestPayLoad)).toStrictEqual({
intent: "AUTHORIZE",
purchase_units: [
{
amount: {
currency_code: "USD",
value: "0.99",
},
payee: {
merchant_id: "2V9L63AM2BYKC",
},
},
],
payer: undefined,
application_context: undefined,
});
});
});

describe("mapGetConfigResponse", () => {
it("should return config data mapped as per client api spec", () => {
const requestPayLoad = {
Expand Down
56 changes: 0 additions & 56 deletions src/applepay.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
getLogger,
getBuyerCountry,
getPayPalDomain,
getPayPalAPIDomain,
getPartnerAttributionID,
} from "@paypal/sdk-client/src";
import { FPTI_KEY } from "@paypal/sdk-constants/src";
Expand All @@ -18,8 +17,6 @@ import {
} from "./util";
import type {
ConfigResponse,
CreateOrderResponse,
OrderPayload,
ValidateMerchantParams,
ApplepayType,
ConfirmOrderParams,
Expand All @@ -29,62 +26,10 @@ import type {
import {
FPTI_TRANSITION,
FPTI_CUSTOM_KEY,
DEFAULT_API_HEADERS,
DEFAULT_GQL_HEADERS,
} from "./constants";
import { logApplePayEvent } from "./logging";

async function createOrder(
payload: OrderPayload
): Promise<CreateOrderResponse> {
const basicAuth = btoa(`${getClientID()}`);
const partnerAttributionId = getPartnerAttributionID();

try {
const accessToken = await fetch(`${getPayPalAPIDomain()}/v1/oauth2/token`, {
method: "POST",
headers: {
Authorization: `Basic ${basicAuth}`,
"PayPal-Partner-Attribution-Id": partnerAttributionId || "",
},
body: "grant_type=client_credentials",
})
.then((res) => {
return res.json();
})
.then(({ access_token }) => {
return access_token;
});

const res = await fetch(`${getPayPalAPIDomain()}/v2/checkout/orders`, {
method: "POST",
headers: {
...DEFAULT_API_HEADERS,
Authorization: `Bearer ${accessToken}`,
},
body: JSON.stringify(payload),
}).catch((err) => {
throw err;
});

const { id, status } = await res.json();

return {
id,
status,
};
} catch (error) {
getLogger()
.error(FPTI_TRANSITION.APPLEPAY_CREATE_ORDER_ERROR)
.track({
[FPTI_KEY.TRANSITION]: FPTI_TRANSITION.APPLEPAY_CREATE_ORDER_ERROR,
[FPTI_CUSTOM_KEY.ERR_DESC]: `Error: ${error.message}) }`,
})
.flush();
throw error;
}
}

function config(): Promise<ConfigResponse | PayPalApplePayErrorType> {
return fetch(`${getPayPalDomain()}/graphql?GetApplepayConfig`, {
method: "POST",
Expand Down Expand Up @@ -341,7 +286,6 @@ function confirmOrder({

export function Applepay(): ApplepayType {
return {
createOrder,
config,
validateMerchant,
confirmOrder,
Expand Down
6 changes: 0 additions & 6 deletions src/types.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,6 @@ export type GQLConfigResponse = {|
supportedNetworks: $ReadOnlyArray<string>,
|};

export type CreateOrderResponse = {|
id: string,
status: string,
|};

export type ApplePaySession = {|
displayName: string,
domainName: string,
Expand Down Expand Up @@ -128,7 +123,6 @@ export type ValidateMerchantResponse = {|
|};

export type ApplepayType = {|
createOrder(OrderPayload): Promise<CreateOrderResponse>,
config(): Promise<ConfigResponse | PayPalApplePayErrorType>,
validateMerchant(
ValidateMerchantParams
Expand Down
34 changes: 2 additions & 32 deletions src/util.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/* eslint-disable eslint-comments/disable-enable-pair */
/* eslint-disable flowtype/no-weak-types */

/* @flow */
import { getSDKQueryParam, getMerchantID } from "@paypal/sdk-client/src";
import { getSDKQueryParam } from "@paypal/sdk-client/src";

import { ORDER_INTENT } from "./constants";
import type { ConfigResponse, GQLConfigResponse } from "./types";

export function getMerchantDomain(): string {
Expand All @@ -15,35 +14,6 @@ export function getCurrency(): string {
return getSDKQueryParam("currency", "USD");
}

type CreateOrderPayLoad = {|
purchase_units: $ReadOnlyArray<{|
amount: {| currency_code: string, value: string |},
|}>,
intent: string,
payer: any,
application_context: any,
|};

export function getCreateOrderPayLoad(requestPayLoad: any): CreateOrderPayLoad {
const merchant_id = getMerchantID();
let { purchase_units, intent, payer, application_context } = requestPayLoad;
purchase_units = purchase_units.map((purchaseUnit) => {
return {
...purchaseUnit,
payee: merchant_id && {
merchant_id,
},
};
});

return {
purchase_units,
intent: intent || ORDER_INTENT.CAPTURE,
payer,
application_context,
};
}

export function mapGetConfigResponse(
applepayConfig: GQLConfigResponse
): ConfigResponse {
Expand Down

0 comments on commit 188e31d

Please sign in to comment.