public ActionResult ExecuteItemPurchase(ItemPurchaseDataToken token, BillingAddressDTO address, eActionKinds actionKind, string referral) { string error; var orderNo = -1; var user = this.CurrentUser(); if (user == null) { return(Redirect2PaymentError("authentication issue. please re-login again")); } try { if (token.IsFree) { if (String.IsNullOrWhiteSpace(token.CouponCode)) { return(Redirect2PaymentError("coupon code required")); } token.PaymentMethod = BillingEnums.ePaymentMethods.Charge_Free; var completed = _billingServices.CompleteFreeCouponRequest(token, user.UserId, out orderNo, out error); if (!completed) { return(Redirect2PaymentError(error)); } SavePurchaseCompleteEvent(token); return(ReturnPurchaseSuccess(token.PriceToken.PriceLineID, orderNo, token.TrackingID, referral)); } var priceToken = _billingServices.GetPriceLineToken(token.PriceToken.PriceLineID, out error); if (priceToken == null) { return(Redirect2PaymentError(error)); } token.BuySubscription = priceToken.PriceType == BillingEnums.ePricingTypes.SUBSCRIPTION; #region private variables //int? storeId = null; //if (!String.IsNullOrEmpty(token.TrackingID)) //{ // storeId = _webStorePortalServices.ValidateTrackingId(token.TrackingID); //} string approval_url; bool paymentExecuted; var paymentInstrumentId = Guid.Empty; var CANCEL_PAYMENT_URL = _BaseUrl + "CancelPayment".GenerateCheckoutUrl(token.PriceToken.PriceLineID, null, token.TrackingID, referral); #endregion #region handle billing address int?addressId = null; if (token.PaymentMethod == BillingEnums.ePaymentMethods.Credit_Card) { var billingAddressDto = address; if (!IsAddressValid(ref billingAddressDto, out error)) { return(Redirect2PaymentError(error)); } token.BillingAddress = billingAddressDto; var addressSaved = _userAccountServices.SaveUserBillingAddress(ref billingAddressDto, out error); if (!addressSaved) { return(Redirect2PaymentError(error)); } addressId = billingAddressDto.AddressId; } #endregion #region payment method validation //in case of subscription with credit card save payment instrument option should be always true //we don't create on the moment recurring paypal payments with credit card = > payment method switched to Saved_Instrument if (token.BuySubscription && token.PaymentMethod == BillingEnums.ePaymentMethods.Credit_Card) { token.SavePaymentInstrument = true; } switch (token.PaymentMethod) { case BillingEnums.ePaymentMethods.Paypal: //if (token.SavePaymentInstrument) return Redirect2PaymentError("saving paypal agreement currently not supported"); break; case BillingEnums.ePaymentMethods.Credit_Card: if (token.CreditCard == null) { return(Redirect2PaymentError("credit card required")); } if (addressId == null) { return(Redirect2PaymentError("Address required")); } if (token.SavePaymentInstrument) { //create card token var card = token.CoursePurchaseDataToken2CreditCardDto(this.CurrentUser()); //save cc to paypal var cardSaved = _paypalServices.SaveCreditCard2Paypal(card, CurrentUserId, out paymentInstrumentId, out error); if (!cardSaved) { return(Redirect2PaymentError(error)); } } Session["RecurringCc"] = token.CreditCard; if (token.BuySubscription) { token.PaymentMethod = BillingEnums.ePaymentMethods.Saved_Instrument; } break; case BillingEnums.ePaymentMethods.Saved_Instrument: paymentInstrumentId = token.PaymentInstrumentId ?? Guid.Empty; if (paymentInstrumentId.Equals(Guid.Empty)) { return(Redirect2PaymentError("Select credit card")); } break; } #endregion switch (priceToken.PriceType) { case BillingEnums.ePricingTypes.ONE_TIME: case BillingEnums.ePricingTypes.RENTAL: PaypalCreditCardPaymentDTO ccToken; switch (token.PaymentMethod) { case BillingEnums.ePaymentMethods.Paypal: #region var SUCCESS_PAYMENT_URL = _BaseUrl + "OnPaymentComplete".GenerateCheckoutUrl(token.PriceToken.PriceLineID, null, token.TrackingID, referral); //Url.Action("OnPaymentComplete", "Payment", new { area = "Widget",referral }); var data = token.ItemPurchaseDataToken2PayPalCreatePaymentDto(SUCCESS_PAYMENT_URL, CANCEL_PAYMENT_URL, addressId, null); paymentExecuted = _paypalServices.CreatePaypalAccountPayment(data, out approval_url, out error); if (paymentExecuted && !String.IsNullOrEmpty(approval_url)) { return(Redirect(approval_url)); } return(Redirect2PaymentError(error ?? "unexpected error")); #endregion case BillingEnums.ePaymentMethods.Credit_Card: #region Guid requestId; if (token.SavePaymentInstrument) { //create cc payment token ccToken = token.CoursePurchaseDataToken2PayPalDirectCcPaymentDto(this.CurrentUser(), addressId, null); ccToken.paymentInstrumentId = paymentInstrumentId; //execute payment and complete purchase process paymentExecuted = _paypalServices.ExecuteSavedCreditCardPayment(ccToken, CurrentUserId, paymentInstrumentId, out requestId, out error) && _billingServices.CompletePaymentRequest(requestId, out orderNo, out error); } else { //create cc payment token ccToken = token.CoursePurchaseDataToken2PayPalDirectCcPaymentDto(this.CurrentUser(), addressId, null); //execute payment and complete purchase process paymentExecuted = _paypalServices.ExecuteDirectCreditCardPayment(ccToken, out requestId, out error) && _billingServices.CompletePaymentRequest(requestId, out orderNo, out error); } if (paymentExecuted) { SavePurchaseCompleteEvent(token); } //return paymentExecuted ? (storeId != null ? View("ThankYouStore",new PaypalCompleteRequestToken{IsSuccess = true,RedirectUrl = itemPageUrl}) : View("ThankYou")) : Redirect2PaymentError(error); return(paymentExecuted ? ReturnPurchaseSuccess(token.PriceToken.PriceLineID, orderNo, token.TrackingID, referral) : Redirect2PaymentError(error)); #endregion case BillingEnums.ePaymentMethods.Saved_Instrument: #region //create cc payment token ccToken = token.CoursePurchaseDataToken2PayPalDirectCcPaymentDto(this.CurrentUser(), addressId, null); ccToken.paymentInstrumentId = token.PaymentInstrumentId; //execute payment and complete purchase process paymentExecuted = _paypalServices.ExecuteSavedCreditCardPayment(ccToken, CurrentUserId, paymentInstrumentId, out requestId, out error) && _billingServices.CompletePaymentRequest(requestId, out orderNo, out error); if (paymentExecuted) { SavePurchaseCompleteEvent(token); } // return paymentExecuted ? (storeId != null ? View("ThankYouStore", new PaypalCompleteRequestToken { IsSuccess = true, RedirectUrl = itemPageUrl }) : View("ThankYou")) : Redirect2PaymentError(error); return(paymentExecuted ? ReturnPurchaseSuccess(token.PriceToken.PriceLineID, orderNo, token.TrackingID, referral) : Redirect2PaymentError(error)); #endregion } break; case BillingEnums.ePricingTypes.SUBSCRIPTION: switch (token.PaymentMethod) { case BillingEnums.ePaymentMethods.Paypal: #region //create paypal agreement //create request token var requestId = Guid.NewGuid(); var success_url = _BaseUrl + "OnSubscriptionComplete".GenerateCheckoutUrl(token.PriceToken.PriceLineID, null, token.TrackingID, referral); //Utils.GetKeyValue("baseUrl") + Url.Action("OnSubscriptionComplete", null, new { id = requestId, referral }); var data = token.CoursePurchaseDataToken2PayPalAgreementDto(user.FullName, user.Email, success_url, CANCEL_PAYMENT_URL, addressId, paymentInstrumentId, token.PaymentMethod, priceToken); //create payment agreement paymentExecuted = _paypalServices.CreateRecurringPaymentAgreement(data, requestId, out approval_url, out error); //redirect to paypal approval page return(paymentExecuted && !String.IsNullOrEmpty(approval_url) ? Redirect(approval_url) : Redirect2PaymentError(error ?? "unexpected error")); #endregion //currently(2014-2-11) this option disabled //case BillingEnums.ePaymentMethods.Credit_Card: // #region // requestId = Guid.NewGuid(); // RecurringPaymentExecutionResultToken result; // var dto = token.CoursePurchaseDataToken2PaypalPaymentRequestDto(requestId, CurrentUserId, addressId); // var executed = _paypalServices.ExecuteCourseSubscriptionCcRecurringPayment(dto, CurrentUserId, requestId, token.CreditCard, out result, out error) && _billingServices.CompleteSubscriptionRequest(result, out error); // var responseToken = new PaypalCompleteRequestToken // { // IsSuccess = executed // ,Message = error // ,RedirectUrl = executed ? Utils.GetKeyValue("baseUrl") + Url.Action("ThankYou", null, new { requestId }) : string.Empty // }; // return executed ? View("ThankYou", responseToken) : Redirect2PaymentError(error ?? "unexpected error"); // #endregion case BillingEnums.ePaymentMethods.Saved_Instrument: #region var subscriptionToken = token.CoursePurchaseDataToken2SubscriptionWithSavedCardDto(CurrentUserId, paymentInstrumentId, addressId); //TODO check case , when initial amount == 0; subscriptionToken.amount = subscriptionToken.InitialAmount > 0 ? subscriptionToken.InitialAmount : (decimal)0.01; paymentExecuted = _paypalServices.ExecuteSubscriptionPaymentWithStoredCreditCard(subscriptionToken, out requestId, out error); if (!paymentExecuted) { return(Redirect2PaymentError(error)); } var subscriptionCreated = _billingServices.CreateSubscriptionWithSavedCard(subscriptionToken, requestId, out orderNo, out error); if (!subscriptionCreated) { return(Redirect2PaymentError(error ?? "unexpected error")); } SavePurchaseCompleteEvent(token); return(ReturnPurchaseSuccess(token.PriceToken.PriceLineID, orderNo, token.TrackingID, referral)); #endregion } break; } CheckoutBase.IsValid = false; CheckoutBase.Message = "Unexpected result. Please contact support team"; CheckoutBase.PurchaseDataToken = token; return(View("Index", CheckoutBase)); // return View("Payment/_PurchaseItem", token); } catch (Exception ex) { error = Utils.FormatError(ex); return(Redirect2PaymentError(error)); } }
public ActionResult OnPaymentComplete(string refferal) { Guid requestId; if (!Guid.TryParse(Request.QueryString["id"], out requestId)) { return(Redirect2PaymentError("RequestId required", string.Empty, -1, string.Empty, eActionKinds.POST, refferal)); } if (String.IsNullOrEmpty(Request.QueryString["PayerID"])) { return(Redirect2PaymentError("PayerID required", string.Empty, -1, string.Empty, eActionKinds.POST, refferal)); } string error; var orderNo = -1; var baseInfo = _paypalServices.GetItemInfoFromPaymentRequest(requestId); if (!baseInfo.IsValid) { return(Redirect2PaymentError("Request not found", string.Empty, -1, string.Empty, eActionKinds.POST, refferal)); } var executed = _paypalServices.ExecutePayPalPayment(requestId, Request.QueryString["PayerID"], out error, Session.SessionID) && _billingServices.CompletePaymentRequest(requestId, out orderNo, out error); //return executed ? View("PurchaseResult", new PurchaseResultToken { IsValid = true, ActionKind = eActionKinds.POST, RedirectUrl = Url.Action("PurchaseComplete", "Payment", new { area = "Widget", id = orderNo, trackingId = baseInfo.TrackingId }) }) : // Redirect2PaymentError(error ?? "unexpected error", baseInfo.ItemName, baseInfo.PriceLineId, baseInfo.TrackingId, eActionKinds.POST); return(RedirectToAction("PurchaseResult", new { isValid = executed, actionKind = eActionKinds.POST, lineId = baseInfo.PriceLineId, itemName = baseInfo.ItemName, trackingId = baseInfo.TrackingId, orderNo, error, refferal })); }