Created
December 23, 2020 23:06
-
-
Save biancadanforth/0cea22fcfaab65843eea961230906bc4 to your computer and use it in GitHub Desktop.
PayPal Reference Transaction prototype, using implementation option 2 from https://developer.paypal.com/docs/archive/express-checkout/ec-set-up-reference-transactions/#implementation-options
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
const fetch = require("node-fetch"); | |
const config = { | |
paypal: require("./paypal-config")["DEV"], | |
}; | |
const PAYPAL_SANDBOX_NVP_BASE_URL = "https://api-3t.sandbox.paypal.com/nvp"; | |
const nvpUrl = new URL(PAYPAL_SANDBOX_NVP_BASE_URL); | |
const params = { | |
USER: config.paypal.USER, // Merchant username | |
PWD: config.paypal.PWD, // Merchant password | |
SIGNATURE: config.paypal.SIGNATURE, // Merchant signature | |
METHOD: "SetExpressCheckout", | |
VERSION: 204, // TODO: Finalize version | |
PAYMENTREQUEST_0_PAYMENTACTION: "AUTHORIZATION", | |
PAYMENTREQUEST_0_AMT: 0, // The amount authorized | |
PAYMENTREQUEST_0_CURRENCYCODE: "USD", // The currency, e.g. US dollars | |
L_BILLINGTYPE0: "MerchantInitiatedBilling", // The type of billing agreement | |
L_BILLINGAGREEMENTDESCRIPTION0: "TestAuthorization", // The description of the billing agreement | |
cancelUrl: "https://example.com/cancel", // For use if the consumer decides not to proceed with payment | |
returnUrl: "https://example.com/success", // For use if the consumer proceeds with payment | |
}; | |
Object.keys(params).forEach(key => nvpUrl.searchParams.append(key, params[key])); | |
async function setupPaymentAuthorization() { | |
const metadata = { | |
method: "POST", | |
body: nvpUrl.searchParams.toString(), | |
} | |
const response = await fetch(PAYPAL_SANDBOX_NVP_BASE_URL, metadata); | |
if (!response.ok) { | |
throw new Error(`${response.statusText}`); | |
} | |
const data = await response.text(); | |
console.log(data); | |
// A successful response is logged of the form described at https://developer.paypal.com/docs/archive/express-checkout/ec-set-up-reference-transactions/#response | |
} | |
// Step 1: Set up the payment authorization | |
setupPaymentAuthorization(); | |
// TODO: Step 2: Redirect the buyer to PayPal for authorization. | |
// TODO: Step 3: Create the billing agreement to obtain the billing ID number. | |
// TODO: Step 4: Capture future payments. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment