solidus_mp 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e90e231c71dc6382c6d05974fd3e07dd33dfe064eb163637799a76c504600d4d
4
- data.tar.gz: 192d0abde83390e074d79f9e310d51c409916b6f0b9fe43fbdc84d5ee0d2ad0b
3
+ metadata.gz: a92f139bc94952294898e8429b8a9f1108383bdcc7f133bff16bbd48729c8afa
4
+ data.tar.gz: f0d4b5c39e59879ea21b891cbe5301401eff848527eea9a6bb34fe799d409318
5
5
  SHA512:
6
- metadata.gz: fecd21500ed0b3a8ed112e66829484f9c89602bc372d2f7e4237230b6ff57439ae8b7ff5b2f67559d4ab407b4c713c538c646a2c10310983696a94e14378f702
7
- data.tar.gz: 75fddd0fd67dce2cbeff6cdb06417ddb32631445e6f91945511e78163ce734bbafa4042cd6dc5e131beaf85ea3c991ca50379e1c1ea14ddfac137b2ec57308aa
6
+ metadata.gz: 07d84ca6d807d13eb35c7802aa46b8187b31e5c02b09e62dced1f024f91d56e0553ecd962e44a322b1ffd9ffdb366883d677a8c0d8f24dad55c96837ab577745
7
+ data.tar.gz: 9364203529ea33840a93a6eb85440f150c0c1c41543bbd77c8744e7ef11eefdf7b6badbce57a121652693309e54eacae133cd0d8e919e9cc3d54066b6777d7e2
data/README.md CHANGED
@@ -0,0 +1,30 @@
1
+ # Solidus Bling
2
+
3
+ [![CircleCI](https://circleci.com/gh/solidusio-contrib/solidus_bling.svg?style=shield)](https://circleci.com/gh/solidusio-contrib/solidus_bling)
4
+ [![codecov](https://codecov.io/gh/solidusio-contrib/solidus_bling/branch/master/graph/badge.svg)](https://codecov.io/gh/solidusio-contrib/solidus_bling)
5
+
6
+ <!-- Explain what your extension does. -->
7
+
8
+ ## Installation
9
+
10
+ Add solidus_bling to your Gemfile:
11
+
12
+ ```ruby
13
+ gem 'solidus_bling'
14
+ ```
15
+
16
+ Bundle your dependencies and run the installation generator:
17
+
18
+ ```shell
19
+ bin/rails generate solidus_bling:install
20
+ ```
21
+
22
+ ```shell
23
+ bin/rails assets:precompile
24
+ ```
25
+
26
+ ## Usage
27
+
28
+ ## License
29
+
30
+ Copyright (c) 2023 ulysses-bull, released under the New BSD License.
@@ -0,0 +1 @@
1
+ json.call(payment_source, :id, :external_id)
@@ -0,0 +1 @@
1
+ json.call(payment_source, :id, :external_id)
@@ -1,220 +1,220 @@
1
- import { Controller } from "@hotwired/stimulus"
2
- import { loadMercadoPago } from "@mercadopago/sdk-js"
3
-
4
- // Connects to data-controller="credit-card"
5
- export default class extends Controller {
6
-
7
- static values = { publicKey: String }
8
- static targets = ["formCardNumber", "formExpirationDate", "formSecurityCode", "formIssuer", "formInstallments", "paymentMethodId", "paymentMethodThumbnail", "formDocumentType", "formCardHolderName"]
9
-
10
- // Anytime the controller is connected to the DOM
11
- async connect() {
12
- await loadMercadoPago()
13
- this.mp = new window.MercadoPago('TEST-dc17baa6-3344-4382-af3f-15d37f3ce4ae')
14
- // estilos dos iframes
15
- const styles = {
16
- style: {
17
- height: "1.5rem",
18
- padding: "0px",
19
- margin: "0px",
20
- fontFamily: "Inter",
21
- placeholderColor: "#9C99AF",
22
- fontSize: "1rem"
23
- },
24
- customFonts: [
25
- {
26
- src: "https://rsms.me/inter/inter.css",
27
- },
28
- ]
29
- }
30
-
31
- // iframe numero cartao
32
- const cardNumberElement = this.createIframe('cardNumber', "Número do cartão", styles, 'formCardNumber')
33
- const expirationDateElement = this.createIframe('expirationDate', "MM/AA", styles, 'formExpirationDate')
34
- const securityCodeElement = this.createIframe('securityCode', "Código de segurança", styles, 'formSecurityCode')
35
- let currentBin
36
- cardNumberElement.on('binChange', async (data) => {
37
- const { bin } = data
38
- try {
39
- if (!bin && this.paymentMethodIdTarget.value) {
40
- this.clearSelectsAndSetPlaceholders("Banco emissor", "Parcelas")
41
- this.paymentMethodIdTarget.value = ""
42
- }
43
-
44
- if (bin && bin !== currentBin) {
45
- const { results } = await this.mp.getPaymentMethods({ bin })
46
- const paymentMethod = results[0]
47
- this.paymentMethodIdTarget.value = paymentMethod.id
48
- this.paymentMethodThumbnailTarget.setAttribute('src', paymentMethod.secure_thumbnail)
49
- this.updatePCIFieldsSettings(paymentMethod, cardNumberElement, securityCodeElement)
50
- this.updateIssuer(paymentMethod, bin)
51
- this.updateInstallments(bin)
52
- }
53
-
54
- currentBin = bin
55
- } catch (e) {
56
- console.error('error getting payment methods: ', e)
57
- }
58
- })
59
-
60
- cardNumberElement.on('focus', this.onFocus(this.formCardNumberTarget))
61
- cardNumberElement.on('blur', this.onBlur(this.formCardNumberTarget))
62
-
63
- expirationDateElement.on('focus', this.onFocus(this.formExpirationDateTarget))
64
- expirationDateElement.on('blur', this.onBlur(this.formExpirationDateTarget))
65
-
66
- securityCodeElement.on('focus', this.onFocus(this.formSecurityCodeTarget))
67
- securityCodeElement.on('blur', this.onBlur(this.formSecurityCodeTarget))
68
- await this.getIdentificationTypes()
69
-
70
- document.getElementById('checkout_form_payment').addEventListener('submit', e => {
71
- if (this.formCardHolderNameTarget.value === "") return
72
- this.createCardToken(e, this.mp)
73
- })
74
-
75
- }
76
-
77
- createIframe(inputName, placeholder, Styles, divId){
78
- const element = this.mp.fields.create(inputName, {
79
- placeholder: placeholder,
80
- ...Styles
81
- }).mount(divId)
82
- return element
83
- }
84
-
85
- onFocus(divElement) {
86
- return function (event) {
87
- divElement.setAttribute('data-focus', 'true')
88
- }
89
- }
90
-
91
- onBlur(divElement) {
92
- return function (event) {
93
- divElement.removeAttribute('data-focus')
94
- }
95
- }
96
-
97
- async getIdentificationTypes() {
98
- try {
99
- const identificationTypes = await this.mp.getIdentificationTypes()
100
-
101
- this.createSelectOptions(this.formDocumentTypeTarget, identificationTypes)
102
- } catch (e) {
103
- return console.error('Error getting identificationTypes: ', e)
104
- }
105
- }
106
-
107
- createSelectOptions(elem, options, labelsAndKeys = { label: "name", value: "id" }) {
108
- const { label, value } = labelsAndKeys
109
-
110
- elem.options.length = 0
111
-
112
- const tempOptions = document.createDocumentFragment()
113
-
114
- options.forEach(option => {
115
- const optValue = option[value]
116
- const optLabel = option[label]
117
-
118
- const opt = document.createElement('option')
119
- opt.value = optValue
120
- opt.textContent = optLabel
121
-
122
- tempOptions.appendChild(opt)
123
- })
124
-
125
- elem.appendChild(tempOptions)
126
- }
127
-
128
- clearSelectsAndSetPlaceholders(issuerPlaceholder, installmentsPlaceholder) {
129
- clearHTMLSelectChildrenFrom(this.formIssuerTarget)
130
- createSelectElementPlaceholder(this.formIssuerTarget, issuerPlaceholder)
131
-
132
- clearHTMLSelectChildrenFrom(this.formInstallmentsTarget)
133
- createSelectElementPlaceholder(this.formInstallmentsTarget, installmentsPlaceholder)
134
- }
135
-
136
- clearHTMLSelectChildrenFrom(element) {
137
- const currOptions = [...element.children]
138
- currOptions.forEach(child => child.remove())
139
- }
140
-
141
- createSelectElementPlaceholder(element, placeholder) {
142
- const optionElement = document.createElement('option')
143
- optionElement.textContent = placeholder
144
- optionElement.setAttribute('selected', "")
145
- optionElement.setAttribute('disabled', "")
146
-
147
- element.appendChild(optionElement)
148
- }
149
-
150
- updatePCIFieldsSettings(paymentMethod, cardNumberElement, securityCodeElement) {
151
- const { settings } = paymentMethod
152
-
153
- const cardNumberSettings = settings[0].card_number
154
- cardNumberElement.update({
155
- settings: cardNumberSettings
156
- })
157
-
158
- const securityCodeSettings = settings[0].security_code
159
- securityCodeElement.update({
160
- settings: securityCodeSettings
161
- })
162
- }
163
-
164
- async updateIssuer(paymentMethod, bin) {
165
- const { additional_info_needed, issuer } = paymentMethod
166
- let issuerOptions = [issuer]
167
-
168
- if (additional_info_needed.includes('issuer_id')) {
169
- issuerOptions = await getIssuers(paymentMethod, bin)
170
- }
171
-
172
- this.createSelectOptions(this.formIssuerTarget, issuerOptions)
173
- }
174
-
175
- async getIssuers(paymentMethod, bin) {
176
- try {
177
- const { id: paymentMethodId } = paymentMethod
178
- return await this.mp.getIssuers({ paymentMethodId, bin })
179
- } catch (e) {
180
- console.error('error getting issuers: ', e)
181
- }
182
- }
183
-
184
- async updateInstallments(bin) {
185
- try {
186
- const installments = await this.mp.getInstallments({
187
- amount: document.getElementById('transactionAmount').value,
188
- bin,
189
- paymentTypeId: 'credit_card'
190
- });
191
- const installmentOptions = installments[0].payer_costs;
192
- const installmentOptionsKeys = { label: 'recommended_message', value: 'installments' };
193
- this.createSelectOptions(this.formInstallmentsTarget, installmentOptions, installmentOptionsKeys);
194
- } catch (error) {
195
- console.error('error getting installments: ', error)
196
- }
197
- }
198
-
199
- async createCardToken(event, mp) {
200
- try {
201
- const tokenElement = document.getElementById('token');
202
- if (!tokenElement.value) {
203
- const formElement = document.getElementById('checkout_form_payment');
204
- event.preventDefault();
205
- const token = await mp.fields.createCardToken({
206
- cardholderName: document.getElementById('formCardHolderName').value,
207
- identificationType: document.getElementById('formDocumentType').value,
208
- identificationNumber: document.getElementById('tax_id').value,
209
- });
210
- tokenElement.value = token.id;
211
- formElement.submit();
212
- } else {
213
- formElement.submit();
214
- }
215
- } catch (e) {
216
- console.error('error creating card token: ', e)
217
- }
218
- }
219
-
220
- }
1
+ import { Controller } from "@hotwired/stimulus"
2
+ import { loadMercadoPago } from "@mercadopago/sdk-js"
3
+
4
+ // Connects to data-controller="credit-card"
5
+ export default class extends Controller {
6
+
7
+ static values = { publicKey: String }
8
+ static targets = ["formCardNumber", "formExpirationDate", "formSecurityCode", "formIssuer", "formInstallments", "paymentMethodId", "paymentMethodThumbnail", "formDocumentType", "formCardHolderName"]
9
+
10
+ // Anytime the controller is connected to the DOM
11
+ async connect() {
12
+ await loadMercadoPago()
13
+ this.mp = new window.MercadoPago(this.publicKeyValue)
14
+ // estilos dos iframes
15
+ const styles = {
16
+ style: {
17
+ height: "1.5rem",
18
+ padding: "0px",
19
+ margin: "0px",
20
+ fontFamily: "Inter",
21
+ placeholderColor: "#9C99AF",
22
+ fontSize: "1rem"
23
+ },
24
+ customFonts: [
25
+ {
26
+ src: "https://rsms.me/inter/inter.css",
27
+ },
28
+ ]
29
+ }
30
+
31
+ // iframe numero cartao
32
+ const cardNumberElement = this.createIframe('cardNumber', "Número do cartão", styles, 'formCardNumber')
33
+ const expirationDateElement = this.createIframe('expirationDate', "MM/AA", styles, 'formExpirationDate')
34
+ const securityCodeElement = this.createIframe('securityCode', "Código de segurança", styles, 'formSecurityCode')
35
+ let currentBin
36
+ cardNumberElement.on('binChange', async (data) => {
37
+ const { bin } = data
38
+ try {
39
+ if (!bin && this.paymentMethodIdTarget.value) {
40
+ this.clearSelectsAndSetPlaceholders("Banco emissor", "Parcelas")
41
+ this.paymentMethodIdTarget.value = ""
42
+ }
43
+
44
+ if (bin && bin !== currentBin) {
45
+ const { results } = await this.mp.getPaymentMethods({ bin })
46
+ const paymentMethod = results[0]
47
+ this.paymentMethodIdTarget.value = paymentMethod.id
48
+ this.paymentMethodThumbnailTarget.setAttribute('src', paymentMethod.secure_thumbnail)
49
+ this.updatePCIFieldsSettings(paymentMethod, cardNumberElement, securityCodeElement)
50
+ this.updateIssuer(paymentMethod, bin)
51
+ this.updateInstallments(bin)
52
+ }
53
+
54
+ currentBin = bin
55
+ } catch (e) {
56
+ console.error('error getting payment methods: ', e)
57
+ }
58
+ })
59
+
60
+ cardNumberElement.on('focus', this.onFocus(this.formCardNumberTarget))
61
+ cardNumberElement.on('blur', this.onBlur(this.formCardNumberTarget))
62
+
63
+ expirationDateElement.on('focus', this.onFocus(this.formExpirationDateTarget))
64
+ expirationDateElement.on('blur', this.onBlur(this.formExpirationDateTarget))
65
+
66
+ securityCodeElement.on('focus', this.onFocus(this.formSecurityCodeTarget))
67
+ securityCodeElement.on('blur', this.onBlur(this.formSecurityCodeTarget))
68
+ await this.getIdentificationTypes()
69
+
70
+ document.getElementById('checkout_form_payment').addEventListener('submit', e => {
71
+ if (this.formCardHolderNameTarget.value === "") return
72
+ this.createCardToken(e, this.mp)
73
+ })
74
+
75
+ }
76
+
77
+ createIframe(inputName, placeholder, Styles, divId){
78
+ const element = this.mp.fields.create(inputName, {
79
+ placeholder: placeholder,
80
+ ...Styles
81
+ }).mount(divId)
82
+ return element
83
+ }
84
+
85
+ onFocus(divElement) {
86
+ return function (event) {
87
+ divElement.setAttribute('data-focus', 'true')
88
+ }
89
+ }
90
+
91
+ onBlur(divElement) {
92
+ return function (event) {
93
+ divElement.removeAttribute('data-focus')
94
+ }
95
+ }
96
+
97
+ async getIdentificationTypes() {
98
+ try {
99
+ const identificationTypes = await this.mp.getIdentificationTypes()
100
+
101
+ this.createSelectOptions(this.formDocumentTypeTarget, identificationTypes)
102
+ } catch (e) {
103
+ return console.error('Error getting identificationTypes: ', e)
104
+ }
105
+ }
106
+
107
+ createSelectOptions(elem, options, labelsAndKeys = { label: "name", value: "id" }) {
108
+ const { label, value } = labelsAndKeys
109
+
110
+ elem.options.length = 0
111
+
112
+ const tempOptions = document.createDocumentFragment()
113
+
114
+ options.forEach(option => {
115
+ const optValue = option[value]
116
+ const optLabel = option[label]
117
+
118
+ const opt = document.createElement('option')
119
+ opt.value = optValue
120
+ opt.textContent = optLabel
121
+
122
+ tempOptions.appendChild(opt)
123
+ })
124
+
125
+ elem.appendChild(tempOptions)
126
+ }
127
+
128
+ clearSelectsAndSetPlaceholders(issuerPlaceholder, installmentsPlaceholder) {
129
+ clearHTMLSelectChildrenFrom(this.formIssuerTarget)
130
+ createSelectElementPlaceholder(this.formIssuerTarget, issuerPlaceholder)
131
+
132
+ clearHTMLSelectChildrenFrom(this.formInstallmentsTarget)
133
+ createSelectElementPlaceholder(this.formInstallmentsTarget, installmentsPlaceholder)
134
+ }
135
+
136
+ clearHTMLSelectChildrenFrom(element) {
137
+ const currOptions = [...element.children]
138
+ currOptions.forEach(child => child.remove())
139
+ }
140
+
141
+ createSelectElementPlaceholder(element, placeholder) {
142
+ const optionElement = document.createElement('option')
143
+ optionElement.textContent = placeholder
144
+ optionElement.setAttribute('selected', "")
145
+ optionElement.setAttribute('disabled', "")
146
+
147
+ element.appendChild(optionElement)
148
+ }
149
+
150
+ updatePCIFieldsSettings(paymentMethod, cardNumberElement, securityCodeElement) {
151
+ const { settings } = paymentMethod
152
+
153
+ const cardNumberSettings = settings[0].card_number
154
+ cardNumberElement.update({
155
+ settings: cardNumberSettings
156
+ })
157
+
158
+ const securityCodeSettings = settings[0].security_code
159
+ securityCodeElement.update({
160
+ settings: securityCodeSettings
161
+ })
162
+ }
163
+
164
+ async updateIssuer(paymentMethod, bin) {
165
+ const { additional_info_needed, issuer } = paymentMethod
166
+ let issuerOptions = [issuer]
167
+
168
+ if (additional_info_needed.includes('issuer_id')) {
169
+ issuerOptions = await getIssuers(paymentMethod, bin)
170
+ }
171
+
172
+ this.createSelectOptions(this.formIssuerTarget, issuerOptions)
173
+ }
174
+
175
+ async getIssuers(paymentMethod, bin) {
176
+ try {
177
+ const { id: paymentMethodId } = paymentMethod
178
+ return await this.mp.getIssuers({ paymentMethodId, bin })
179
+ } catch (e) {
180
+ console.error('error getting issuers: ', e)
181
+ }
182
+ }
183
+
184
+ async updateInstallments(bin) {
185
+ try {
186
+ const installments = await this.mp.getInstallments({
187
+ amount: document.getElementById('transactionAmount').value,
188
+ bin,
189
+ paymentTypeId: 'credit_card'
190
+ });
191
+ const installmentOptions = installments[0].payer_costs;
192
+ const installmentOptionsKeys = { label: 'recommended_message', value: 'installments' };
193
+ this.createSelectOptions(this.formInstallmentsTarget, installmentOptions, installmentOptionsKeys);
194
+ } catch (error) {
195
+ console.error('error getting installments: ', error)
196
+ }
197
+ }
198
+
199
+ async createCardToken(event, mp) {
200
+ try {
201
+ const tokenElement = document.getElementById('token');
202
+ if (!tokenElement.value) {
203
+ const formElement = document.getElementById('checkout_form_payment');
204
+ event.preventDefault();
205
+ const token = await mp.fields.createCardToken({
206
+ cardholderName: document.getElementById('formCardHolderName').value,
207
+ identificationType: document.getElementById('formDocumentType').value,
208
+ identificationNumber: document.getElementById('tax_id').value,
209
+ });
210
+ tokenElement.value = token.id;
211
+ formElement.submit();
212
+ } else {
213
+ formElement.submit();
214
+ }
215
+ } catch (e) {
216
+ console.error('error creating card token: ', e)
217
+ }
218
+ }
219
+
220
+ }
@@ -1,4 +1,4 @@
1
- <div data-controller="credit-card" >
1
+ <div data-controller="credit-card" data-credit-card-public-key-value="<%= payment_method.preferences[:public_key] %>" >
2
2
 
3
3
  <!-- Numero do cartao -->
4
4
  <div class="relative mt-2 flex items-center">
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SolidusMp
4
- VERSION = '0.1.0'
4
+ VERSION = '0.1.2'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: solidus_mp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - caio
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-12-05 00:00:00.000000000 Z
11
+ date: 2023-12-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: solidus_core
@@ -94,8 +94,12 @@ files:
94
94
  - app/models/solidus_mp/mp_card.rb
95
95
  - app/models/solidus_mp/mp_pix.rb
96
96
  - app/models/solidus_mp/pix_payment_source.rb
97
+ - app/views/spree/admin/payments/source_forms/_mercado_pago_card.html.erb
98
+ - app/views/spree/admin/payments/source_forms/_mercado_pago_pix.html.erb
97
99
  - app/views/spree/admin/payments/source_views/_mercado_pago_card.html.erb
98
100
  - app/views/spree/admin/payments/source_views/_mercado_pago_pix.html.erb
101
+ - app/views/spree/api/payments/source_views/_mercado_pago_card.json.jbuilder
102
+ - app/views/spree/api/payments/source_views/_mercado_pago_pix.json.jbuilder
99
103
  - bin/console
100
104
  - bin/rails
101
105
  - bin/rails-engine
@@ -149,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
149
153
  - !ruby/object:Gem::Version
150
154
  version: '0'
151
155
  requirements: []
152
- rubygems_version: 3.4.6
156
+ rubygems_version: 3.4.19
153
157
  signing_key:
154
158
  specification_version: 4
155
159
  summary: ''