Skip to content

Commit fe382bc

Browse files
committed
Use postmessage api to update attributes.
1 parent dba0261 commit fe382bc

File tree

5 files changed

+40
-32
lines changed

5 files changed

+40
-32
lines changed

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
"karma-jasmine": "^1.1.2",
2929
"karma-phantomjs-launcher": "^1.0.4",
3030
"karma-webpack": "^3.0.5",
31-
"qs": "^6.5.2",
3231
"raw-loader": "^0.5.1",
3332
"rimraf": "^2.6.2",
3433
"tachyons": "^4.11.1",

readme.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,15 @@ The following properties can be set on the component:
5959
| `editable` | `Boolean` | `false` |
6060
| `currency` | `String` | `'USD'` |
6161
| `label` | `String` | `null` |
62+
| `successMessage` | `String` | `null` |
6263
| `opReturn` | `String` | `null` |
6364
| `outputs` | `Array` | `[]` |
6465
| `clientIdentifier` | `String` | `null` |
6566
| `buttonId` | `String` or `Number` | `null` |
6667
| `buttonData` | `String` or `Number` | `null` |
6768
| `type` | `String` - `buy` or `tip` | `'buy'` |
6869
| `devMode` | `Boolean` | `false` |
70+
| `disabled` | `Boolean` | `false` |
6971

7072
An array of `outputs` can be set **instead of the `to`, `amount` and `currency` properties**. Each output output object has the following parameters:
7173

src/components/MoneyButton.vue

Lines changed: 36 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
</template>
2424

2525
<script>
26-
import qs from 'qs'
2726
import config from 'config'
2827
import Popup from 'components/Popup.vue'
2928
import LoaderIcon from 'components/Loader.vue'
@@ -35,13 +34,15 @@ export default {
3534
editable: { type: Boolean, default: undefined },
3635
currency: { type: String, default: 'USD' },
3736
label: { type: String, required: true },
37+
successMessage: String,
3838
opReturn: String,
3939
outputs: { type: Array, default: () => [] },
4040
clientIdentifier: String,
4141
buttonId: true,
4242
buttonData: true,
4343
type: { type: String, default: 'buy' },
44-
devMode: { type: Boolean, default: undefined }
44+
devMode: { type: Boolean, default: undefined },
45+
disabled: { type: Boolean, default: undefined }
4546
},
4647
4748
data() {
@@ -51,64 +52,70 @@ export default {
5152
size: {
5253
width: '280px',
5354
height: '50px'
54-
}
55+
},
56+
iframeSrc: config.iframeUrl.concat('?', 'format=postmessage')
5557
}
5658
},
5759
5860
mounted() {
59-
window.addEventListener('message', this.handleMessage, false);
61+
window.addEventListener('message', this.handleMessage, false)
6062
},
6163
6264
destroyed() {
63-
window.removeEventListener('message', this.handleMessage, false);
64-
},
65-
66-
watch: {
67-
iframeSrc(val) {
68-
this.loading = true;
69-
}
65+
window.removeEventListener('message', this.handleMessage, false)
7066
},
7167
7268
computed: {
73-
iframeSrc() {
74-
return config.iframeUrl.concat('?', qs.stringify(this.queryParams));
75-
},
76-
7769
queryParams() {
7870
return {
79-
to: this.to,
80-
amt: this.amount,
81-
edt: this.editable,
82-
ccy: this.outputs.length ? undefined : this.currency,
83-
lbl: this.label,
84-
opd: this.opReturn,
85-
os: this.outputs.length ? JSON.stringify(this.outputs) : undefined,
86-
cid: this.clientIdentifier,
87-
bid: this.buttonId,
88-
bdt: this.buttonData,
89-
t: this.type,
90-
dev: this.devMode
71+
to: this.to,
72+
amt: this.amount,
73+
edt: this.editable,
74+
ccy: this.outputs.length ? undefined : this.currency,
75+
lbl: this.label,
76+
scsmsg: this.successMessage,
77+
opd: this.opReturn,
78+
os: this.outputs.length ? JSON.stringify(this.outputs) : undefined,
79+
cid: this.clientIdentifier,
80+
bid: this.buttonId,
81+
bdt: this.buttonData,
82+
t: this.type,
83+
dev: this.devMode,
84+
dsbd: this.disabled
9185
}
9286
},
9387
},
9488
89+
watch: {
90+
queryParams(params) {
91+
this.postMessage('attributes-updated', params)
92+
}
93+
},
94+
9595
methods: {
9696
loaded(e) {
97+
this.postMessage('attributes-updated', this.queryParams)
9798
setTimeout(() => { this.loading = false }, 2000)
9899
},
99100
101+
postMessage(topic, payload) {
102+
this.$refs.iframe.contentWindow.postMessage({
103+
v1: { topic, payload }
104+
}, config.iframeUrl)
105+
},
106+
100107
handleMessage(e) {
101108
if ( this.$refs['iframe'] && e.source === this.$refs['iframe'].contentWindow ) {
102109
const { error, size, payment, popup } = e.data;
103110
104111
// Check valid iframe origin
105112
if ( e.origin !== config.iframeOrigin) {
106-
console.log(`vue-money-button: postMessage: wrong origin: ${e.origin} should be ${config.iframeOrigin}`);
113+
console.log(`vue-money-button: postMessage: wrong origin: ${e.origin} should be ${config.iframeOrigin}`)
107114
return
108115
}
109116
110117
if (process.env.NODE_ENV === 'development')
111-
console.log('vue-money-button: Received message', e.data);
118+
console.log('vue-money-button: Received message', e.data)
112119
113120
// If popup
114121
if ( popup ) {

src/demo/App.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
to="74"
3535
:amount="amount"
3636
:label="label"
37-
type="tip"
37+
success-message="Yehah!"
3838

3939
@payment="onPayment"
4040
@error="onError"

yarn.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4079,7 +4079,7 @@ qs@6.5.1:
40794079
version "6.5.1"
40804080
resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.1.tgz#349cdf6eef89ec45c12d7d5eb3fc0c870343a6d8"
40814081

4082-
qs@6.5.2, qs@^6.5.2, qs@~6.5.2:
4082+
qs@6.5.2, qs@~6.5.2:
40834083
version "6.5.2"
40844084
resolved "https://registry.yarnpkg.com/qs/-/qs-6.5.2.tgz#cb3ae806e8740444584ef154ce8ee98d403f3e36"
40854085

0 commit comments

Comments
 (0)