Order address is lost when saving cart as purchase order

Vote:
 

Episerver version: 11.8.0

Commerce version: 12.2.0

When converting a basket to a purchase order the invoice address is lost.

var orderReference = _orderRepository.SaveAsPurchaseOrder(cart);

Any hints or pointers?

See data below

Cart json

{
	"Id": 54,
	"Created": "2018-06-08T11:50:47.83Z",
	"Currency": "SEK",
	"CustomerId": "7f2f3f62-204d-4c41-b72d-cce81afcdaaa",
	"Forms": [
		{
			"OrderFormId": -1,
			"AuthorizedPaymentTotal": 0,
			"CapturedPaymentTotal": 0,
			"HandlingTotal": 0,
			"Name": "Swedish",
			"Shipments": [
				{
					"ShipmentId": -1,
					"ShippingMethodId": "d4544b04-a554-4540-81da-d4f530216a01",
					"ShippingMethodName": null,
					"ShippingAddress": {
						"Id": "Invoice",
						"FirstName": null,
						"LastName": null,
						"Organization": null,
						"Line1": null,
						"Line2": null,
						"City": null,
						"CountryCode": null,
						"CountryName": null,
						"PostalCode": null,
						"RegionCode": null,
						"RegionName": null,
						"DaytimePhoneNumber": null,
						"EveningPhoneNumber": null,
						"FaxNumber": null,
						"Email": null,
						"Properties": {}
					},
					"ShipmentTrackingNumber": "GLS",
					"OrderShipmentStatus": 1,
					"PickListId": null,
					"WarehouseCode": null,
					"LineItems": [
						{
							"LineItemId": -1,
							"Code": "1234",
							"DisplayName": null,
							"PlacedPrice": 7050,
							"Quantity": 5,
							"ReturnQuantity": 0,
							"InventoryTrackingStatus": 0,
							"IsInventoryAllocated": false,
							"IsGift": false,
							"Properties": {},
							"AllowBackordersAndPreorders": false,
							"InStockQuantity": 0,
							"BackorderQuantity": 0,
							"PreorderQuantity": 0,
							"InventoryStatus": 0,
							"MaxQuantity": 0,
							"MinQuantity": 0,
							"EntryAmount": 0,
							"OrderAmount": 0,
							"TaxCategoryId": 0,
							"SalesTax": 0,
							"IsSalesTaxUpToDate": true
						}
					],
					"Properties": {},
					"ShipmentDiscount": 0,
					"IsShippingCostUpToDate": true,
					"IsShippingTaxUpToDate": true,
					"ShippingCost": 65,
					"ShippingTax": 0,
					"OperationKeys": {}
				}
			],
			"Promotions": [],
			"CouponCodes": [],
			"Payments": [
				{
					"CardType": null,
					"CreditCardNumber": null,
					"CreditCardSecurityCode": null,
					"ExpirationMonth": 0,
					"ExpirationYear": 0,
					"ProviderPaymentId": null,
					"Amount": 35250,
					"AuthorizationCode": null,
					"BillingAddress": {
						"Id": "Invoice",
						"FirstName": "John Johnson",
						"LastName": null,
						"Organization": null,
						"Line1": "High street 42",
						"Line2": null,
						"City": "Velgreen",
						"CountryCode": "SE",
						"CountryName": null,
						"PostalCode": "11111",
						"RegionCode": null,
						"RegionName": null,
						"DaytimePhoneNumber": "",
						"EveningPhoneNumber": null,
						"FaxNumber": null,
						"Email": "my@email.se",
						"Properties": {}
					},
					"CustomerName": null,
					"ImplementationClass": "EPiServer.Commerce.Order.Internal.SerializableCreditCardPayment, EPiServer.Business.Commerce, Version=12.2.0.0, Culture=neutral, PublicKeyToken=8fe83dea738b45b7",
					"PaymentId": 0,
					"PaymentMethodId": "00000000-0000-0000-0000-000000000000",
					"PaymentMethodName": "VISA",
					"PaymentType": 0,
					"ProviderTransactionID": null,
					"Status": null,
					"TransactionID": null,
					"TransactionType": null,
					"ValidationCode": null,
					"Properties": {}
				}
			],
			"Properties": {}
		}
	],
	"Market": null,
	"MarketId": "sv-SE",
	"MarketName": "Swedish",
	"PricesIncludeTax": false,
	"Modified": "2018-06-08T11:52:00.814821Z",
	"Name": "Swedish",
	"Notes": [],
	"OrderLink": {
		"OrderGroupId": 54,
		"Name": "Swedish",
		"CustomerId": "7f2f3f62-204d-4c41-b72d-cce81afcdaaa",
		"OrderType": "EPiServer.Commerce.Order.Internal.SerializableCart, EPiServer.Business.Commerce, Version=12.2.0.0, Culture=neutral, PublicKeyToken=8fe83dea738b45b7"
	},
	"OrderStatus": 4,
	"Organization": null,
	"Properties": {
		"CustomOrderNumber": {
			"$type": "System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089",
			"$value": "PO54926"
		}
	},
	"IsTaxTotalUpToDate": true,
	"TaxTotal": 0
}

Purchase order data

See image: https://www.screencast.com/t/ZjltJjNLjy6

#193960
Jun 08, 2018 14:07
Vote:
 

Just FYI I tried on Quicksilver and everything works fine

{
   "Id":504,
   "Created":"2018-06-08T12:42:15.97Z",
   "Currency":"USD",
   "CustomerId":"b6606261-7a31-4556-adf1-141dde2e4744",
   "Forms":[
      {
         "OrderFormId":-1,
         "Name":"Default",
         "Shipments":[
            {
               "ShipmentId":-1,
               "ShippingMethodId":"515056e2-b696-4281-92e1-93d09f504bb8",
               "ShippingAddress":{
                  "Id":"No addresses exists",
                  "FirstName":"Quan",
                  "LastName":"Mai",
                  "Line1":"abcxyz",
                  "City":"STOCKHOLM",
                  "CountryCode":"SWE",
                  "CountryName":"Sweden",
                  "PostalCode":"111 56",
                  "RegionCode":"Alabama",
                  "RegionName":"Alabama",
                  "Email":"vimvq1987@gmail.com",
                  "Properties":{

                  }
               },
               "OrderShipmentStatus":1,
               "WarehouseCode":"stockholmstore",
               "LineItems":[
                  {
                     "LineItemId":-1,
                     "Code":"SKU-44477844",
                     "DisplayName":"Puma Guida Moc SF Chase Mens Size 14 Red Leather Loafers Shoes UK   EU 48.5",
                     "PlacedPrice":52.500000000,
                     "Quantity":1.0,
                     "InventoryTrackingStatus":1,
                     "Properties":{

                     },
                     "InStockQuantity":451.0,
                     "InventoryStatus":1,
                     "MaxQuantity":100.000000000,
                     "MinQuantity":1.000000000,
                     "TaxCategoryId":1,
                     "IsSalesTaxUpToDate":true
                  }
               ],
               "Properties":{

               },
               "IsShippingCostUpToDate":true,
               "IsShippingTaxUpToDate":true,
               "ShippingCost":20.000000000,
               "OperationKeys":{

               }
            }
         ],
         "Promotions":[

         ],
         "CouponCodes":[

         ],
         "Payments":[
            {
               "CardType":"Credit card",
               "CreditCardNumber":"4662519843660534",
               "CreditCardSecurityCode":"212",
               "ExpirationMonth":6,
               "ExpirationYear":2018,
               "Amount":72.5000000000,
               "BillingAddress":{
                  "Id":"Billing address (ab5)",
                  "FirstName":"Van",
                  "LastName":"Mai",
                  "Line1":"Ab5",
                  "City":"Bandhagen",
                  "CountryCode":"SWE",
                  "CountryName":"Sweden",
                  "PostalCode":"12476",
                  "RegionCode":"Alabama",
                  "RegionName":"Alabama",
                  "Email":"vimvq1987@gmail.com",
                  "Properties":{

                  }
               },
               "CustomerName":"abc",
               "ImplementationClass":"EPiServer.Commerce.Order.Internal.SerializableCreditCardPayment, EPiServer.Business.Commerce, Version=12.3.1.0, Culture=neutral, PublicKeyToken=8fe83dea738b45b7",
               "PaymentMethodId":"87080c43-d08d-4471-a4fd-9897e2530358",
               "PaymentMethodName":"GenericCreditCard",
               "Status":"Pending",
               "TransactionType":"Authorization",
               "Properties":{

               }
            }
         ],
         "Properties":{

         }
      }
   ],
   "MarketId":"US",
   "MarketName":"USA",
   "Modified":"2018-06-08T12:42:38.687617Z",
   "Name":"Default",
   "Notes":[

   ],
   "OrderLink":{
      "OrderGroupId":504,
      "Name":"Default",
      "CustomerId":"b6606261-7a31-4556-adf1-141dde2e4744",
      "OrderType":"EPiServer.Commerce.Order.Internal.SerializableCart, EPiServer.Business.Commerce, Version=12.3.1.0, Culture=neutral, PublicKeyToken=8fe83dea738b45b7"
   },
   "OrderStatus":4,
   "Properties":{

   },
   "IsTaxTotalUpToDate":true
}

And the order has correct billing address ... 

#193964
Edited, Jun 08, 2018 14:44
Vote:
 

Thanks Quan,

Any pointer to how I can find the error?

#193987
Jun 11, 2018 8:14
Vote:
 

Can you post your entire code for creating the order here? from loading the cart? 

#193991
Jun 11, 2018 8:56
Vote:
 

A difference I spot is that the adress on the Shipment is missing in your cart data, maybe the SaveAsPurchasOrder takes it's OrderAdress data from the Shipment and not the Payment?

Maybe due to them being named the same thing? "Invoice". Try renaming the shipping adress.

#193996
Edited, Jun 11, 2018 10:34
Vote:
 

I doubt it. It should take both into account. Well it's not possible that we might have missed something, but it's very unlikely 

#193997
Jun 11, 2018 10:36
Vote:
 

Yikes, you're fast! :D

I've edited my answer with a possible reason. :P

#193998
Edited, Jun 11, 2018 10:38
Vote:
 

Yes that is a very good guess. I missed that. When you save the addresses they are identified by names so matching names can be the problem 

#193999
Jun 11, 2018 10:39
Vote:
 

Nice works guys, 

If I choose a differnt shipping address it works.

I try the reuse addresses link explained here: https://world.episerver.com/documentation/developer-guides/commerce/orders/order-manipulation/ (Working with addresses)

var orderRepository = ServiceLocator.Current.GetInstance<IOrderRepository>();
var orderGroupFactory = ServiceLocator.Current.GetInstance<IOrderGroupFactory>();
var contactId = PrincipalInfo.CurrentPrincipal.GetContactId();
var cart = orderRepository.LoadCart<ICart>(contactId, "Default");

var address = orderGroupFactory.CreateOrderAddress(cart);

//Use Id to reuse
address.Id = "Billing";
cart.GetFirstForm().Payments.First().BillingAddress = address;

//Since there is already an address with "Billing" it will use that address instead of creating another one on the order.
var reuseOtherAddress = orderGroupFactory.CreateOrderAddress(cart);
reuseOtherAddress.Id = "Billing";
cart.GetFirstShipment().ShippingAddress = reuseOtherAddress;
#194001
Edited, Jun 11, 2018 11:46
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.