2021-10-13 05:08:07 +00:00
|
|
|
# [@root/paypal-checkout](https://git.rootprojects.org/root/paypal-checkout.js)
|
2021-10-09 23:10:40 +00:00
|
|
|
|
|
|
|
In contrast to the official PayPal Checkout SDK - which is auto-generated code
|
|
|
|
with lots of abstraction without much value - this is very little abstraction,
|
|
|
|
but specificially designed to be (mostly) idiomatic JavaScript / Node.js. \
|
|
|
|
(excuse the `snake_case` - that's how the PayPal REST API is designed).
|
|
|
|
|
2021-10-12 07:24:06 +00:00
|
|
|
![](https://i.imgur.com/brFTseM.png "PayPal Checkout API Flow")
|
|
|
|
|
|
|
|
<img src="https://www.paypalobjects.com/webstatic/en_US/i/buttons/checkout-logo-large.png" alt="Check out with PayPal" />
|
|
|
|
|
|
|
|
The Good Documentation™ for the PayPal API (a.k.a. PayPal Checkout SDK) is the
|
|
|
|
"REST API". See
|
|
|
|
|
|
|
|
- <https://developer.paypal.com/docs/api/orders/v2/> (one-time payments)
|
|
|
|
- <https://developer.paypal.com/docs/api/subscriptions/v1/> (recurring
|
|
|
|
subscriptions)
|
|
|
|
- <https://www.paypal.com/webapps/mpp/logos-buttons> (the buttons)
|
|
|
|
|
|
|
|
# Install
|
|
|
|
|
2021-10-09 23:10:40 +00:00
|
|
|
```bash
|
|
|
|
npm install --save @root/paypal-checkout
|
|
|
|
```
|
|
|
|
|
2021-10-12 07:24:06 +00:00
|
|
|
# Usage
|
|
|
|
|
2021-10-09 23:10:40 +00:00
|
|
|
```js
|
|
|
|
"use strict";
|
|
|
|
|
|
|
|
require("dotenv").config({ path: ".env" });
|
|
|
|
|
|
|
|
let PPC = require("@root/paypal-checkout");
|
|
|
|
PPC.init({
|
|
|
|
client_id: "xxxx",
|
|
|
|
client_secret: "****",
|
|
|
|
});
|
|
|
|
|
2021-10-09 23:17:38 +00:00
|
|
|
PPC.Subscriptions.createRequest({
|
|
|
|
// See https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions
|
|
|
|
});
|
2021-10-09 23:10:40 +00:00
|
|
|
```
|
|
|
|
|
2021-10-12 07:24:06 +00:00
|
|
|
# API
|
2021-10-09 23:10:40 +00:00
|
|
|
|
2021-10-12 07:24:06 +00:00
|
|
|
```txt
|
|
|
|
PayPal.init(client_id, client_secret, 'sandbox|live', defaults);
|
|
|
|
PayPal.request({ method, url, headers, json });
|
2021-10-17 04:07:59 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
### Subscrptions (Recurring Payments)
|
2021-10-09 23:10:40 +00:00
|
|
|
|
2021-10-17 04:07:59 +00:00
|
|
|
```txt
|
2021-10-12 08:25:56 +00:00
|
|
|
// Webhook 'event_type':
|
|
|
|
|
|
|
|
PayPal.Product.create({ ... }); // CATALOG.PRODUCT.CREATED
|
2021-10-12 07:24:06 +00:00
|
|
|
PayPal.Product.list();
|
|
|
|
PayPal.Product.details(id);
|
2021-10-12 08:25:56 +00:00
|
|
|
PayPal.Product.update(id, { description }); // CATALOG.PRODUCT.UPDATED
|
2021-10-12 07:24:06 +00:00
|
|
|
|
2021-10-12 08:25:56 +00:00
|
|
|
PayPal.Plan.create({ ... }); // BILLING.PLAN.CREATED
|
2021-10-12 07:24:06 +00:00
|
|
|
PayPal.Plan.list();
|
|
|
|
PayPal.Plan.details(id);
|
2021-10-12 08:25:56 +00:00
|
|
|
PayPal.Plan.update(id, { description }); // BILLING.PLAN.UPDATED
|
2021-10-12 07:24:06 +00:00
|
|
|
|
2021-10-12 08:25:56 +00:00
|
|
|
PayPal.Subscription.createRequest({ ... }); // BILLING.SUBSCRIPTION.CREATED
|
|
|
|
// subscription.links[rel="approve"].href // BILLING.SUBSCRIPTION.ACTIVATED
|
|
|
|
// PAYMENT.SALE.COMPLETED
|
2021-10-12 07:24:06 +00:00
|
|
|
PayPal.Subscription.details(id);
|
|
|
|
PayPal.Subscription.cancel(id, { reason });
|
|
|
|
```
|
|
|
|
|
2021-10-17 04:07:59 +00:00
|
|
|
### Orders (One-Time Payments)
|
|
|
|
|
|
|
|
```txt
|
|
|
|
PayPal.Order.createRequest({ ... }); // ??
|
|
|
|
```
|
|
|
|
|
|
|
|
See also:
|
|
|
|
|
|
|
|
- <https://developer.paypal.com/docs/api/orders/v2/#orders_create>
|
|
|
|
- <https://developer.paypal.com/docs/api/orders/v2/#definition-purchase_unit_request>
|
|
|
|
- <https://developer.paypal.com/docs/api/orders/v2/#definition-order_application_context>
|
|
|
|
|
2021-10-12 07:43:47 +00:00
|
|
|
# Redirects
|
|
|
|
|
2021-10-16 23:26:04 +00:00
|
|
|
- `return_url`
|
|
|
|
- `cancel_url`
|
|
|
|
|
2021-10-17 04:07:59 +00:00
|
|
|
### Orders
|
|
|
|
|
|
|
|
#### `return_url`
|
|
|
|
|
|
|
|
Order Request `return_url` will be called with the `token` query param as the
|
|
|
|
`order_id`:
|
|
|
|
|
|
|
|
```txt
|
|
|
|
https://example.com/redirects/paypal-checkout/return
|
|
|
|
?token=XXXXXXXXXXXXXXXXX
|
|
|
|
&PayerID=XXXXXXXXXXXXX
|
|
|
|
```
|
|
|
|
|
|
|
|
Again, `token` is the `order_id`.
|
|
|
|
|
|
|
|
#### `cancel_url`
|
|
|
|
|
|
|
|
The `cancel_url` will have the same query params as the `return_url`.
|
|
|
|
|
|
|
|
Also, PayPal presents the raw `cancel_url` and will NOT update the order status.
|
|
|
|
It's up to you to confirm with the user and change the status to `CANCELLED`.
|
|
|
|
|
|
|
|
### Subscriptions
|
|
|
|
|
2021-10-16 23:26:04 +00:00
|
|
|
#### `return_url`
|
|
|
|
|
2021-10-12 07:43:47 +00:00
|
|
|
Subscription Request `return_url` will include the following:
|
|
|
|
|
|
|
|
```txt
|
|
|
|
https://example.com/redirects/paypal-checkout/return
|
|
|
|
?subscription_id=XXXXXXXXXXXXXX
|
|
|
|
&ba_token=BA-00000000000000000
|
|
|
|
&token=XXXXXXXXXXXXXXXXX
|
|
|
|
```
|
|
|
|
|
2021-10-16 23:26:04 +00:00
|
|
|
#### `cancel_url`
|
|
|
|
|
|
|
|
The `cancel_url` will have the same query params as the `return_url`.
|
|
|
|
|
|
|
|
Also, PayPal presents the raw `cancel_url` and will NOT update the subscription
|
|
|
|
status. It's up to you to confirm with the user and change the status to
|
|
|
|
`CANCELLED`.
|
|
|
|
|
2021-10-12 07:24:06 +00:00
|
|
|
# Webhooks
|
|
|
|
|
|
|
|
Webhooks can be set up in the Application section of the Dashboard:
|
|
|
|
|
|
|
|
- <https://developer.paypal.com/developer/applications>
|
|
|
|
|
|
|
|
You'll see a list of applications. Click on one to access the webhooks.
|
|
|
|
|
2021-10-16 23:04:23 +00:00
|
|
|
**Security**: You must put a `secret` or `token` or your webhook URLs - PayPal
|
|
|
|
provides no measure of authentication (and otherwise an attacker could just send
|
|
|
|
random crap to your webhooks making it look like they've paid for all sorts of
|
|
|
|
things).
|
|
|
|
|
|
|
|
# Security
|
|
|
|
|
|
|
|
#### User email addresses
|
|
|
|
|
|
|
|
Emails addresses available through the PayPal Checkout API guaranteed to have
|
|
|
|
been verified by PayPal.
|
|
|
|
|
|
|
|
See:
|
|
|
|
|
|
|
|
- [Is `resource.subscriber.email_address` verified by PayPal?](https://twitter.com/paypaldev/status/1448238655743488008)
|
|
|
|
- [How do I receive money through PayPal?](https://www.paypal.com/us/smarthelp/article/how-do-i-receive-money-through-paypal-faq1750)
|
|
|
|
|
2021-10-12 07:24:06 +00:00
|
|
|
# Notes
|
|
|
|
|
2021-10-09 23:10:40 +00:00
|
|
|
Note: Just about everything in the PayPal SDK that uses `ALL_CAPS` is a
|
|
|
|
`constant`/`enum` representing an option you can pick from limited number of
|
|
|
|
options.
|
|
|
|
|
|
|
|
Sandbox accounts (for creating fake purchases) can be managed at:
|
|
|
|
<https://developer.paypal.com/developer/accounts>
|
|
|
|
|
|
|
|
Note on Auth + Capture:
|
|
|
|
|
|
|
|
> Authorization and capture enables you to authorize fund availability but delay
|
|
|
|
> fund capture. This can be useful for merchants who have a delayed order
|
|
|
|
> fulfillment process. Authorize & Capture also enables merchants to change the
|
|
|
|
> original authorization amount in case the order changes due to shipping,
|
|
|
|
> taxes, or gratuity.
|
|
|
|
>
|
|
|
|
> For any payment type, you can capture less than or the full original
|
|
|
|
> authorized amount. You can also capture up to 115% of or $75 USD more than the
|
|
|
|
> original authorized amount, whichever is less.
|
|
|
|
>
|
|
|
|
> See
|
|
|
|
>
|
|
|
|
> - <https://developer.paypal.com/docs/admin/auth-capture/>
|
|
|
|
> - <https://developer.paypal.com/docs/api/payments/v2/#authorizations_capture>
|
|
|
|
|
|
|
|
Buttons:
|
|
|
|
|
|
|
|
- <https://www.paypal.com/webapps/mpp/logos-buttons> <== THE ONE YOU WANT
|
|
|
|
- <img src="https://www.paypalobjects.com/webstatic/en_US/i/buttons/checkout-logo-large.png" alt="Check out with PayPal" />
|
|
|
|
- <https://developer.paypal.com/docs/checkout/>
|
|
|
|
- <https://www.paypal.com/buttons/>
|