Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

A module for braintree reoccurring payments and transactions 💳

License

NotificationsYou must be signed in to change notification settings

nestjsx/nestjs-braintree

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

99 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Coverage Status

Nestjs Braintree

A module forBraintree reoccurring payments and transactions built for theNestjs framework.


Using theBraintree node SDK.

NOTE! Currently building

Install

$ yarn add nestjs-braintree

Use

Basic use

import{Module}from'@nestjs/common';import{BraintreeModule}from'nestjs-braintree';import*asbraintreefrom'braintree';@Module({imports:[BraintreeModule.forRoot({environment:braintree.Environment.Sandbox,merchantId:'',publicKey:'',privateKey:'',}),],})exportdefaultclassAppModule{}

In a subModule

import{Module}from'@nestjs/common';import{BraintreeModule}from'nestjs-braintree';@Module({imports:[BraintreeModule.forFeature(),],})exportdefaultclassSubModule{}

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],}),],})exportdefaultclassAppModule{}//config/braintree.tsimport*asbraintreefrom'braintree';exportdefault{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';classTransactionProvider{constructor(    @InjectBraintreeProvider()privatereadonlybraintreeProvider: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],})exportdefaultclassAppModule{}

Available 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 yourendpoint 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()classSubscriptionProvider{  @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],})exportdefaultclassAppModule{}

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()exportclassSubscriptionProvider{constructor(@InjectRepository(Subscription)privatereadonlysubscriptionRepository:Repository<Subscription>){}asyncfindByBraintreeId(braintreeId:string):Promise<Subscription|null>{returnawaitthis.subscriptionRepository.find({where:{        braintreeId,},});}asyncupdate(subscription:Subscription):Promise<boolean>{returnawaitthis.subscriptionRepository.update(subscription);}  @BraintreeSubscriptionCanceled()asynccanceled(webhook:BraintreeWebhook){constsubscription=awaitthis.findByBraintreeId(webhook.subscription.id);if(!subscription){return;}subscription.active=false;awaitthis.update(subscription);}}

Available webhooks

ShortnameBraintree webhook name/const/keyNestJS decorator
Subscription Canceledsubscription_canceled@BraintreeSubscriptionCanceled()
Subscription Expiredsubscription_expired@BraintreeSubscriptionExpired()
Subscription Charged Successfullysubscription_charged_successfully@BraintreeSubscriptionChargedSuccessfully()
Subscription Charged Unsuccessfullysubscription_charged_unsuccessfully@BraintreeSubscriptionChargedUnsuccessfully()
Subscription Went Activesubscription_went_active@BraintreeSubscriptionWentActive()
Subscription Went Past Duesubscription_went_past_due@BraintreeSubscriptionWentPastDue()
Subscription Trial Endedsubscription_trial_ended@BraintreeSubscriptionTrialEnded()

You can find out more about the webhookshere.

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 theforRoot method on theBraintreeWebhookModule 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],})exportdefaultclassAppModule{}

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


[8]ページ先頭

©2009-2025 Movatter.jp