nestjs-braintree
所属分类:TypeScript编程
开发工具:TypeScript
文件大小:0KB
下载次数:0
上传日期:2023-01-23 22:25:42
上 传 者:
sh-1993
说明: 用于智能树重复发生支付和交易的模块
(A module for braintree reoccurring payments and transactions)
文件列表:
.env.dist (96, 2021-06-09)
.prettierrc (198, 2021-06-09)
.travis.yml (235, 2021-06-09)
LICENSE (1073, 2021-06-09)
package.json (1456, 2021-06-09)
src/ (0, 2021-06-09)
src/__tests__/ (0, 2021-06-09)
src/__tests__/__stubs__/ (0, 2021-06-09)
src/__tests__/__stubs__/config/ (0, 2021-06-09)
src/__tests__/__stubs__/config/braintree.ts (258, 2021-06-09)
src/__tests__/braintree.controller.spec.ts (1924, 2021-06-09)
src/__tests__/braintree.module.spec.ts (3479, 2021-06-09)
src/__tests__/braintree.provider.spec.ts (1270, 2021-06-09)
src/__tests__/braintree.subscription.spec.ts (1120, 2021-06-09)
src/__tests__/braintree.transactions.spec.ts (1809, 2021-06-09)
src/__tests__/braintree.webhook.controller.options.spec.ts (2702, 2021-06-09)
src/__tests__/braintree.webhook.provider.spec.ts (4483, 2021-06-09)
src/__tests__/e2e/ (0, 2021-06-09)
src/__tests__/e2e/braintree.controller.e2e-spec.ts (2074, 2021-06-09)
src/__tests__/webhooks.spec.ts (5666, 2021-06-09)
src/braintree.constants.ts (843, 2021-06-09)
src/braintree.core.module.ts (1334, 2021-06-09)
src/braintree.module.ts (752, 2021-06-09)
src/braintree.provider.ts (2327, 2021-06-09)
src/braintree.webhook.controller.ts (1638, 2021-06-09)
src/braintree.webhook.module.ts (2636, 2021-06-09)
src/braintree.webhook.provider.ts (2193, 2021-06-09)
src/decorators/ (0, 2021-06-09)
src/decorators/braintree.webhook.handler.ts (234, 2021-06-09)
src/decorators/index.ts (178, 2021-06-09)
src/decorators/inject.braintree.provider.ts (205, 2021-06-09)
src/decorators/webhooks/ (0, 2021-06-09)
src/decorators/webhooks/braintree.webhook.method.decorator.ts (425, 2021-06-09)
src/decorators/webhooks/index.ts (32, 2021-06-09)
src/decorators/webhooks/subscriptions.ts (1374, 2021-06-09)
src/index.ts (605, 2021-06-09)
src/interfaces/ (0, 2021-06-09)
src/interfaces/braintree.options.interface.ts (250, 2021-06-09)
... ...
Nestjs Braintree
A module for Braintree reoccurring payments and transactions built for the Nestjs framework.
Using the Braintree node SDK.
> NOTE! Currently building
## Install
```bash
$ yarn add nestjs-braintree
```
## Use
### Basic use
```typescript
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
```typescript
import { Module } from '@nestjs/common';
import { BraintreeModule } from 'nestjs-braintree';
@Module({
imports: [
BraintreeModule.forFeature(),
],
})
export default class SubModule {}
```
### Use with nestjs-config
```typescript
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
```typescript
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 {}
```
Available methods relating to transactions are
#### Sale
`braintreeProvider.sale(transaction: BraintreeTransactionInterface): Promise
`
#### Refund
`braintreeProvider.refund(transactionId: string, amount?: string, orderId?: string): Promise`
#### Find
`braintreeProvider.find(transactionId: string): Promise`
> 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.
```typescript
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.
```ts
@BraintreeWebhookHandler()
export class SubscriptionProvider {
constructor(@InjectRepository(Subscription) private readonly subscriptionRepository: Repository) {}
async findByBraintreeId(braintreeId: string): Promise {
return await this.subscriptionRepository.find({
where: {
braintreeId,
},
});
}
async update(subscription: Subscription): Promise {
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](https://developers.braintreepayments.com/reference/general/webhooks/overview).
#### 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
```ts
@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`
近期下载者:
相关文件:
收藏者: