vigour-pay
Version:
Native store purchasing and web payment
109 lines (106 loc) • 3.11 kB
JavaScript
var Observable = require('vigour-js/lib/observable')
var _parseValue = Observable.prototype.parseValue
var TEMPLATED = /\{(.+?)\}/g
module.exports = new Observable({
owned: {
val: false,
on: {
data: {
condition (val, next, event) {
val = this.val
if (val) {
let owned = this
let product = owned.parent
let pay = product.parent.parent
let storeId = product.val
let responseId = product.responseId && product.responseId.val
if (storeId) {
pay._platform.emit('buy', {
id: storeId,
responseId: responseId,
cb: function bought (err, receipt) {
if (!err) {
owned.receipt.val = receipt
pay.emit('bought', {
label: product.key,
receipt: receipt
})
next()
} else {
owned.val = false
pay.emit('error', err)
owned._input = false
next(true)
}
}
})
} else {
pay.emit('error',
'buy failed: could not determine storeId of ' + product.key +
'with value ' + product._val
)
}
}
},
val: function () {}
}
},
receipt: false
},
on: {
value (data, event) {
var product = this
if (product._templated) {
// console.log('pop dat validateProducts?')
return
}
var val = product._input
if (val instanceof Observable) {
val = val.val
}
if (typeof val === 'string') {
let templateKey
while ((templateKey = TEMPLATED.exec(val))) {
if (!product._templated) {
product._templated = {}
}
templateKey = templateKey[1]
let pay = this.parent.parent
let has = pay.get(templateKey, {})
has.on(product)
product._templated[templateKey] = has
}
}
}
},
define: {
parseValue: function parseProductValue () {
var product = this
var val = product._input
if (val instanceof Observable) {
val = val.val
}
if (typeof val === 'string') {
let found
while ((found = TEMPLATED.exec(val))) {
let templateKey = found[1]
let templateVal = product._templated[templateKey]
templateVal = templateVal && templateVal.val
if (typeof templateVal !== 'string') {
TEMPLATED.lastIndex = 0
return false
}
let foundAt = found.index
val = val.slice(0, foundAt) +
templateVal +
val.slice(foundAt + found[0].length)
TEMPLATED.lastIndex = foundAt + templateVal.length
}
return val
} else {
return _parseValue.apply(this, arguments)
}
}
}
}).Constructor