PayPalPlugin
PayPalPlugin
This plugin enables payments via the PayPal Order API.
Note: This plugin only supports a 2 step flow. This means that the payment has to manually captured by an admin.
Requirements
-
Create a PayPal business account.
-
Make sure you have the client ID and secret of your PayPal REST API app.
-
Get the merchant ID from your PayPal account. This is the account ID of your PayPal business account.
-
Install the Payments plugin:
yarn add @vendure/payments-plugin
or
npm install @vendure/payments-plugin
Setup
- Add the plugin to your VendureConfig
plugins
array:
import { PayPalPlugin } from '@vendure/payments-plugin/package/paypal';
// ...
plugins: [
// Set the apiUrl to the PayPal sandbox environment
PayPalPlugin.init({ apiUrl: 'https://sandbox.paypal.com/' }),
]
- Create a new PaymentMethod in the Admin UI, and select "PayPal payments" as the handler.
- Set your PayPal client ID, secret and merchant ID in the according fields.
Storefront usage
To successfully make a payment, the following steps are explained in detail:
- Create a PayPal order
- Use the PayPal SDK to authorize the payment
- Add the payment to the order
- Capture the payment
Create PayPal order
Begin by creating a new PayPal order. This order is the reference for the payment and is used to authorize the payment. Make sure to store the order ID in your frontend, as it is needed in the next steps.
This step does not modify any data on your Vendure instance. It only creates a new order in the PayPal system.
Create the PayPal order using the following mutation:
mutation CreatePayPalOrder {
createPayPalOrder() {
... on PayPalOrder {
id
}
}
}
Authorize payment
The PayPal order you created must be authorized by your customers. This step is handled by the PayPal SDK for the most part.
For JavaScript projects, you can check out this integration guide to integrate the PayPal SDK: PayPal SDK Integration Guide.
Add payment
After authorizing the payment, you need to add it to the Vendure order. This will add and validate the authorizations add to the payment in the previous step. If the payment is valid, the order will be updated to the next state.
mutation AddPaymentToOrder() {
addPaymentToOrder(input: {
method: "paypal-payment-method",
metadata: {
orderId: "the PayPal order ID"
}
}) {
... on Order {
id
code
state
payments {
id
state
transactionId
method
}
}
... on ErrorResult {
message
errorCode
}
... on PaymentFailedError {
paymentErrorMessage
}
}
}
Capture payment
Using the admin ui, the admin can settle this payment. After this step, the payment is successfully captured.
Creating refunds
Refunds can be created like any other refund in Vendure. The refund will be processed through the PayPal API.
class PayPalPlugin {
static options: PayPalPluginOptions;
init(options: PayPalPluginOptions) => Type<PayPalPlugin>;
}
options
PayPalPluginOptions
init
(options: PayPalPluginOptions) => Type<PayPalPlugin>
Initialize the PayPal payment plugin