Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

paypal-v2-sdk

Package Overview
Dependencies
Maintainers
1
Versions
37
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

paypal-v2-sdk

Unofficial promise-based PayPal API v2 SDK for node.js

  • 2.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
11
increased by83.33%
Maintainers
1
Weekly downloads
 
Created
Source

Installation

npm install paypal-v2-sdk

Usage

To write an app using this unofficial PayPal SDK (for v2 api)

  • Register for a Developer account and get your client_id and client_secret from the PayPal Developer Portal
  • Install paypal-v2-sdk (ensure dependency is in your package.json)
  • Import paypal-v2-sdk in your file
import PayPal from "paypal-v2-sdk";
  • Configure your PayPal instance (last param, true = sandbox, false = live)
PayPal.configure("YOUR CLIENT ID", "YOUR CLIENT SECRET", true);
  • Authenticate your connection (you only need to do this once)
try {
  await PayPal.authenticate();
} catch (e) {
  // handle your error
}

Samples

Invoices

  • Getting an invoice Returns Invoice object
import PayPal, { Invoice } from "paypal-v2-sdk";
const invoice: Invoice = await PayPal.Invoicing.get("id of invoice");
  • Getting an invoice and then deleting it
import PayPal, { Invoice } from "paypal-v2-sdk";
const invoice: Invoice = await PayPal.Invoicing.get("id of invoice");
const deleted = await Invoice.delete();
console.log(deleted); // true
  • Creating an invoice object and then creating a draft invoice on PayPal (fetches next invoice number for you)
import PayPal, { Invoice } from "paypal-v2-sdk";
const invoice: Invoice = await new PayPal.Invoice()
  .setDetail(
    new PayPal.Detail()
      .setCurrencyCode("USD")
      .setNote("Creating an invoice!")
      .setTermsAndConditions("url here")
  )
  .setInvoicer(
    new PayPal.InvoicerInfo()
      .setEmailAddress("test@test.com")
      .setPhones(
        [
          new PayPal.PhoneDetail()
            .setCountryCode("44")
            .setNationalNumber("123456")
        ]
      )
      .setWebsite("https://mystore.com/")
      .setTaxId("your tax id")
      .setLogoUrl("url")
  )
  .setPrimaryRecipients(
    [
      new PayPal.RecipientInfo()
        .setBillingInfo(
          new PayPal.BillingInfo()
            .setEmailAddress("test@test.com")
            .setAdditionalInfo("additional info")
        )
        .setShippingInfo(
          new PayPal.ContactInfo()
            .setBusinessName("My Cool Business")
            .setName(
              new PayPal.Name()
                .setPrefix("Mr")
                .setGivenName("Thomas")
                .setSurname("Whoever")
            )
            .setAddress(
              new PayPal.AddressPortable()
                .setAddressLine1("12")
                .setPostalCode("LE12 AH7")
                .setCountryCode("GB")
            )
        )
    ],
    [ // another one? ]
  )
  .setItems(
    [
      new PayPal.Item()
        .setName("T-Shirt")
        .setDescription("A very cool T-Shirt")
        .setQuantity("1")
        .setUnitAmount(
          new PayPal.Money()
            .setCurrencyCode("USD")
            .setValue("25")
        )
        .setTax(
          new PayPal.Tax()
            .setName("VAT")
            .setPercent("20")
        )
        .setDiscount(
          new PayPal.Discount()
            .setPercent("5")
        )
    ],
    [
      new PayPal.Item()
        .setName("Hoodie")
        .setDescription("A very very cool Hoodie")
        .setQuantity("3")
        .setUnitAmount(
          new PayPal.Money()
            .setCurrencyCode("USD")
            .setValue("40")
        )
        .setTax(
          new PayPal.Tax()
            .setName("VAT")
            .setPercent("20")
        )
        .setDiscount(
          new PayPal.Discount()
            .setPercent("15")
        )
    ]
  )
  .setConfiguration(
    new PayPal.Configuration()
      .setTaxCalculatedAfterDiscount(true)
      .setTaxInclusive(false)
      .setAllowTip(true)
      .setPartialPayment(
        new PayPal.PartialPayment()
          .setAllowPartialPayment(true)
          .setMinimumAmountDue(
            new PayPal.Money()
              .setCurrencyCode("USD")
              .setValue("72.5")
          )
      )
  )
  .setAmount(
    new PayPal.AmountSummaryDetail()
      .setBreakdown(
        new PayPal.AmountWithBreakdown()
          .setShipping(
            new PayPal.ShippingCost()
              .setTax(
                new PayPal.Tax()
                  .setName("Shipping Fee")
                  .setAmount(
                    new PayPal.Money()
                      .setCurrencyCode("USD")
                      .setValue("5.99")
                  )
              )
          )
          .setDiscount(
            new PayPal.AggregatedDiscount()
              .setInvoiceDiscount(
                new PayPal.Discount()
                  .setPercent("10")
              )
          )
      )
  ).createDraft(true)

  • Getting the next invoice number & creating an invoice via an object
import PayPal, { Invoice } from "paypal-v2-sdk";
const invoice: Invoice = await new PayPal.Invoice()
  .fromObject({
    detail: {
      invoice_number: "#123",
      reference: "deal-ref",
      invoice_date: "2018-11-12",
      currency_code: "USD",
      note: "Thank you for your business.",
      term: "No refunds after 30 days.",
      memo: "This is a long contract",
      payment_term: {
        term_type: "NET_10",
        due_date: "2018-11-22",
      },
    },
    invoicer: {
      name: {
        given_name: "David",
        surname: "Larusso",
      },
      address: {
        address_line_1: "1234 First Street",
        address_line_2: "337673 Hillside Court",
        admin_area_2: "Anytown",
        admin_area_1: "CA",
        postal_code: "98765",
        country_code: "US",
      },
      email_address: "merchant@example.com",
      phones: [
        {
          country_code: "001",
          national_number: "4085551234",
          phone_type: "MOBILE",
        },
      ],
      website: "www.test.com",
      tax_id: "ABcNkWSfb5ICTt73nD3QON1fnnpgNKBy- Jb5SeuGj185MNNw6g",
      logo_url: "https://example.com/logo.PNG",
      additional_notes: "2-4",
    },
    primary_recipients: [
      {
        billing_info: {
          name: {
            given_name: "Stephanie",
            surname: "Meyers",
          },
          address: {
            address_line_1: "1234 Main Street",
            admin_area_2: "Anytown",
            admin_area_1: "CA",
            postal_code: "98765",
            country_code: "US",
          },
          email_address: "bill-me@example.com",
          phones: [
            {
              country_code: "001",
              national_number: "4884551234",
              phone_type: "HOME",
            },
          ],
          additional_info_value: "add-info",
        },
        shipping_info: {
          name: {
            given_name: "Stephanie",
            surname: "Meyers",
          },
          address: {
            address_line_1: "1234 Main Street",
            admin_area_2: "Anytown",
            admin_area_1: "CA",
            postal_code: "98765",
            country_code: "US",
          },
        },
      },
    ],
    items: [
      {
        name: "Yoga Mat",
        description: "Elastic mat to practice yoga.",
        quantity: "1",
        unit_amount: {
          currency_code: "USD",
          value: "50.00",
        },
        tax: {
          name: "Sales Tax",
          percent: "7.25",
        },
        discount: {
          percent: "5",
        },
        unit_of_measure: "QUANTITY",
      },
      {
        name: "Yoga t-shirt",
        quantity: "1",
        unit_amount: {
          currency_code: "USD",
          value: "10.00",
        },
        tax: {
          name: "Sales Tax",
          percent: "7.25",
        },
        discount: {
          amount: {
            currency_code: "USD",
            value: "5.00",
          },
        },
        unit_of_measure: "QUANTITY",
      },
    ],
    configuration: {
      partial_payment: {
        allow_partial_payment: true,
        minimum_amount_due: {
          currency_code: "USD",
          value: "20.00",
        },
      },
      allow_tip: true,
      tax_calculated_after_discount: true,
      tax_inclusive: false,
      template_id: "TEMP-19V05281TU309413B",
    },
    amount: {
      breakdown: {
        custom: {
          label: "Packing Charges",
          amount: {
            currency_code: "USD",
            value: "10.00",
          },
        },
        shipping: {
          amount: {
            currency_code: "USD",
            value: "10.00",
          },
          tax: {
            name: "Sales Tax",
            percent: "7.25",
          },
        },
        discount: {
          invoice_discount: {
            percent: "5",
          },
        },
      },
    },
  })
  .createDraft(true);

Debugging

paypal-v2-sdk has a built-in Event Emitter.

You can output debug messages:

PayPal.on("debug", (str) => console.debug(str));

Keywords

FAQs

Package last updated on 21 Sep 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc