-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(payment): Add BigPay v2 payload mappers
- Loading branch information
Showing
12 changed files
with
548 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { omitNil } from '../../../common/utils'; | ||
|
||
export default class CartMapper { | ||
/** | ||
* @returns {CartMapper} | ||
*/ | ||
static create() { | ||
return new CartMapper(); | ||
} | ||
|
||
/** | ||
* @param {PaymentRequestData} data | ||
* @returns {Object} | ||
*/ | ||
mapToCart(data) { | ||
const { cart = {} } = data; | ||
|
||
return omitNil({ | ||
currency_code: cart.currency, | ||
items: this.mapToItems(data), | ||
totals: this.mapToOrderTotals(data), | ||
}); | ||
} | ||
|
||
/** | ||
* @private | ||
* @param {PaymentRequestData} data | ||
* @returns {Object[]} | ||
*/ | ||
mapToItems(data) { | ||
const { cart = { items: [] } } = data; | ||
|
||
return cart.items.map(itemData => omitNil({ | ||
discount_amount: itemData.integerDiscount, | ||
name: itemData.name, | ||
price: itemData.integerAmount, | ||
quantity: itemData.quantity, | ||
sku: itemData.sku, | ||
tax_amount: itemData.integerTax, | ||
amount: itemData.integerAmountAfterDiscount, | ||
type: this.mapToType(itemData), | ||
})); | ||
} | ||
|
||
/** | ||
* @private | ||
* @param {PaymentRequestData} data | ||
* @returns {Object} | ||
*/ | ||
mapToOrderTotals(data) { | ||
const { cart = {} } = data; | ||
|
||
return omitNil({ | ||
discount_total: cart.discount ? cart.discount.integerAmount : null, | ||
grand_total: cart.grandTotal ? cart.grandTotal.integerAmount : null, | ||
shipping_total: cart.shipping ? cart.shipping.integerAmount : null, | ||
subtotal: cart.subtotal ? cart.subtotal.integerAmount : null, | ||
surcharge_total: cart.handling ? cart.handling.integerAmount : null, | ||
tax_total: cart.taxTotal ? cart.taxTotal.integerAmount : null, | ||
}); | ||
} | ||
|
||
/** | ||
* @private | ||
* @param {Object} itemData | ||
* @returns {Object} | ||
*/ | ||
mapToType(itemData) { | ||
const types = { | ||
ItemPhysicalEntity: 'physical', | ||
ItemDigitalEntity: 'digital', | ||
ItemGiftCertificateEntity: 'gift_card', | ||
}; | ||
|
||
return types[itemData.type]; | ||
} | ||
} |
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,37 @@ | ||
import { omitNil } from '../../../common/utils'; | ||
import PaymentMethodIdMapper from '../../payment-method-mappers/payment-method-id-mapper'; | ||
|
||
export default class GatewayMapper { | ||
/** | ||
* @returns {GatewayMapper} | ||
*/ | ||
static create() { | ||
const paymentMethodIdMapper = PaymentMethodIdMapper.create(); | ||
|
||
return new GatewayMapper(paymentMethodIdMapper); | ||
} | ||
|
||
/** | ||
* @param {PaymentMethodIdMapper} paymentMethodIdMapper | ||
* @returns {void} | ||
*/ | ||
constructor(paymentMethodIdMapper) { | ||
/** | ||
* @private | ||
* @type {PaymentMethodIdMapper} | ||
*/ | ||
this.paymentMethodIdMapper = paymentMethodIdMapper; | ||
} | ||
|
||
/** | ||
* @param {PaymentRequestData} data | ||
* @returns {Object} | ||
*/ | ||
mapToGateway(data) { | ||
const { paymentMethod = {} } = data; | ||
|
||
return omitNil({ | ||
name: this.paymentMethodIdMapper.mapToId(paymentMethod), | ||
}); | ||
} | ||
} |
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,22 @@ | ||
import { omitNil } from '../../../common/utils'; | ||
|
||
export default class HeaderMapper { | ||
/** | ||
* @returns {HeaderMapper} | ||
*/ | ||
static create() { | ||
return new HeaderMapper(); | ||
} | ||
|
||
/** | ||
* @param {PaymentRequestData} data | ||
* @returns {Object} | ||
*/ | ||
mapToHeaders(data) { | ||
const { authToken } = data; | ||
|
||
return omitNil({ | ||
Authorization: authToken, | ||
}); | ||
} | ||
} |
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,46 @@ | ||
import { omitNil } from '../../../common/utils'; | ||
|
||
export default class QuoteMapper { | ||
/** | ||
* @returns {QuoteMapper} | ||
*/ | ||
static create() { | ||
return new QuoteMapper(); | ||
} | ||
|
||
/** | ||
* @param {PaymentRequestData} data | ||
* @returns {Object} | ||
*/ | ||
mapToQuote(data) { | ||
return omitNil({ | ||
billing_address: this.mapToAddress(data, 'billingAddress'), | ||
shipping_address: this.mapToAddress(data, 'shippingAddress'), | ||
}); | ||
} | ||
|
||
/** | ||
* @private | ||
* @param {PaymentRequestData} data | ||
* @param {string} addressKey | ||
* @returns {Object} | ||
*/ | ||
mapToAddress(data, addressKey) { | ||
const { customer = {} } = data; | ||
const address = data[addressKey] || {}; | ||
|
||
return omitNil({ | ||
address_line_1: address.addressLine1, | ||
address_line_2: address.addressLine2, | ||
city: address.city, | ||
company: address.company, | ||
country_code: address.countryCode, | ||
email: customer.email, | ||
first_name: address.firstName, | ||
last_name: address.lastName, | ||
phone: address.phone, | ||
postal_code: address.postCode, | ||
state: address.province, | ||
}); | ||
} | ||
} |
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,67 @@ | ||
import { omitNil, toNumber } from '../../../common/utils'; | ||
|
||
export default class StoreMapper { | ||
/** | ||
* @returns {StoreMapper} | ||
*/ | ||
static create() { | ||
return new StoreMapper(); | ||
} | ||
|
||
/** | ||
* @param {PaymentRequestData} data | ||
* @returns {Object} | ||
*/ | ||
mapToStore(data) { | ||
return omitNil({ | ||
locale: this.mapToLocale(data), | ||
store_identity: this.mapToIdentity(data), | ||
urls: this.mapToUrls(data), | ||
}); | ||
} | ||
|
||
/** | ||
* @private | ||
* @param {PaymentRequestData} data | ||
* @returns {Object} | ||
*/ | ||
mapToLocale(data) { | ||
const { store = {} } = data; | ||
|
||
return omitNil({ | ||
country_code: store.countryCode, | ||
currency_code: store.currencyCode, | ||
language_code: store.storeLanguage, | ||
}); | ||
} | ||
|
||
/** | ||
* @private | ||
* @param {PaymentRequestData} data | ||
* @returns {Object} | ||
*/ | ||
mapToIdentity(data) { | ||
const { store = {} } = data; | ||
|
||
return omitNil({ | ||
id: store.storeId ? toNumber(store.storeId) : null, | ||
name: store.storeName, | ||
}); | ||
} | ||
|
||
/** | ||
* @private | ||
* @param {PaymentRequestData} data | ||
* @returns {Object} | ||
*/ | ||
mapToUrls(data) { | ||
const { store = {} } = data; | ||
|
||
return omitNil({ | ||
cart: store.cartLink, | ||
checkout: store.checkoutLink, | ||
confirmation: store.orderConfirmationLink, | ||
home: store.shopPath, | ||
}); | ||
} | ||
} |
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
Oops, something went wrong.