Code Monkey home page Code Monkey logo

nestjs-paypal's Introduction

PayPal Payment

Usage

Initialization

import { Module } from '@nestjs/common';
import { PaypalPaymentModule } from './paypal-payment.module';
import { ConfigModule, ConfigService } from '@nestjs/config';
import configurations from './configurations';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
      expandVariables: true,
      load: [configurations],
    }),
    // Sync
    PaypalPaymentModule.register({
      clientId: process.env.PAYPAL_CLIENT_ID,
      clientSecret: process.env.PAYPAL_CLIENT_SECRET,
      environment: process.env.PAYPAL_ENVIRONMENT as ("sandbox" | "live")
    }),
    // Async
    PaypalPaymentModule.registerAsync({
      inject: [ConfigService],
      useFactory: (configService: ConfigService) => {
        return {
          ...configService.get('paypalModuleInterface'),
        };
      },
    }),
  ],
  controllers: [],
  providers: [],
})
export class AppModule {
}

Inject & Use service

constructor(@InjectScandiniaviaPaypal() private paymentService: PaypalPaymentService) {}

Initiate order

    const orderPayload: CreatePaypalOrderDto = {
      intent: 'CAPTURE',
      purchase_units: [
        {
          amount: {
            "currency_code": "USD",
            "value": "100.00"
          }
        }
      ]
    }
    const order = await initiateOrder(orderPayload, {
      Prefer: 'return=representation'
    });

Second param of initiateOrder is custom headers to control the response of initiating order

Available headers: The server stores keys for 6 hours. The API callers can request the times to up to 72 hours by speaking to their Account Manager.

  • 'PayPal-Request-Id'?: string;
  • 'PayPal-Partner-Attribution-Id'?: string;
  • 'PayPal-Client-Metadata-Id'?: string;

The preferred server response upon successful completion of the request. Value is: return=minimal. The server returns a minimal response to optimize communication between the API caller and the server. A minimal response includes the id, status and HATEOAS links. return=representation. The server returns a complete resource representation, including the current state of the resource.

  • Prefer?: 'return=minimal' | 'return=representation';

Get order

    const order = await paymentService.getOrderDetails(orderId);

Update order

const order = // PayPal created order
paymentService.updateOrder(order.id, [
        {
          op: 'add',
          path: `/purchase_units/@reference_id=='${order.purchase_units[0].reference_id}'/shipping/address`,
          value: {
            "address_line_1": "123 Townsend St",
            "address_line_2": "Floor 6",
            "admin_area_2": "San Francisco",
            "admin_area_1": "CA",
            "postal_code": "94107",
            "country_code": "US"
          }
        }
      ]);
    })
    
 

nestjs-paypal's People

Contributors

baopq27 avatar falyoun avatar

Watchers

Pham Quoc Bao avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.