Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add device data collection #116

Merged
merged 1 commit into from
Mar 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions app/models/solidus_braintree/gateway.rb
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,10 @@ def transaction_options(source, options, submit_for_settlement: false)
params[:payment_method_nonce] = source.nonce
end

if source&.device_data
params[:device_data] = source.device_data
end

if source.paypal?
params[:shipping] = braintree_shipping_address(options)
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddDeviceDataToBraintreeSources < ActiveRecord::Migration[5.1]
def change
add_column :solidus_paypal_braintree_sources, :device_data, :string
gsmendoza marked this conversation as resolved.
Show resolved Hide resolved
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ $(function() {

if ($field.is(":visible") && $field.is(":enabled") && !$field.data("submitting")) {
var $nonce = $("#payment_method_nonce", $field);

var $deviceData = $("#device_data", $field);
if ($nonce.length > 0 && $nonce.val() === "") {
var client = braintreeForm._merchantConfigurationOptions._solidusClient;

Expand All @@ -56,6 +56,8 @@ $(function() {
}

$nonce.val(payload.nonce);

$deviceData.val(client._dataCollectorInstance.deviceData);

if (!client.useThreeDSecure) {
$paymentForm.submit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SolidusBraintree.HostedForm.prototype.initialize = function() {
this.client = SolidusBraintree.createClient({
paymentMethodId: this.paymentMethodId,
useThreeDSecure: (typeof(window.threeDSecureOptions) !== 'undefined'),
useDataCollector: true,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SyborgStudios I removed the IS_FRONTEND const since we now have separate hosted_form JS files for frontend and backend.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome. I was not happy with it anyways.

});

return this.client.initialize().
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
<div class="clear"></div>
<input type="hidden" name="<%= prefix %>[payment_type]" value="<%= SolidusBraintree::Source::CREDIT_CARD %>">
<input type="hidden" id="payment_method_nonce" name="<%= prefix %>[nonce]">
<input type="hidden" id="device_data" name="<%= prefix %>[device_data]">
</div>


Expand Down
5 changes: 4 additions & 1 deletion lib/solidus_braintree/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ class Engine < Rails::Engine
config.to_prepare do
app.config.spree.payment_methods << SolidusBraintree::Gateway
SolidusBraintree::Gateway.allowed_admin_form_preference_types.push(:preference_select).uniq!
::Spree::PermittedAttributes.source_attributes.concat([:nonce, :payment_type, :paypal_funding_source]).uniq!

::Spree::PermittedAttributes.source_attributes.concat(
[:nonce, :payment_type, :paypal_funding_source, :device_data]
).uniq!
end
end

Expand Down
34 changes: 33 additions & 1 deletion spec/models/solidus_braintree/gateway_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
nonce: 'fake-valid-nonce',
user: user,
payment_type: payment_type,
payment_method: gateway
payment_method: gateway,
device_data: 'fake-device-data'
)
end

Expand Down Expand Up @@ -220,6 +221,37 @@
expect(authorize.message).to eq 'authorized'
expect(authorize.authorization).to be_present
end

context 'with available device data' do
it 'passes the device data as a parameter in the request' do
expect_any_instance_of(Braintree::TransactionGateway).
to receive(:sale).
with(hash_including({ device_data: "fake-device-data" })).and_call_original
authorize
end
end

context 'without device_data' do
let(:source) do
SolidusBraintree::Source.create!(
nonce: 'fake-valid-nonce',
user: user,
payment_type: payment_type,
payment_method: gateway
)
end

before do
allow_any_instance_of(Braintree::TransactionGateway).to receive(:sale).and_call_original
end

it 'does not pass any device data in the request' do
expect_any_instance_of(Braintree::TransactionGateway)
.not_to receive(:sale).with(hash_including({ device_data: "" }))

authorize
end
end
end

context 'with different merchant account for currency', vcr: {
Expand Down