npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

nestjs-braintree

v0.0.2

Published

Nestjs braintree module

Downloads

343

Readme

NOTE! Currently building

Install

$ yarn add nestjs-braintree

Use

Basic use

import { Module } from '@nestjs/common';
import { BraintreeModule } from 'nestjs-braintree';
import * as braintree from 'braintree';

@Module({
  imports: [
    BraintreeModule.forRoot({
      environment: braintree.Environment.Sandbox,
      merchantId: '',
      publicKey: '',
      privateKey: '',
    }),
  ],
})
export default class AppModule {}

In a subModule

import { Module } from '@nestjs/common';
import { BraintreeModule } from 'nestjs-braintree';

@Module({
  imports: [
    BraintreeModule.forFeature(),
  ],
})
export default class SubModule {}

Use with nestjs-config

import { Module } from '@nestjs/common';
import { BraintreeModule } from 'nestjs-braintree';
import { ConfigModule, ConfigService } from 'nestjs-config';

@Module({
  imports: [
    ConfigModule.load('root/to/config/*/**.{ts,js}'),
    BraintreeModule.forRootAsync({
      useFactory: async (config: ConfigService) => config.get('braintree'),
      inject: [ConfigService],
    }),
  ],
})
export default class AppModule {}

//config/braintree.ts
import * as braintree from 'braintree';

export default {
  environment:
    process.env.NODE_ENV == 'development'
      ? braintree.Environment.Sandbox
      : braintree.Environment.Live,
  merchantId: process.env.BRAINTREE_MERCHANT_ID,
  publicKey: process.env.BRAINTREE_PUBLIC_KEY,
  privateKey: process.env.BRAINTREE_PRIVATE_KEY,
};

Transactions

Braintree is capable of making one off transactions

import { Module } from '@nestjs/common';
import { BraintreeModule, InjectBraintreeProvider } from 'nestjs-braintree';
import { ConfigModule, ConfigService } from 'nestjs-config';

class TransactionProvider {
  constructor(
    @InjectBraintreeProvider()
    private readonly braintreeProvider: BraintreeProvider,
  ) {}

  takePayment(amount: string, nonce: string) {
    this.braintreeProvider.sale({
      payment_method_nonce: nonce,
      amount,
    });
  }
}

@Module({
  imports: [
    ConfigModule.load('root/to/config/*/**.{ts,js}'),
    BraintreeModule.forRoot({
      useFactory: async (config: ConfigService) => config.get('braintree'),
      inject: [ConfigService],
    }),
  ],
  providers: [TransactionProvider],
})
export default class AppModule {}

Avaliable methods relating to transactions are

Sale

braintreeProvider.sale(transaction: BraintreeTransactionInterface): Promise<BraintreeTransactionResultInterface>

Refund

braintreeProvider.refund(transactionId: string, amount?: string, orderId?: string): Promise<BraintreeTransactionResultInterface>

Find

braintreeProvider.find(transactionId: string): Promise<BraintreeTransactionResultInterface>

The braintree SDK does offer additional methods. I will implement them soon hopefully

Webhooks

When using subscriptions with braintree, braintree will issue webhooks to your endpoint which you can use the decorators to handle those actions.

import { Module } from '@nestjs/common';
import {
  BraintreeModule,
  BraintreeWebhookModule,
  BraintreeSubscriptionCanceled,
  BraintreeSubscriptionExpired,
  BraintreeWebhookHandler,
} from 'nestjs-braintree';
import { ConfigModule, ConfigService } from 'nestjs-config';

@BraintreeWebhookHandler()
class SubscriptionProvider {
  @BraintreeSubscriptionCanceled()
  canceled() {
    console.log('subscription canceled');
  }

  @BraintreeSubscriptionExpired()
  expired() {
    console.log('subscription expired');
  }
}

@Module({
  imports: [
    ConfigModule.load('root/to/config/*/**.{ts,js}'),
    BraintreeModule.forRootAsync({
      useFactory: async (config: ConfigService) => config.get('braintree'),
      inject: [ConfigService],
    }),
    BraintreeWebhookModule,
  ],
  providers: [SubscriptionProvider],
})
export default class AppModule {}

Use Example

The idea of the Braintree Webhook Module is to make implementation of actions a lot easier. For example we can build a provider like this one to cancel canceled subscriptions.

@BraintreeWebhookHandler()
export class SubscriptionProvider {
  constructor(@InjectRepository(Subscription) private readonly subscriptionRepository: Repository<Subscription>) {}

  async findByBraintreeId(braintreeId: string): Promise<Subscription|null> {
    return await this.subscriptionRepository.find({
      where: {
        braintreeId,
      },
    });
  }

  async update(subscription: Subscription): Promise<boolean> {
    return await this.subscriptionRepository.update(subscription);
  }

  @BraintreeSubscriptionCanceled()
  async canceled(webhook: BraintreeWebhook) {
    const subscription = await this.findByBraintreeId(webhook.subscription.id);
    if (!subscription) {
      return;
    }
    subscription.active = false;
    await this.update(subscription);
  }
}

Available webhooks

Shortname | Braintree webhook name/const/key | NestJS decorator --- | --- | --- Subscription Canceled | subscription_canceled | @BraintreeSubscriptionCanceled() Subscription Expired | subscription_expired | @BraintreeSubscriptionExpired() Subscription Charged Successfully | subscription_charged_successfully | @BraintreeSubscriptionChargedSuccessfully() Subscription Charged Unsuccessfully | subscription_charged_unsuccessfully | @BraintreeSubscriptionChargedUnsuccessfully() Subscription Went Active | subscription_went_active | @BraintreeSubscriptionWentActive() Subscription Went Past Due | subscription_went_past_due | @BraintreeSubscriptionWentPastDue() Subscription Trial Ended | subscription_trial_ended | @BraintreeSubscriptionTrialEnded()

You can find out more about the webhooks here.

Custom routing for webhooks

You may want to divert from the default routing of {your_domain}/braintree/webhook for whatever reason. You can do so using the forRoot method on the BraintreeWebhookModule like so

@Module({
  imports: [
    ConfigModule.load('root/to/config/*/**.{ts,js}'),
    BraintreeModule.forRootAsync({
      useFactory: async (config: ConfigService) => config.get('braintree'),
      inject: [ConfigService],
    }),
    BraintreeWebhookModule.forRoot({
      root: 'replace-braintree',
      handle: 'replace-webhook',
    }),
  ],
  providers: [SubscriptionProvider],
})
export default class AppModule {}

The above will result in your route for your braintree webhooks being {your_domain}/replace-braintree/replace-webhook