-
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.
refactor(payment): Extract classes from initializeOffsitePayment func…
…tion
- Loading branch information
Showing
32 changed files
with
606 additions
and
323 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 |
---|---|---|
@@ -1,9 +1,7 @@ | ||
import * as PAYMENT_TYPES from './payment-types'; | ||
import initializeOffsitePayment from './initialize-offsite-payment'; | ||
import submitPayment from './submit-payment'; | ||
|
||
export { | ||
PAYMENT_TYPES, | ||
initializeOffsitePayment, | ||
submitPayment, | ||
}; |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,63 @@ | ||
import { createFormPoster } from 'form-poster'; | ||
import { HOSTED } from './payment-types'; | ||
import PayloadMapper from './offsite-payment-mappers/payload-mapper'; | ||
import UrlHelper from './url-helper'; | ||
|
||
export default class OffsitePaymentInitializer { | ||
/** | ||
* @param {Object} config | ||
* @returns {OffsitePaymentInitializer} | ||
*/ | ||
static create(config) { | ||
const urlHelper = UrlHelper.create(config); | ||
const formPoster = createFormPoster(); | ||
const payloadMapper = PayloadMapper.create(); | ||
|
||
return new OffsitePaymentInitializer(urlHelper, formPoster, payloadMapper); | ||
} | ||
|
||
/** | ||
* @param {UrlHelper} urlHelper | ||
* @param {FormPoster} formPoster | ||
* @param {PayloadMapper} payloadMapper | ||
* @returns {void} | ||
*/ | ||
constructor(urlHelper, formPoster, payloadMapper) { | ||
/** | ||
* @private | ||
* @type {UrlHelper} | ||
*/ | ||
this.urlHelper = urlHelper; | ||
|
||
/** | ||
* @private | ||
* @type {FormPoster} | ||
*/ | ||
this.formPoster = formPoster; | ||
|
||
/** | ||
* @private | ||
* @type {PayloadMapper} | ||
*/ | ||
this.payloadMapper = payloadMapper; | ||
} | ||
|
||
/** | ||
* @param {PaymentRequestData} data | ||
* @param {Function} [callback] | ||
* @returns {void} | ||
* @throws {Error} | ||
*/ | ||
initializeOffsitePayment(data, callback) { | ||
const { paymentMethod = {} } = data; | ||
|
||
if (paymentMethod.type !== HOSTED) { | ||
throw new Error(`${paymentMethod.type} is not supported.`); | ||
} | ||
|
||
const payload = this.payloadMapper.mapToPayload(data); | ||
const url = this.urlHelper.getOffsitePaymentUrl(); | ||
|
||
this.formPoster.postForm(url, payload, callback); | ||
} | ||
} |
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,52 @@ | ||
import { omitNil, toSnakeCase } from '../../common/utils'; | ||
|
||
export default class AddressMapper { | ||
/** | ||
* @returns {AddressMapper} | ||
*/ | ||
static create() { | ||
return new AddressMapper(); | ||
} | ||
|
||
/** | ||
* @param {PaymentRequestData} data | ||
* @returns {Object} | ||
*/ | ||
mapToBillingAddress(data) { | ||
return this.mapToAddress(data, 'billingAddress'); | ||
} | ||
|
||
/** | ||
* @param {PaymentRequestData} data | ||
* @returns {Object} | ||
*/ | ||
mapToShippingAddress(data) { | ||
return this.mapToAddress(data, 'shippingAddress'); | ||
} | ||
|
||
/** | ||
* @private | ||
* @param {PaymentRequestData} data | ||
* @param {string} addressKey | ||
* @returns {Object} | ||
*/ | ||
mapToAddress(data, addressKey) { | ||
const address = data[addressKey] || {}; | ||
const formattedAddressKey = toSnakeCase(addressKey); | ||
|
||
return omitNil({ | ||
[`${formattedAddressKey}_city`]: address.city, | ||
[`${formattedAddressKey}_company`]: address.company, | ||
[`${formattedAddressKey}_country_code`]: address.countryCode, | ||
[`${formattedAddressKey}_country`]: address.country, | ||
[`${formattedAddressKey}_first_name`]: address.firstName, | ||
[`${formattedAddressKey}_last_name`]: address.lastName, | ||
[`${formattedAddressKey}_phone`]: address.phone, | ||
[`${formattedAddressKey}_state_code`]: address.provinceCode, | ||
[`${formattedAddressKey}_state`]: address.province, | ||
[`${formattedAddressKey}_street_1`]: address.addressLine1, | ||
[`${formattedAddressKey}_street_2`]: address.addressLine2, | ||
[`${formattedAddressKey}_zip`]: address.postCode, | ||
}); | ||
} | ||
} |
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,30 @@ | ||
import { omitNil } from '../../common/utils'; | ||
|
||
export default class CustomerMapper { | ||
/** | ||
* @returns {CustomerMapper} | ||
*/ | ||
static create() { | ||
return new CustomerMapper(); | ||
} | ||
|
||
/** | ||
* @param {PaymentRequestData} data | ||
* @returns {Object} | ||
*/ | ||
mapToCustomer(data) { | ||
const { customer = {}, quoteMeta = {}, store = {} } = data; | ||
|
||
return omitNil({ | ||
customer_browser_info: navigator.userAgent, | ||
customer_email: customer.email, | ||
customer_first_name: customer.firstName, | ||
customer_geo_ip_country_code: quoteMeta.request ? quoteMeta.request.geoCountryCode : null, | ||
customer_last_name: customer.lastName, | ||
customer_locale: store.storeLanguage, | ||
customer_name: customer.name, | ||
customer_phone: customer.phoneNumber, | ||
customer_reference: customer.email, | ||
}); | ||
} | ||
} |
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,24 @@ | ||
import { omitNil } from '../../common/utils'; | ||
|
||
export default class MetaMapper { | ||
/** | ||
* @returns {MetaMapper} | ||
*/ | ||
static create() { | ||
return new MetaMapper(); | ||
} | ||
|
||
/** | ||
* @param {PaymentRequestData} data | ||
* @returns {Object} | ||
*/ | ||
mapToMeta(data) { | ||
const { source } = data; | ||
|
||
return omitNil({ | ||
meta_referrer: document.referrer, | ||
meta_source: source, | ||
meta_user_agent: navigator.userAgent, | ||
}); | ||
} | ||
} |
Oops, something went wrong.