-
Notifications
You must be signed in to change notification settings - Fork 139
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #701 from recurly/ideal
IDeal and Sofort
- Loading branch information
Showing
18 changed files
with
693 additions
and
5 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,204 @@ | ||
import Emitter from 'component-emitter'; | ||
import errors from './errors'; | ||
|
||
const debug = require('debug')('recurly:bank-redirect'); | ||
|
||
/** | ||
* Instantiation factory. | ||
* | ||
* @param {Object} options | ||
* @return {BankRedirect} | ||
*/ | ||
export function factory (options) { | ||
return new BankRedirect(Object.assign({}, options, { recurly: this })); | ||
} | ||
|
||
/** | ||
* Initializes a BankRedirect session. | ||
* | ||
* @param {Object} options | ||
* @param {Recurly} options.recurly | ||
* @constructor | ||
* @public | ||
*/ | ||
class BankRedirect extends Emitter { | ||
constructor (options) { | ||
debug('Creating new BankRedirect session'); | ||
|
||
super(); | ||
this.recurly = options.recurly; | ||
} | ||
|
||
/** | ||
* Fetch the banks. | ||
* @param {Object} data | ||
* @param {string} attachTo | ||
*/ | ||
loadBanks (data, attachTo) { | ||
debug('Load banks'); | ||
|
||
const errors = validateBankPayload(data); | ||
if (errors.length > 0) { | ||
return this.error('validation', { fields: errors }); | ||
} | ||
|
||
this.recurly.request.get({ | ||
route: '/bank_redirect/banks', | ||
data, | ||
done: (error, response) => { | ||
if (error) { | ||
return this.error('banks-error', { cause: error }); | ||
} | ||
|
||
if (attachTo) { | ||
attachListToSelect(attachTo, response.banks, 'issuer_id'); | ||
} | ||
this.emit('banks', response.banks); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Fetch the countries. | ||
* @param {Object} data | ||
* @param {string} attachTo | ||
*/ | ||
loadCountries (data, attachTo) { | ||
debug('Load countries'); | ||
|
||
const errors = validateCountriesPayload(data); | ||
if (errors.length > 0) { | ||
return this.error('validation', { fields: errors }); | ||
} | ||
|
||
const countries = [ | ||
{ id: 'AT', name: 'Austria' }, | ||
{ id: 'BE', name: 'Belgium' }, | ||
{ id: 'DE', name: 'Germany' }, | ||
{ id: 'IT', name: 'Italy' }, | ||
{ id: 'ES', name: 'Spain' }, | ||
{ id: 'NL', name: 'The Netherlands' } | ||
]; | ||
if (attachTo) { | ||
attachListToSelect(attachTo, countries, 'country_code'); | ||
} | ||
this.emit('countries', countries); | ||
} | ||
|
||
/** | ||
* Start the BankRedirect Payment Modal. | ||
* @param {Object} data | ||
*/ | ||
start (data) { | ||
debug('Start BankRedirect Payment Modal'); | ||
|
||
const errors = validatePayload(data); | ||
if (errors.length > 0) { | ||
return this.error('validation', { fields: errors }); | ||
} | ||
|
||
const frame = this.recurly.Frame({ | ||
height: 600, | ||
path: '/bank_redirect/start', | ||
payload: data | ||
}); | ||
frame.once('error', cause => this.error('bank-redirect-error', { cause })); | ||
frame.once('done', token => this.emit('token', token)); | ||
return frame; | ||
} | ||
|
||
error (...params) { | ||
const err = params[0] instanceof Error ? params[0] : errors(...params); | ||
this.emit('error', err); | ||
|
||
return err; | ||
} | ||
} | ||
|
||
function validatePayload (data) { | ||
const errors = validatePaymentMethodType(data); | ||
|
||
const validation = { | ||
ideal: validateIdealPayload, | ||
sofort: validateSofortPayload, | ||
}[data && data.payment_method_type]; | ||
if (validation) { | ||
errors.push(...validation(data)); | ||
} | ||
|
||
return errors; | ||
} | ||
|
||
function validateBankPayload (data) { | ||
return validatePaymentMethodType(data); | ||
} | ||
|
||
function validateCountriesPayload (data) { | ||
return validatePaymentMethodType(data); | ||
} | ||
|
||
function validatePaymentMethodType (data) { | ||
if (!data || !data.payment_method_type) { | ||
return ['payment_method_type cannot be blank']; | ||
} | ||
|
||
if (data.payment_method_type !== 'ideal' && data.payment_method_type !== 'sofort') { | ||
return ['invalid payment_method_type']; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
function validateIdealPayload (data) { | ||
const errors = []; | ||
|
||
if (!data || !data.issuer_id) { | ||
errors.push('issuer_id cannot be blank'); | ||
} | ||
|
||
if (!data || !data.invoice_uuid) { | ||
errors.push('invoice_uuid cannot be blank'); | ||
} | ||
|
||
return errors; | ||
} | ||
|
||
function validateSofortPayload (data) { | ||
const errors = []; | ||
|
||
if (!data || !data.country_code) { | ||
errors.push('country_code cannot be blank'); | ||
} | ||
|
||
if (!data || !data.invoice_uuid) { | ||
errors.push('invoice_uuid cannot be blank'); | ||
} | ||
|
||
return errors; | ||
} | ||
|
||
function attachListToSelect (selector, list, selectorId) { | ||
let $select = document.querySelector(selector); | ||
if (!$select) { | ||
return; | ||
} | ||
|
||
if ($select.tagName != 'SELECT') { | ||
const $container = $select; | ||
$select = document.createElement('select'); | ||
$select.id = selectorId; | ||
$select.setAttribute('name', selectorId); | ||
$container.appendChild($select); | ||
} | ||
|
||
while ($select.options.length > 0) { | ||
$select.remove(0); | ||
} | ||
|
||
for (const { id, name } of list) { | ||
const $option = document.createElement('option'); | ||
$option.appendChild(document.createTextNode(name)); | ||
$option.setAttribute('value', id); | ||
$select.appendChild($option); | ||
} | ||
} |
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
Binary file modified
BIN
+200 Bytes
(100%)
...desktop_firefox/BrowserStackFirefox/elements/card-element-pacifico-1280x973.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-35 Bytes
(100%)
...op_firefox/BrowserStackFirefox/elements/distinct-elements-pacifico-1280x973.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+195 Bytes
(100%)
...op_firefox/BrowserStackFirefox/hosted-fields/card-element-pacifico-1280x973.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
-30 Bytes
(100%)
...refox/BrowserStackFirefox/hosted-fields/distinct-elements-pacifico-1280x973.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+4 Bytes
(100%)
...ots/desktop_msedge/BrowserStackEdge/elements/card-element-pacifico-945x1020.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+22 Bytes
(100%)
...esktop_msedge/BrowserStackEdge/elements/distinct-elements-pacifico-945x1020.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+20 Bytes
(100%)
...esktop_msedge/BrowserStackEdge/hosted-fields/card-element-pacifico-945x1020.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified
BIN
+21 Bytes
(100%)
...p_msedge/BrowserStackEdge/hosted-fields/distinct-elements-pacifico-945x1020.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,28 @@ | ||
module.exports = function banks () { | ||
const payload = this.query; | ||
|
||
if (payload.payment_method_type === 'ideal') { | ||
if (payload.error) { | ||
return { | ||
error: { | ||
code: 'api-error', | ||
message: 'Api error', | ||
} | ||
}; | ||
} | ||
|
||
return { | ||
banks: [ | ||
{ id: 'bank1', name: 'Bank 1' }, | ||
{ id: 'bank2', name: 'Bank 2' }, | ||
] | ||
}; | ||
} | ||
|
||
return { | ||
error: { | ||
code: 'invalid-payment-method-type', | ||
message: 'Invalid payment method 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
Oops, something went wrong.