-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(payment): Add payment API route
- Loading branch information
1 parent
2da9adb
commit d605f85
Showing
6 changed files
with
251 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Working with the Square API | ||
|
||
The `square` package from npm is used to easily interact with the Square API. Documentation for the SDK is available [here](https://developer.squareup.com/docs/sdks/nodejs). | ||
|
||
We work with the Checkout API's payment links. [Docs](https://developer.squareup.com/reference/square/checkout-api/create-payment-link) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { type NextRequest } from 'next/server'; | ||
import type { CreatePaymentLinkRequest, OrderLineItem, CreatePaymentLinkResponse } from 'square'; | ||
import { Client, Environment } from 'square'; | ||
import { products } from '@/data/products'; | ||
|
||
const client = new Client({ | ||
accessToken: process.env.SQUARE_ACCESS_TOKEN, | ||
environment: | ||
process.env.NODE_ENV === 'production' ? Environment.Production : Environment.Sandbox, | ||
}); | ||
|
||
const { checkoutApi } = client; | ||
|
||
export async function POST(request: NextRequest) { | ||
const params = request.nextUrl.searchParams; | ||
const product = params.get('product'); | ||
const customerId = params.get('customerId'); | ||
const redirectUrl = params.get('redirect'); | ||
|
||
// TODO: Validate that the user is signed in | ||
|
||
if (!product || !customerId || !redirectUrl) { | ||
return new Response('Missing URL parameters', { status: 400 }); | ||
} | ||
|
||
let lineItem: OrderLineItem; | ||
if (product === 'membership') { | ||
lineItem = products.membership; | ||
} else { | ||
return new Response('Product does not exist', { status: 400 }); | ||
} | ||
|
||
const body: CreatePaymentLinkRequest = { | ||
idempotencyKey: crypto.randomUUID(), | ||
description: 'Payment made from CS Club website', | ||
order: { | ||
locationId: process.env.SQUARE_LOCATION_ID!, | ||
customerId: customerId, | ||
lineItems: [lineItem], | ||
}, | ||
checkoutOptions: { | ||
allowTipping: false, | ||
redirectUrl: redirectUrl, | ||
askForShippingAddress: false, | ||
acceptedPaymentMethods: { | ||
applePay: true, | ||
googlePay: true, | ||
cashAppPay: false, | ||
afterpayClearpay: false, | ||
}, | ||
enableCoupon: false, | ||
enableLoyalty: false, | ||
}, | ||
}; | ||
|
||
const resp = await checkoutApi.createPaymentLink(body); | ||
const respFields: CreatePaymentLinkResponse = resp.result; | ||
if (resp.statusCode != 200) { | ||
return new Response(JSON.stringify(respFields.errors), { status: resp.statusCode }); | ||
} | ||
|
||
// The URL to direct the user is accessed from `url` and `long_url` | ||
return Response.json(respFields.paymentLink); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import type { OrderLineItem } from 'square'; | ||
|
||
export const products: { [key: string]: OrderLineItem } = { | ||
membership: { | ||
name: 'CS Club Membership 2024', | ||
quantity: '1', | ||
itemType: 'ITEM', | ||
metadata: { | ||
SKU: 'club_membership', | ||
}, | ||
basePriceMoney: { | ||
amount: BigInt(1000), | ||
currency: 'AUD', | ||
}, | ||
}, | ||
}; |