public GetExpressCheckoutResult GetExpressCheckout() { HttpContext context = HttpContext.Current; ExpressCheckoutSession existingSession = ExpressCheckoutSession.Current; if (existingSession == null) { ErrorType[] customErrorList = new ErrorType[1]; ErrorType customError = new ErrorType(); customError.ErrorCode = "SESSION"; customError.ShortMessage = "Missing Token"; customError.LongMessage = "The PayPal session token was expired or unavailable. Please try again."; customErrorList[0] = customError; return(new GetExpressCheckoutResult(null, customErrorList)); } context.Trace.Write("Detected PayPal Token:" + existingSession.Token); context.Trace.Write("Token Expiration:" + existingSession.TokenExpiration.ToLongDateString()); GetExpressCheckoutDetailsRequestType expressCheckoutRequest = new GetExpressCheckoutDetailsRequestType(); expressCheckoutRequest.Token = existingSession.Token; expressCheckoutRequest.Version = "1.0"; //EXECUTE REQUEST GetExpressCheckoutDetailsResponseType expressCheckoutResponse; expressCheckoutResponse = (GetExpressCheckoutDetailsResponseType)SoapCall("GetExpressCheckoutDetails", expressCheckoutRequest); if (expressCheckoutResponse == null) { ErrorType[] customErrorList = new ErrorType[1]; ErrorType customError = new ErrorType(); customError.ErrorCode = "NORESP"; customError.ShortMessage = "No Response From Server"; customError.LongMessage = "The PayPal service is unavailable at this time."; customErrorList[0] = customError; return(new GetExpressCheckoutResult(null, customErrorList)); } //IF ERRORS ARE IN RESPONSE, RETURN THEM AND EXIT PROCESS if (expressCheckoutResponse.Errors != null) { return(new GetExpressCheckoutResult(null, expressCheckoutResponse.Errors)); } //GET THE DETAILS OF THE REQUEST GetExpressCheckoutDetailsResponseDetailsType expressCheckoutDetails; expressCheckoutDetails = expressCheckoutResponse.GetExpressCheckoutDetailsResponseDetails; //MAKE SURE CUSTOMER IDS MATCH User currentUser = Token.Instance.User; if (expressCheckoutDetails.Custom != ("UID" + currentUser.UserId.ToString())) { ErrorType[] customErrorList = new ErrorType[1]; ErrorType customError = new ErrorType(); customError.ErrorCode = "USER"; customError.ShortMessage = "User Mismatch"; customError.LongMessage = "The PayPal basket did not have the expected user context."; customErrorList[0] = customError; Logger.Warn("Error in PayPal GetExpressCheckout. User ID detected in PayPal response: " + expressCheckoutDetails.Custom + ", Customer User ID: " + currentUser.UserId.ToString()); return(new GetExpressCheckoutResult(null, customErrorList)); } //CHECK WHETHER AN EXISTING USER IS ASSOCIATED WITH THE RETURNED PAYPAL ID //IF THE CURRENT USER DOES NOT MATCH, LOG IN THE PAYPAL USER ACCOUNT string paypalEmail = expressCheckoutDetails.PayerInfo.Payer; string paypalPayerID = expressCheckoutDetails.PayerInfo.PayerID; //PAYER ID IS SUPPOSED TO BE UNIQUE REGARDLESS OF EMAIL ADDRESS, LOOK FOR ASSOCIATED ACCT User paypalUser = UserDataSource.LoadForPayPalId(paypalPayerID); //IF NOT FOUND, SEE IF AN ACCOUNT EXISTS WITH THAT EMAIL AS USERNAME if (paypalUser == null) { paypalUser = UserDataSource.LoadForUserName(paypalEmail); } if (paypalUser != null) { //WE FOUND AN ACCOUNT FOR THIS PAYPAL USER context.Trace.Write(this.GetType().ToString(), "PAYPAL USER FOUND IN DATABASE"); if (currentUser.UserId != paypalUser.UserId) { //THE PAYPAL USER IS NOT THE CURRENT USER CONTEXT, SO TRANSFER THE BASKET context.Trace.Write(this.GetType().ToString(), "MOVE BASKET TO " + paypalUser.UserName); Basket.Transfer(currentUser.UserId, paypalUser.UserId, true); //REMOVE PAYPAL EXPRESS SESSION FROM OLD USER SESSION ExpressCheckoutSession.Delete(currentUser); } } else { //WE DID NOT FIND AN ACCOUNT context.Trace.Write(this.GetType().ToString(), "PAYPAL USER NOT FOUND IN DATABASE"); if (currentUser.IsAnonymous) { //CURRENT USER IS ANON, REGISTER A NEW USER ACCOUNT context.Trace.Write(this.GetType().ToString(), "REGISTERING " + paypalEmail); MembershipCreateStatus status; paypalUser = UserDataSource.CreateUser(paypalEmail, paypalEmail, StringHelper.RandomString(8), string.Empty, string.Empty, true, 0, out status); paypalUser.PayPalId = paypalPayerID; paypalUser.Save(); Basket.Transfer(currentUser.UserId, paypalUser.UserId, true); //REMOVE PAYPAL EXPRESS SESSION FROM OLD USER SESSION ExpressCheckoutSession.Delete(currentUser); } else { //UPDATE THE PAYPAL ID OF THE CURRENTLY AUTHENTICATED USER context.Trace.Write(this.GetType().ToString(), "ASSIGNING CURRENT USER TO " + paypalEmail); paypalUser = currentUser; paypalUser.PayPalId = paypalPayerID; paypalUser.Save(); } } //PAYPAL HAS AUTHENTICATED THE USER FormsAuthentication.SetAuthCookie(paypalUser.UserName, false); //UPDATE THE PRIMARY ADDRESS INFORMATION FOR THE USER Address billingAddress = paypalUser.PrimaryAddress; billingAddress.FirstName = expressCheckoutDetails.PayerInfo.PayerName.FirstName; billingAddress.LastName = expressCheckoutDetails.PayerInfo.PayerName.LastName; billingAddress.Company = expressCheckoutDetails.PayerInfo.PayerBusiness; billingAddress.Address1 = expressCheckoutDetails.PayerInfo.Address.Street1; billingAddress.Address2 = expressCheckoutDetails.PayerInfo.Address.Street2; billingAddress.City = expressCheckoutDetails.PayerInfo.Address.CityName; billingAddress.Province = expressCheckoutDetails.PayerInfo.Address.StateOrProvince; billingAddress.PostalCode = expressCheckoutDetails.PayerInfo.Address.PostalCode; billingAddress.CountryCode = expressCheckoutDetails.PayerInfo.Address.Country.ToString(); if (!string.IsNullOrEmpty(expressCheckoutDetails.ContactPhone)) { billingAddress.Phone = expressCheckoutDetails.ContactPhone; } billingAddress.Email = expressCheckoutDetails.PayerInfo.Payer; billingAddress.Residence = (!string.IsNullOrEmpty(billingAddress.Company)); paypalUser.Save(); //UPDATE THE SHIPPING ADDRESS IN THE BASKET Basket basket = paypalUser.Basket; basket.Package(); foreach (BasketShipment shipment in basket.Shipments) { shipment.AddressId = billingAddress.AddressId; } basket.Save(); //PUT PAYPAL DETAILS INTO SESSION context.Trace.Write(this.GetType().ToString(), "Saving ExpressCheckoutSession"); existingSession.Token = expressCheckoutDetails.Token; existingSession.TokenExpiration = DateTime.UtcNow.AddHours(3); existingSession.PayerID = paypalPayerID; existingSession.Payer = expressCheckoutDetails.PayerInfo.Payer; existingSession.Save(paypalUser); context.Trace.Write("Saved PayPal Token:" + existingSession.Token); context.Trace.Write("Token Expiration:" + existingSession.TokenExpiration.ToLongDateString()); return(new GetExpressCheckoutResult(paypalUser, null)); }