-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
206 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { | ||
Payment, | ||
PaymentDraft, | ||
ReferenceTypeId, | ||
StateReference, | ||
Transaction, | ||
} from '@commercetools/platform-sdk' | ||
import AbstractRepository from './abstract' | ||
import { | ||
createCustomFields, | ||
createTypedMoney, | ||
getReferenceFromResourceIdentifier, | ||
} from './helpers' | ||
import { getBaseResourceProperties } from '../helpers' | ||
import { v4 as uuidv4 } from 'uuid' | ||
|
||
export class PaymentRepository extends AbstractRepository { | ||
getTypeId(): ReferenceTypeId { | ||
return 'payment' | ||
} | ||
|
||
create(projectKey: string, draft: PaymentDraft): Payment { | ||
const resource: Payment = { | ||
...getBaseResourceProperties(), | ||
amountPlanned: createTypedMoney(draft.amountPlanned), | ||
paymentMethodInfo: draft.paymentMethodInfo!, | ||
paymentStatus: draft.paymentStatus | ||
? { | ||
...draft.paymentStatus, | ||
state: draft.paymentStatus.state | ||
? getReferenceFromResourceIdentifier<StateReference>( | ||
draft.paymentStatus.state, | ||
projectKey, | ||
this._storage | ||
) | ||
: undefined, | ||
} | ||
: {}, | ||
transactions: (draft.transactions || []).map( | ||
(t): Transaction => ({ | ||
...t, | ||
id: uuidv4(), | ||
amount: createTypedMoney(t.amount), | ||
}) | ||
), | ||
interfaceInteractions: (draft.interfaceInteractions || []).map( | ||
interaction => | ||
createCustomFields(interaction, projectKey, this._storage)! | ||
), | ||
custom: createCustomFields(draft.custom, projectKey, this._storage), | ||
} | ||
|
||
this.save(projectKey, resource) | ||
return resource | ||
} | ||
|
||
actions = { | ||
// addInterfaceInteraction: () => {}, | ||
// addTransaction: () => {}, | ||
// changeAmountPlanned: () => {}, | ||
// changeTransactionInteractionId: () => {}, | ||
// changeTransactionState: () => {}, | ||
// changeTransactionTimestamp: () => {}, | ||
// setAmountPaid: () => {}, | ||
// setAmountRefunded: () => {}, | ||
// setAnonymousId: () => {}, | ||
// setAuthorization: () => {}, | ||
// setCustomField: () => {}, | ||
// setCustomType: () => {}, | ||
// setCustomer: () => {}, | ||
// setExternalId: () => {}, | ||
// setInterfaceId: () => {}, | ||
// setKey: () => {}, | ||
// setMethodInfoInterface: () => {}, | ||
// setMethodInfoMethod: () => {}, | ||
// setMethodInfoName: () => {}, | ||
// setStatusInterfaceCode: () => {}, | ||
// setStatusInterfaceText: () => {}, | ||
// transitionState: () => {}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { PaymentDraft } from '@commercetools/platform-sdk' | ||
import supertest from 'supertest' | ||
import { CommercetoolsMock } from '../index' | ||
|
||
const ctMock = new CommercetoolsMock() | ||
|
||
describe('Payment', () => { | ||
beforeEach(async () => { | ||
const response = await supertest(ctMock.app) | ||
.post('/dummy/types') | ||
.send({ | ||
key: 'custom-payment', | ||
name: { | ||
'nl-NL': 'custom-payment', | ||
}, | ||
resourceTypeIds: ['payment'], | ||
}) | ||
expect(response.status).toBe(200) | ||
}) | ||
|
||
test('Create payment', async () => { | ||
const draft: PaymentDraft = { | ||
amountPlanned: { currencyCode: 'EUR', centAmount: 1337 }, | ||
custom: { | ||
type: { typeId: 'type', key: 'custom-payment' }, | ||
fields: { | ||
foo: 'bar', | ||
}, | ||
}, | ||
} | ||
const response = await supertest(ctMock.app) | ||
.post('/dummy/payments') | ||
.send(draft) | ||
|
||
expect(response.status).toBe(200) | ||
expect(response.body).toEqual({ | ||
id: expect.anything(), | ||
createdAt: expect.anything(), | ||
lastModifiedAt: expect.anything(), | ||
version: 1, | ||
amountPlanned: { | ||
type: 'centPrecision', | ||
fractionDigits: 2, | ||
currencyCode: 'EUR', | ||
centAmount: 1337, | ||
}, | ||
paymentStatus: {}, | ||
transactions: [], | ||
interfaceInteractions: [], | ||
custom: { | ||
type: { typeId: 'type', id: expect.anything() }, | ||
fields: { foo: 'bar' }, | ||
}, | ||
}) | ||
}) | ||
test('Get payment', async () => { | ||
const draft: PaymentDraft = { | ||
amountPlanned: { currencyCode: 'EUR', centAmount: 1337 }, | ||
custom: { | ||
type: { typeId: 'type', key: 'custom-payment' }, | ||
fields: { | ||
foo: 'bar', | ||
}, | ||
}, | ||
} | ||
const createResponse = await supertest(ctMock.app) | ||
.post('/dummy/payments') | ||
.send(draft) | ||
|
||
const response = await supertest(ctMock.app).get( | ||
`/dummy/payments/${createResponse.body.id}` | ||
) | ||
|
||
expect(response.status).toBe(200) | ||
expect(response.body).toEqual(createResponse.body) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import AbstractService from './abstract' | ||
import { Router } from 'express' | ||
import { AbstractStorage } from '../storage' | ||
import { PaymentRepository } from 'repositories/payment' | ||
|
||
export class PaymentService extends AbstractService { | ||
public repository: PaymentRepository | ||
|
||
constructor(parent: Router, storage: AbstractStorage) { | ||
super(parent) | ||
this.repository = new PaymentRepository(storage) | ||
} | ||
|
||
getBasePath() { | ||
return 'payments' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters