Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
refactor: use handlePayment method in checkout (#1511)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkucmus authored May 25, 2021
1 parent 44206c3 commit 0b71b2b
Show file tree
Hide file tree
Showing 16 changed files with 47 additions and 56 deletions.
16 changes: 8 additions & 8 deletions api/shopware-6-client.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -386,22 +386,22 @@ export const getStoreNewsletterSubscribeEndpoint: () => string;
export const getStoreNewsletterUnsubscribeEndpoint: () => string;

// @beta (undocumented)
export function getStoreOrderPaymentUrl(orderId: string, finishUrl?: string, errorUrl?: string, contextInstance?: ShopwareApiInstance): Promise<{
redirectUrl: string | null;
apiAlias: string;
}>;
export const getSuggestSearchEndpoint: () => string;

// @beta (undocumented)
export const getStoreOrderPaymentUrlEndpoint: () => string;
export function getUserCountry(countryId: string, contextInstance?: ShopwareApiInstance): Promise<Country>;

// @beta (undocumented)
export const getSuggestSearchEndpoint: () => string;
export function getUserSalutation(salutationId: string, contextInstance?: ShopwareApiInstance): Promise<Salutation>;

// @beta (undocumented)
export function getUserCountry(countryId: string, contextInstance?: ShopwareApiInstance): Promise<Country>;
export function handlePayment(orderId: string, finishUrl?: string, errorUrl?: string, contextInstance?: ShopwareApiInstance): Promise<{
redirectUrl: string | null;
apiAlias: string;
}>;

// @beta (undocumented)
export function getUserSalutation(salutationId: string, contextInstance?: ShopwareApiInstance): Promise<Salutation>;
export const handlePaymentEndpoint: () => string;

// @beta
export function invokeGet({ address }: {
Expand Down
5 changes: 3 additions & 2 deletions docs/landing/concepts/payment.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,12 @@ POST store-api/handle-payment
or using [**@shopware-pwa/shopware-6-client**](https://www.npmjs.com/package/@shopware-pwa/shopware-6-client)

```
import { getOrderPaymentUrl } from "@shopware-pwa/shopware-6-client"
import { handlePayment } from "@shopware-pwa/shopware-6-client"
const response: Promise<{ paymentUrl: string }> = getOrderPaymentUrl({
const response: Promise<{ paymentUrl: string }> = handlePayment({
orderId,
finishUrl,
errorUrl
})
```

Expand Down
2 changes: 1 addition & 1 deletion docs/landing/fundamentals/security.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ Similar to `composables`, the API client instance should be created for every re
import {
getShippingMethodDetails,
getPaymentMethodDetails,
getOrderPaymentUrl,
handlePayment,
} from "@shopware-pwa/shopware-6-client"
import { getApplicationContext } from "@shopware-pwa/composables"

Expand Down
2 changes: 1 addition & 1 deletion docs/landing/operations/migrations/0.8.x_to_0.9.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Just add `<script lang="ts">` in your components to start using it there. All yo
- `getContextPaymentMethodDetailsEndpoint`
- `getContextSalutationItemEndpoint`
- `getContextShippingMethodDetailsEndpoint`
- `getOrderPaymentUrl`
- `getOrderPaymentUrl` (`handlePayment` method is a replacement)

### [@shopware-pwa/composables](../../resources/api/composables)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@shopware-pwa/shopware-6-client](./shopware-6-client.md) &gt; [getStoreOrderPaymentUrl](./shopware-6-client.getstoreorderpaymenturl.md)
[Home](./index.md) &gt; [@shopware-pwa/shopware-6-client](./shopware-6-client.md) &gt; [handlePayment](./shopware-6-client.handlepayment.md)

## getStoreOrderPaymentUrl() function
## handlePayment() function

> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
>
<b>Signature:</b>

```typescript
export declare function getStoreOrderPaymentUrl(orderId: string, finishUrl?: string, errorUrl?: string, contextInstance?: ShopwareApiInstance): Promise<{
export declare function handlePayment(orderId: string, finishUrl?: string, errorUrl?: string, contextInstance?: ShopwareApiInstance): Promise<{
redirectUrl: string | null;
apiAlias: string;
}>;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [@shopware-pwa/shopware-6-client](./shopware-6-client.md) &gt; [getStoreOrderPaymentUrlEndpoint](./shopware-6-client.getstoreorderpaymenturlendpoint.md)
[Home](./index.md) &gt; [@shopware-pwa/shopware-6-client](./shopware-6-client.md) &gt; [handlePaymentEndpoint](./shopware-6-client.handlepaymentendpoint.md)

## getStoreOrderPaymentUrlEndpoint variable
## handlePaymentEndpoint variable

> This API is provided as a preview for developers and may change based on feedback that we receive. Do not use this API in a production environment.
>
Expand All @@ -11,5 +11,5 @@
<b>Signature:</b>

```typescript
getStoreOrderPaymentUrlEndpoint: () => string
handlePaymentEndpoint: () => string
```
4 changes: 2 additions & 2 deletions docs/landing/resources/api/shopware-6-client.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@
| [getSessionContext(contextInstance)](./shopware-6-client.getsessioncontext.md) | <b><i>(BETA)</i></b> Loads session context, containing all session-related data. |
| [getShippingMethodDetails(shippingId, contextInstance)](./shopware-6-client.getshippingmethoddetails.md) | <b><i>(BETA)</i></b> |
| [getStoreNavigation({ requestActiveId, requestRootId, depth, buildTree, searchCriteria, }, contextInstance)](./shopware-6-client.getstorenavigation.md) | <b><i>(BETA)</i></b> |
| [getStoreOrderPaymentUrl(orderId, finishUrl, errorUrl, contextInstance)](./shopware-6-client.getstoreorderpaymenturl.md) | <b><i>(BETA)</i></b> |
| [getUserCountry(countryId, contextInstance)](./shopware-6-client.getusercountry.md) | <b><i>(BETA)</i></b> |
| [getUserSalutation(salutationId, contextInstance)](./shopware-6-client.getusersalutation.md) | <b><i>(BETA)</i></b> |
| [handlePayment(orderId, finishUrl, errorUrl, contextInstance)](./shopware-6-client.handlepayment.md) | <b><i>(BETA)</i></b> |
| [invokeGet({ address }, contextInstance)](./shopware-6-client.invokeget.md) | <b><i>(BETA)</i></b> Invoke custom GET request to shopware API. Mostly for plugins usage. You can skip domain and pass only endpoint ex. <code>/api/my/endpoint</code> |
| [invokePost({ address, payload, }, contextInstance)](./shopware-6-client.invokepost.md) | <b><i>(BETA)</i></b> Invoke custom POST request to shopware API. Mostly for plugins usage. You can skip domain and pass only endpoint ex. <code>/api/my/endpoint</code> |
| [login({ username, password }, contextInstance)](./shopware-6-client.login.md) | <b><i>(BETA)</i></b> Login user to shopware instance. |
Expand Down Expand Up @@ -133,8 +133,8 @@
| [getStoreNewsletterConfirmEndpoint](./shopware-6-client.getstorenewsletterconfirmendpoint.md) | <b><i>(BETA)</i></b> |
| [getStoreNewsletterSubscribeEndpoint](./shopware-6-client.getstorenewslettersubscribeendpoint.md) | <b><i>(BETA)</i></b> |
| [getStoreNewsletterUnsubscribeEndpoint](./shopware-6-client.getstorenewsletterunsubscribeendpoint.md) | <b><i>(BETA)</i></b> |
| [getStoreOrderPaymentUrlEndpoint](./shopware-6-client.getstoreorderpaymenturlendpoint.md) | <b><i>(BETA)</i></b> |
| [getSuggestSearchEndpoint](./shopware-6-client.getsuggestsearchendpoint.md) | <b><i>(BETA)</i></b> |
| [handlePaymentEndpoint](./shopware-6-client.handlepaymentendpoint.md) | <b><i>(BETA)</i></b> |
| [onConfigChange](./shopware-6-client.onconfigchange.md) | <b><i>(BETA)</i></b> |
| [setup](./shopware-6-client.setup.md) | <b><i>(BETA)</i></b> Setup configuration. Merge default values with provided in param. This method will override existing config. For config update invoke \*\*update\*\* method. |
| [update](./shopware-6-client.update.md) | <b><i>(BETA)</i></b> Update current configuration. This will change only provided values. |
Expand Down
3 changes: 2 additions & 1 deletion packages/cli/src/commands/create-theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ const command: GluegunCommand = {
],
initial: 0,
});
const themeSelectOption: ThemeSelectOptions = baseThemeAnswers.baseThemeSelect as ThemeSelectOptions;
const themeSelectOption: ThemeSelectOptions =
baseThemeAnswers.baseThemeSelect as ThemeSelectOptions;

let baseThemePackageName = "";
if (themeSelectOption === "custom") {
Expand Down
7 changes: 3 additions & 4 deletions packages/cli/src/commands/languages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,11 @@ module.exports = {
const localPluginsConfig = await toolbox.plugins.getPluginsConfig({
localPlugins: true,
});
const localPluginsLocalesPaths = await toolbox.languages.getPluginsLocalesPaths(
{
const localPluginsLocalesPaths =
await toolbox.languages.getPluginsLocalesPaths({
pluginsConfig: localPluginsConfig,
rootDirectory: "sw-plugins",
}
);
});
for (let index = 0; index < localPluginsLocalesPaths.length; index++) {
const localePath = localPluginsLocalesPaths[index];
const localeMap = await toolbox.languages.getLocalesMap(localePath);
Expand Down
5 changes: 2 additions & 3 deletions packages/cli/src/extensions/plugins-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ module.exports = (toolbox: GluegunToolbox) => {
toolbox.fetchPluginsAuthToken = async (
{ shopwareEndpoint, username, password } = toolbox.inputParameters
) => {
const normalizedShopwareEndpoint = toolbox.normalizeBaseUrl(
shopwareEndpoint
);
const normalizedShopwareEndpoint =
toolbox.normalizeBaseUrl(shopwareEndpoint);
let authTokenResponse;
// Temporary turn off automatic credentials
// if (
Expand Down
4 changes: 2 additions & 2 deletions packages/default-theme/src/components/SwOrderDetails.vue
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ import {
import {
getShippingMethodDetails,
getPaymentMethodDetails,
getStoreOrderPaymentUrl,
handlePayment,
} from "@shopware-pwa/shopware-6-client"
import SwButton from "@/components/atoms/SwButton.vue"
import SwOrderDetailsItem from "@/components/SwOrderDetailsItem.vue"
Expand Down Expand Up @@ -210,7 +210,7 @@ export default {
try {
isPaymentButtonLoading.value = true
order.value = await getOrderDetails(orderId)
const resp = await getStoreOrderPaymentUrl(
const resp = await handlePayment(
orderId,
root.$routing.getAbsoluteUrl(
`${PAGE_ORDER_SUCCESS}?orderId=${orderId}`
Expand Down
2 changes: 1 addition & 1 deletion packages/default-theme/src/pages/checkout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ import {
getApplicationContext,
} from "@shopware-pwa/composables"
import { ref } from "@vue/composition-api"
import { getStoreOrderPaymentUrl as handlePayment } from "@shopware-pwa/shopware-6-client"
import { handlePayment } from "@shopware-pwa/shopware-6-client"
import SwRegistrationForm from "@/components/forms/SwRegistrationForm.vue"
import SwButton from "@/components/atoms/SwButton.vue"
import SwPluginSlot from "sw-plugins/SwPluginSlot.vue"
Expand Down
6 changes: 3 additions & 3 deletions packages/shopware-6-client/__tests__/endpoints.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {
getStoreNewsletterConfirmEndpoint,
getStoreNewsletterUnsubscribeEndpoint,
getStoreNavigationEndpoint,
getStoreOrderPaymentUrlEndpoint,
handlePaymentEndpoint,
getContactFormEndpoint,
getConfirmPasswordResetEndpoint,
} from "../src/endpoints";
Expand Down Expand Up @@ -183,9 +183,9 @@ describe("endpoints", () => {
});
});

describe("getStoreOrderPaymentUrlEndpoint", () => {
describe("handlePaymentEndpoint", () => {
it("should return handle payment method url endpoint", async () => {
const result = getStoreOrderPaymentUrlEndpoint();
const result = handlePaymentEndpoint();
expect(result).toEqual(`/store-api/handle-payment`);
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { getStoreOrderPaymentUrl } from "@shopware-pwa/shopware-6-client";
import { handlePayment } from "@shopware-pwa/shopware-6-client";
import { defaultInstance } from "../../../src/apiService";

jest.mock("../../../src/apiService");
const mockApiInstance = defaultInstance as jest.Mocked<typeof defaultInstance>;

describe("CheckoutService - getStoreOrderPaymentUrl", () => {
describe("CheckoutService - handlePayment", () => {
const mockedGet = jest.fn();
beforeEach(() => {
jest.resetAllMocks();
Expand All @@ -15,7 +15,7 @@ describe("CheckoutService - getStoreOrderPaymentUrl", () => {

it("should invoke correct API endpoint with given parameters", async () => {
mockedGet.mockResolvedValueOnce({ data: {} });
await getStoreOrderPaymentUrl("cd1a64c7166f42fa88b212b81e611d57");
await handlePayment("cd1a64c7166f42fa88b212b81e611d57");
expect(mockedGet).toBeCalledTimes(1);
expect(mockedGet).toHaveBeenCalledWith("/store-api/handle-payment", {
params: { orderId: "cd1a64c7166f42fa88b212b81e611d57" },
Expand All @@ -24,8 +24,8 @@ describe("CheckoutService - getStoreOrderPaymentUrl", () => {

it("should throw an error when data is incorrect", async () => {
mockedGet.mockRejectedValueOnce(new Error("404"));
expect(getStoreOrderPaymentUrl(null as any)).rejects.toThrowError(
"getStoreOrderPaymentUrl method requires orderId"
expect(handlePayment(null as any)).rejects.toThrowError(
"handlePayment method requires orderId"
);
expect(mockedGet).not.toBeCalled();
});
Expand All @@ -37,9 +37,7 @@ describe("CheckoutService - getStoreOrderPaymentUrl", () => {
apiAlias: "array_struct",
},
});
const result = await getStoreOrderPaymentUrl(
"cd1a64c7166f42fa88b212b81e611d57"
);
const result = await handlePayment("cd1a64c7166f42fa88b212b81e611d57");
expect(result).toEqual({
redirectResponse: null,
apiAlias: "array_struct",
Expand Down
3 changes: 1 addition & 2 deletions packages/shopware-6-client/src/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,7 @@ export const getContactFormEndpoint = () => `/store-api/contact-form`;
/**
* @beta
*/
export const getStoreOrderPaymentUrlEndpoint = () =>
`/store-api/handle-payment`;
export const handlePaymentEndpoint = () => `/store-api/handle-payment`;

/**
* @beta
Expand Down
18 changes: 6 additions & 12 deletions packages/shopware-6-client/src/services/checkoutService.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { defaultInstance, ShopwareApiInstance } from "../apiService";
import {
getCheckoutOrderEndpoint,
getStoreOrderPaymentUrlEndpoint,
} from "../endpoints";
import { getCheckoutOrderEndpoint, handlePaymentEndpoint } from "../endpoints";
import { Order } from "@shopware-pwa/commons/interfaces/models/checkout/order/Order";

/**
Expand All @@ -23,7 +20,7 @@ export async function createOrder(
* @param errorUrl - URL where the customer is redirected to after payment fails
* @beta
*/
export async function getStoreOrderPaymentUrl(
export async function handlePayment(
orderId: string,
finishUrl?: string,
errorUrl?: string,
Expand All @@ -33,15 +30,12 @@ export async function getStoreOrderPaymentUrl(
apiAlias: string;
}> {
if (!orderId) {
throw new Error("getStoreOrderPaymentUrl method requires orderId");
throw new Error("handlePayment method requires orderId");
}

const resp = await contextInstance.invoke.get(
getStoreOrderPaymentUrlEndpoint(),
{
params: { orderId, finishUrl, errorUrl },
}
);
const resp = await contextInstance.invoke.get(handlePaymentEndpoint(), {
params: { orderId, finishUrl, errorUrl },
});

return resp.data;
}

1 comment on commit 0b71b2b

@vercel
Copy link

@vercel vercel bot commented on 0b71b2b May 25, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.