Compare commits

...

3 Commits
v0.3.0 ... main

Author SHA1 Message Date
f47523f56d 0.3.1 2021-10-23 16:09:05 -06:00
70da150599 bugfix: add missing subscription properties 2021-10-23 16:08:47 -06:00
3d18aa00de docs: add dispute and reversal webhooks to quickstart 2021-10-19 00:01:27 -06:00
4 changed files with 36 additions and 11 deletions

View File

@ -135,10 +135,13 @@ here's the gist of what you need to do:
}); });
``` ```
4. Handle the PAYMENT.CAPTURE.COMPLETED WebHook 4. [Set](https://developer.paypal.com/developer/applications) and Handle the
[`PAYMENT.CAPTURE.COMPLETED`, `PAYMENT.CAPTURE.REVERSED`, and `CUSTOMER.DISPUTE.CREATED`](https://developer.paypal.com/docs/api-basics/notifications/webhooks/event-names/)
WebHooks
```js ```js
// Set webhook at https://developer.paypal.com/developer/applications // Set webhook at https://developer.paypal.com/developer/applications
// Descriptions at https://developer.paypal.com/docs/api-basics/notifications/webhooks/event-names/
app.get("/api/webhooks/paypal-checkout/:secret", async function (req, res) { app.get("/api/webhooks/paypal-checkout/:secret", async function (req, res) {
let crypto = require("crypto"); let crypto = require("crypto");
let secret = process.env.PAYPAL_WEBHOOK_SECRET || ""; let secret = process.env.PAYPAL_WEBHOOK_SECRET || "";
@ -154,13 +157,30 @@ here's the gist of what you need to do:
let event = req.body; let event = req.body;
switch (event.event_type) { switch (event.event_type) {
case "PAYMENT.CAPTURE.COMPLETED": { case "PAYMENT.CAPTURE.COMPLETED":
let orderId = event.supplementary_data.related_ids.order_id; {
let localDbId = event.custom_id; let orderId = event.supplementary_data.related_ids.order_id;
console.info( let localDbId = event.custom_id;
`Confirm that PayPal Order ${orderId} for ${localDbId} has been paid.` console.info(
); `Confirm that PayPal Order ${orderId} for ${localDbId} has been paid.`
} );
}
break;
case "PAYMENT.CAPTURE.REVERSED":
{
// deduct from user's account
}
break;
case "CUSTOMER.DISPUTE.CREATED":
{
// TODO send email to merchant (myself) to check out the dispute
}
break;
case "CUSTOMER.DISPUTE.CREATED":
{
// TODO send email to merchant (myself) to review the dispute status
}
break;
default: default:
console.log("Ignoring", event.event_type); console.log("Ignoring", event.event_type);
res.json({ sucess: true }); res.json({ sucess: true });

4
package-lock.json generated
View File

@ -1,12 +1,12 @@
{ {
"name": "@root/paypal-checkout", "name": "@root/paypal-checkout",
"version": "0.3.0", "version": "0.3.1",
"lockfileVersion": 2, "lockfileVersion": 2,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "@root/paypal-checkout", "name": "@root/paypal-checkout",
"version": "0.3.0", "version": "0.3.1",
"license": "SEE LICENSE IN LICENSE", "license": "SEE LICENSE IN LICENSE",
"dependencies": { "dependencies": {
"@root/request": "^1.7.0" "@root/request": "^1.7.0"

View File

@ -1,6 +1,6 @@
{ {
"name": "@root/paypal-checkout", "name": "@root/paypal-checkout",
"version": "0.3.0", "version": "0.3.1",
"description": "A more sensible, human-generated wrapper for the PayPal Checkout REST API", "description": "A more sensible, human-generated wrapper for the PayPal Checkout REST API",
"main": "paypal-checkout.js", "main": "paypal-checkout.js",
"files": [ "files": [

View File

@ -441,6 +441,7 @@ Subscription.payee_preferences = {
IMMEDIATE_PAYMENT_REQUIRED: "IMMEDIATE_PAYMENT_REQUIRED", IMMEDIATE_PAYMENT_REQUIRED: "IMMEDIATE_PAYMENT_REQUIRED",
}; };
// See https://developer.paypal.com/docs/api/subscriptions/v1/#subscriptions_create
Subscription.createRequest = async function _createSubscription({ Subscription.createRequest = async function _createSubscription({
request_id, request_id,
plan_id, plan_id,
@ -449,6 +450,8 @@ Subscription.createRequest = async function _createSubscription({
shipping_amount, shipping_amount,
subscriber, subscriber,
application_context, application_context,
custom_id,
plan,
}) { }) {
return await PayPal.request({ return await PayPal.request({
method: "POST", method: "POST",
@ -496,6 +499,8 @@ Subscription.createRequest = async function _createSubscription({
} }
*/ */
application_context: application_context, application_context: application_context,
custom_id: custom_id,
plan: plan,
}, },
}) })
.then(must201or200) .then(must201or200)