Skip to content

Commit

Permalink
Send WorldPay emails to contact_email for AD renewals (#278)
Browse files Browse the repository at this point in the history
* Send WorldPay emails to contact_email for AD renewals

https://eaflood.atlassian.net/browse/WC-489

Users sometimes go through the assisted digital route to renew because they cannot access their account. When a user goes through WorldPay for payment, we send the account email through to WorldPay, which it then uses to send confirmation of the payment. If the user is AD because they can't access the account, this makes the confirmation meaningless.

So if a renewal is done via assisted digital, we should be passing the contact email to WorldPay, and not the account email.

* Add spec for AD renewals in WorldpayXmlService

The XML should use a different email address when the renewal is AD, so we need a new fixture and a new spec.

* WorldpayXmlService now takes a current_user when initialising

Improve the specs to handle this.

* Get different contact emails for digital and AD

We do this by checking if the current user's email matches the renewal account email. If they do, then the user who owns the registration is doing the renewal and it must be digital. If they don't, then another user is doing the renewal on their behalf and it must be assisted digital.
  • Loading branch information
irisfaraway committed Oct 8, 2018
1 parent 0f1f11b commit 4297b34
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 7 deletions.
2 changes: 1 addition & 1 deletion app/services/waste_carriers_engine/worldpay_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def prepare_params(params)
end

def send_request
xml_service = WorldpayXmlService.new(@transient_registration, @order)
xml_service = WorldpayXmlService.new(@transient_registration, @order, @current_user)
xml = xml_service.build_xml

Rails.logger.debug "Sending initial request to WorldPay"
Expand Down
13 changes: 11 additions & 2 deletions app/services/waste_carriers_engine/worldpay_xml_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

module WasteCarriersEngine
class WorldpayXmlService
def initialize(transient_registration, order)
def initialize(transient_registration, order, current_user)
@transient_registration = transient_registration
@order = order
@current_user = current_user
end

def build_xml
Expand Down Expand Up @@ -61,7 +62,7 @@ def build_payment_methods(xml)
end

def build_shopper(xml)
email = @transient_registration.account_email
email = get_email

xml.shopper do
xml.shopperEmailAddress email
Expand Down Expand Up @@ -99,5 +100,13 @@ def get_country_code(country)
return "GB" if country.nil?
country.alpha2
end

def get_email
if @current_user.email == @transient_registration.account_email
@transient_registration.account_email
else
@transient_registration.contact_email
end
end
end
end
4 changes: 4 additions & 0 deletions spec/factories/transient_registration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@
finance_details { build(:finance_details, balance: 0) }
end

trait :has_different_contact_email do
contact_email { "contact-foo@example.com" }
end

# Overseas registrations

trait :has_required_overseas_data do
Expand Down
30 changes: 30 additions & 0 deletions spec/fixtures/files/request_to_worldpay_ad.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?xml version="1.0"?>
<!DOCTYPE paymentService PUBLIC "-//WorldPay/DTD WorldPay PaymentService v1/EN" "http://dtd.worldpay.com/paymentService_v1.dtd">
<paymentService version="1.4" merchantCode="MERCHANTCODE">
<submit>
<order orderCode="1234567890">
<description>Your Waste Carrier Registration CBDU9999</description>
<amount currencyCode="GBP" value="10000" exponent="2"/>
<orderContent>Waste Carrier Registration renewal: CBDU9999 for Acme Waste</orderContent>
<paymentMethodMask>
<include code="VISA-SSL"/>
<include code="MAESTRO-SSL"/>
<include code="ECMC-SSL"/>
</paymentMethodMask>
<shopper>
<shopperEmailAddress>contact-foo@example.com</shopperEmailAddress>
</shopper>
<billingAddress>
<address>
<firstName>Jane</firstName>
<lastName>Doe</lastName>
<address1>42 Foo Gardens</address1>
<address2/>
<postalCode>FA1 1KE</postalCode>
<city>Baz City</city>
<countryCode>SK</countryCode>
</address>
</billingAddress>
</order>
</submit>
</paymentService>
27 changes: 23 additions & 4 deletions spec/services/waste_carriers_engine/worldpay_xml_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ module WasteCarriersEngine
let(:transient_registration) do
create(:transient_registration,
:has_required_data,
:has_different_contact_email,
:has_overseas_addresses,
:has_finance_details,
temp_cards: 0)
end
let(:order) { transient_registration.finance_details.orders.first }
let(:worldpay_xml_service) { WorldpayXmlService.new(transient_registration, order) }
let(:current_user) { build(:user) }
let(:worldpay_xml_service) { WorldpayXmlService.new(transient_registration, order, current_user) }

before do
# Set a specific reg_identifier so we can match our XML
Expand All @@ -25,9 +27,26 @@ module WasteCarriersEngine
end

describe "build_xml" do
it "returns correctly-formatted XML" do
xml = File.read("./spec/fixtures/files/request_to_worldpay.xml")
expect(worldpay_xml_service.build_xml).to eq(xml)
context "when it's a digital registration" do
before do
current_user.email = transient_registration.account_email
end

it "returns correctly-formatted XML" do
xml = File.read("./spec/fixtures/files/request_to_worldpay.xml")
expect(worldpay_xml_service.build_xml).to eq(xml)
end
end

context "when it's an assisted digital registration" do
before do
current_user.email = "not-the-same-account@example.com"
end

it "returns correctly-formatted XML" do
xml = File.read("./spec/fixtures/files/request_to_worldpay_ad.xml")
expect(worldpay_xml_service.build_xml).to eq(xml)
end
end
end
end
Expand Down

0 comments on commit 4297b34

Please sign in to comment.