Просмотр исходного кода

Generate address xml from object.

Shane Kilkelly 10 лет назад
Родитель
Сommit
72c73809f6

+ 10 - 10
services/web/app/coffee/Features/Subscription/RecurlyWrapper.coffee

@@ -9,6 +9,15 @@ Async = require('async')
 module.exports = RecurlyWrapper =
 	apiUrl : "https://api.recurly.com/v2"
 
+	_addressToXml: (address) ->
+		allowedKeys = ['address1', 'address2', 'city', 'country', 'state', 'zip', 'postal_code']
+		resultString = "<billing_info>\n"
+		for k, v of address
+			if v and (k in allowedKeys)
+				resultString += "<#{k}#{if k == 'address2' then ' nil="nil"' else ''}>#{v || ''}</#{k}>\n"
+		resultString += "</billing_info>\n"
+		return resultString
+
 	_createPaypalSubscription: (user, subscriptionDetails, recurly_token_id, callback) ->
 		logger.log {user_id: user._id, recurly_token_id}, "starting process of creating paypal subscription"
 		Async.waterfall([
@@ -108,16 +117,7 @@ module.exports = RecurlyWrapper =
 					address = subscriptionDetails.address
 					if !address
 						return next(new Error('no address in subscriptionDetails at setAddress stage'))
-					requestBody = """
-					<billing_info>
-						<address1>#{address.address1}</address1>
-						<address2 nil="nil">#{address.address2}</address2>
-						<city>#{address.city || ''}</city>
-						<state>#{address.state || ''}</state>
-						<zip>#{address.zip || ''}</zip>
-						<country>#{address.country}</country>
-					</billing_info>
-					"""
+					requestBody = RecurlyWrapper._addressToXml(address)
 					RecurlyWrapper.apiRequest({
 						url: "accounts/#{accountCode}/billing_info"
 						method: "PUT"

+ 6 - 5
services/web/public/coffee/main/new-subscription.coffee

@@ -117,11 +117,12 @@ define [
 						coupon_code:pricing.items?.coupon?.code || ""
 						isPaypal: $scope.paymentMethod == 'paypal'
 						address:
-							address1: $scope.data.address1
-							address2: $scope.data.address2
-							country:  $scope.data.country
-							state:    $scope.data.state
-							zip:      $scope.data.zip
+							address1:    $scope.data.address1
+							address2:    $scope.data.address2
+							country:     $scope.data.country
+							state:       $scope.data.state
+							postal_code: $scope.date.postal_code
+							zip:         $scope.data.zip
 				$http.post("/user/subscription/create", postData)
 					.success (data, status, headers)->
 						sixpack.convert "in-editor-free-trial-plan", pricing.items.plan.code, (err)->

+ 26 - 0
services/web/test/UnitTests/coffee/Subscription/RecurlyWrapperTests.coffee

@@ -329,5 +329,31 @@ describe "RecurlyWrapper", ->
 			@apiRequest.called.should.equal true
 	
 
+	describe "_addressToXml", ->
+
+		beforeEach ->
+			@address =
+				address1: "addr_one"
+				address2: "addr_two"
+				country:  "some_country"
+				state:    "some_state"
+				zip:      "some_zip"
+				nonsenseKey: "rubbish"
+
+		it 'should generate the correct xml', () ->
+			result = RecurlyWrapper._addressToXml @address
+			should.equal(
+				result,
+				"""
+				<billing_info>
+				<address1>addr_one</address1>
+				<address2 nil="nil">addr_two</address2>
+				<country>some_country</country>
+				<state>some_state</state>
+				<zip>some_zip</zip>
+				</billing_info>\n
+				"""
+			)
+