Skip to content

Commit

Permalink
Merge pull request #22 from mediaopt/add-payment-source-to-create-order
Browse files Browse the repository at this point in the history
added payment source to create order call
  • Loading branch information
JonYeb authored Oct 27, 2023
2 parents 4aaa9c8 + ee81af7 commit 6e66c1c
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "paypal-commercetools-client",
"version": "0.0.46",
"version": "0.0.47",
"private": false,
"type": "module",
"license": "MIT",
Expand Down
10 changes: 6 additions & 4 deletions src/app/usePayment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
ClientTokenResponse,
CustomOnApproveData,
OnApproveResponse,
CreateOrderData,
} from "../types";
import { createPayment, createOrder, onApprove } from "../services";

Expand All @@ -42,7 +43,7 @@ type PaymentContextT = {
requestHeader: RequestHeader;
handleCreatePayment: () => void;
clientToken: string;
handleCreateOrder: () => Promise<string>;
handleCreateOrder: (orderData?: CreateOrderData) => Promise<string>;
handleOnApprove: (data: CustomOnApproveData) => Promise<void>;
};

Expand All @@ -52,7 +53,7 @@ const PaymentContext = createContext<PaymentContextT>({
requestHeader: {},
handleCreatePayment: () => {},
clientToken: "",
handleCreateOrder: () => Promise.resolve(""),
handleCreateOrder: (orderData?: CreateOrderData) => Promise.resolve(""),
handleOnApprove: () => Promise.resolve(),
});

Expand Down Expand Up @@ -101,14 +102,15 @@ export const PaymentProvider: FC<
setResultMessage("Test success successful");
};

const handleCreateOrder = async () => {
const handleCreateOrder = async (orderData?: CreateOrderData) => {
if (!createOrderUrl) return "";

const createOrderResult = await createOrder(
requestHeader,
createOrderUrl,
paymentInfo.id,
paymentInfo.version
paymentInfo.version,
orderData
);

if (createOrderResult) {
Expand Down
5 changes: 4 additions & 1 deletion src/services/createOrder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,21 @@ import {
RequestHeader,
CreateOrderRequest,
CreateOrderResponse,
CreateOrderData,
} from "../types";

export const createOrder = async (
requestHeader: RequestHeader,
url: string,
paymentId: string,
paymentVersion: number
paymentVersion: number,
orderData?: CreateOrderData
) => {
try {
const data: CreateOrderRequest = {
paymentId,
paymentVersion,
orderData,
};

return await makeRequest<CreateOrderResponse, CreateOrderRequest>(
Expand Down
6 changes: 6 additions & 0 deletions src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@ import {
ReactPayPalScriptOptions,
PayPalMessagesComponentProps,
} from "@paypal/react-paypal-js";
import { FUNDING_SOURCE } from "@paypal/paypal-js/types/components/funding-eligibility";

export type CreateOrderRequest = {
paymentId: string;
paymentVersion: number;
orderData?: CreateOrderData;
};

export type CreateOrderData = {
paymentSource: FUNDING_SOURCE;
};

export type CreateOrderResponse = {
Expand Down

0 comments on commit 6e66c1c

Please sign in to comment.