Esempio n. 1
0
		/// <summary>
		/// Processes the Authorize and AuthorizeAndCapture transactions
		/// </summary>
		/// <param name="invoice">The <see cref="IInvoice"/> to be paid</param>
		/// <param name="payment">The <see cref="IPayment"/> record</param>
		/// <param name="args"></param>
		/// <returns>The <see cref="IPaymentResult"/></returns>
		public IPaymentResult InitializePayment(IInvoice invoice, IPayment payment, ProcessorArgumentCollection args) {

			var setExpressCheckoutRequestDetails = new SetExpressCheckoutRequestDetailsType();

			Func<string, string> adjustUrl = (url) => {
				if (!url.StartsWith("http")) url = GetWebsiteUrl() + (url[0] == '/' ? "" : "/") + url;
				url = url.Replace("{invoiceKey}", invoice.Key.ToString(), StringComparison.InvariantCultureIgnoreCase);
				url = url.Replace("{paymentKey}", payment.Key.ToString(), StringComparison.InvariantCultureIgnoreCase);
				url = url.Replace("{paymentMethodKey}", payment.PaymentMethodKey.ToString(), StringComparison.InvariantCultureIgnoreCase);
				return url;
			};
			
			// Save ReturnUrl and CancelUrl in ExtendedData.
			// They will be usefull in PayPalApiController.

			var returnUrl = args.GetReturnUrl();
			if (returnUrl.IsEmpty()) returnUrl = _settings.ReturnUrl;
			if (returnUrl.IsEmpty()) returnUrl = "/";
			returnUrl = adjustUrl(returnUrl);
			payment.ExtendedData.SetValue(Constants.ExtendedDataKeys.ReturnUrl, returnUrl);

			var cancelUrl = args.GetCancelUrl();
			if (cancelUrl.IsEmpty()) cancelUrl = _settings.CancelUrl;
			if (cancelUrl.IsEmpty()) cancelUrl = "/";
			cancelUrl = adjustUrl(cancelUrl);
			payment.ExtendedData.SetValue(Constants.ExtendedDataKeys.CancelUrl, cancelUrl);

			// Set ReturnUrl and CancelUrl of PayPal request to PayPalApiController.
			setExpressCheckoutRequestDetails.ReturnURL = adjustUrl("/umbraco/MerchelloPayPal/PayPalApi/SuccessPayment?InvoiceKey={invoiceKey}&PaymentKey={paymentKey}");
			setExpressCheckoutRequestDetails.CancelURL = adjustUrl("/umbraco/MerchelloPayPal/PayPalApi/AbortPayment?InvoiceKey={invoiceKey}&PaymentKey={paymentKey}");

			//setExpressCheckoutRequestDetails.OrderDescription = "#" + invoice.InvoiceNumber;
			setExpressCheckoutRequestDetails.PaymentDetails = new List<PaymentDetailsType> { CreatePayPalPaymentDetails(invoice, args) };

			var setExpressCheckout = new SetExpressCheckoutReq() {
				SetExpressCheckoutRequest = new SetExpressCheckoutRequestType(setExpressCheckoutRequestDetails)
			};

			try {
				var response = GetPayPalService().SetExpressCheckout(setExpressCheckout);
				if (response.Ack != AckCodeType.SUCCESS && response.Ack != AckCodeType.SUCCESSWITHWARNING) {
					return new PaymentResult(Attempt<IPayment>.Fail(payment, CreateErrorResult(response.Errors)), invoice, false);
				}

				var redirectUrl = string.Format("https://www.{0}paypal.com/cgi-bin/webscr?cmd=_express-checkout&token={1}", (_settings.LiveMode ? "" : "sandbox."), response.Token);
				payment.ExtendedData.SetValue("RedirectUrl", redirectUrl);
				return new PaymentResult(Attempt<IPayment>.Succeed(payment), invoice, true);

			} catch (Exception ex) {
				return new PaymentResult(Attempt<IPayment>.Fail(payment, ex), invoice, true);
			}

		}
Esempio n. 2
0
        /// <summary>
        /// Processes the Authorize and AuthorizeAndCapture transactions
        /// </summary>
        /// <param name="invoice">The <see cref="IInvoice"/> to be paid</param>
        /// <param name="payment">The <see cref="IPayment"/> record</param>
        /// <param name="args"></param>
        /// <returns>The <see cref="IPaymentResult"/></returns>
        public IPaymentResult InitializePayment(IInvoice invoice, IPayment payment, ProcessorArgumentCollection args)
        {
            var setExpressCheckoutRequestDetails = new SetExpressCheckoutRequestDetailsType();

            Func <string, string> adjustUrl = (url) => {
                if (!url.StartsWith("http"))
                {
                    url = GetWebsiteUrl() + (url[0] == '/' ? "" : "/") + url;
                }
                url = url.Replace("{invoiceKey}", invoice.Key.ToString(), StringComparison.InvariantCultureIgnoreCase);
                url = url.Replace("{paymentKey}", payment.Key.ToString(), StringComparison.InvariantCultureIgnoreCase);
                url = url.Replace("{paymentMethodKey}", payment.PaymentMethodKey.ToString(), StringComparison.InvariantCultureIgnoreCase);
                return(url);
            };

            // Save ReturnUrl and CancelUrl in ExtendedData.
            // They will be usefull in PayPalApiController.

            var returnUrl = args.GetReturnUrl();

            if (returnUrl.IsEmpty())
            {
                returnUrl = _settings.ReturnUrl;
            }
            if (returnUrl.IsEmpty())
            {
                returnUrl = "/";
            }
            returnUrl = adjustUrl(returnUrl);
            payment.ExtendedData.SetValue(Constants.ExtendedDataKeys.ReturnUrl, returnUrl);

            var cancelUrl = args.GetCancelUrl();

            if (cancelUrl.IsEmpty())
            {
                cancelUrl = _settings.CancelUrl;
            }
            if (cancelUrl.IsEmpty())
            {
                cancelUrl = "/";
            }
            cancelUrl = adjustUrl(cancelUrl);
            payment.ExtendedData.SetValue(Constants.ExtendedDataKeys.CancelUrl, cancelUrl);

            // Set ReturnUrl and CancelUrl of PayPal request to PayPalApiController.
            setExpressCheckoutRequestDetails.ReturnURL = adjustUrl("/umbraco/MerchelloPayPal/PayPalApi/SuccessPayment?InvoiceKey={invoiceKey}&PaymentKey={paymentKey}");
            setExpressCheckoutRequestDetails.CancelURL = adjustUrl("/umbraco/MerchelloPayPal/PayPalApi/AbortPayment?InvoiceKey={invoiceKey}&PaymentKey={paymentKey}");

            //setExpressCheckoutRequestDetails.OrderDescription = "#" + invoice.InvoiceNumber;
            setExpressCheckoutRequestDetails.PaymentDetails = new List <PaymentDetailsType> {
                CreatePayPalPaymentDetails(invoice, args)
            };

            var setExpressCheckout = new SetExpressCheckoutReq()
            {
                SetExpressCheckoutRequest = new SetExpressCheckoutRequestType(setExpressCheckoutRequestDetails)
            };

            try {
                var response = GetPayPalService().SetExpressCheckout(setExpressCheckout);
                if (response.Ack != AckCodeType.SUCCESS && response.Ack != AckCodeType.SUCCESSWITHWARNING)
                {
                    return(new PaymentResult(Attempt <IPayment> .Fail(payment, CreateErrorResult(response.Errors)), invoice, false));
                }

                var redirectUrl = string.Format("https://www.{0}paypal.com/cgi-bin/webscr?cmd=_express-checkout&token={1}", (_settings.LiveMode ? "" : "sandbox."), response.Token);
                payment.ExtendedData.SetValue("RedirectUrl", redirectUrl);
                return(new PaymentResult(Attempt <IPayment> .Succeed(payment), invoice, true));
            } catch (Exception ex) {
                return(new PaymentResult(Attempt <IPayment> .Fail(payment, ex), invoice, true));
            }
        }