Ejemplo n.º 1
0
        /// <summary>
        /// The Initialize method call is the backbone of the credit card implementation and all other payment methods. This method
        /// is used for setting up the transaction, specifying the price of the transaction and the purchase operation.<br />
        /// Initialize is used to supply PayEx with all the necessary data to initialize an order. Upon the successful
        /// initialization of an order, a reference (orderRef) is returned to the merchant. The merchant may then redirect the user
        /// or alternatively call one of the server to server methods.
        /// Documentation: http://www.payexpim.com/technical-reference/pxorder/initialize8/
        /// </summary>
        /// <param name="request">The parameters to the Initialize request</param>
        public async Task <InitializeResult> Initialize(InitializeRequest request)
        {
            // Validation
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request), "request is required");
            }
            if (string.IsNullOrEmpty(request.OrderID))
            {
                throw new ArgumentNullException("OrderID", "OrderID is required");
            }
            if (string.IsNullOrEmpty(request.CurrencyCode))
            {
                throw new ArgumentNullException("CurrencyCode", "CurrencyCode is required");
            }
            if (request.Amount <= 0)
            {
                throw new ArgumentOutOfRangeException("Price", "Price must be non-zero.");
            }

            var clientIdentifier = "USERAGENT=" + (request.UserAgent ?? string.Empty);

            var additionalValues = "RESPONSIVE=1";

            if (!string.IsNullOrWhiteSpace(request.MobilePhoneNumber))
            {
                additionalValues += $"&MSISDN={Regex.Replace(request.MobilePhoneNumber, @"\D", "")}";
            }

            var convPrice    = request.Amount.ToPayEx();
            var priceArgList = string.Empty;
            // string.Format("VISA={0},MC={0},AMEX={0},FSPA={0},NB={0},SHB={0},SEB={0},SWISH={0}", convPrice);
            long usedPrice  = convPrice;
            var  vat        = request.VatPercent.ToPayEx(); // Percent * 100
            var  externalID = string.Empty;
            var  view       = request.View ?? "CREDITCARD"; // Default payment method.

            // Build string for md5 including all fields except empty strings and description field
            var hashInput = new StringBuilder();

            hashInput.Append(Account.AccountNumber);
            hashInput.Append(request.PurchaseOperation.ToPayEx());
            hashInput.Append(usedPrice);
            hashInput.Append(priceArgList);
            hashInput.Append(request.CurrencyCode);
            hashInput.Append(vat);
            hashInput.Append(request.OrderID);
            hashInput.Append(request.ProductNumber);
            hashInput.Append(request.Description);
            hashInput.Append(request.ClientIPAddress);
            hashInput.Append(clientIdentifier);
            hashInput.Append(additionalValues);
            hashInput.Append(externalID);
            hashInput.Append(request.ReturnURL);
            hashInput.Append(view);
            hashInput.Append(request.AgreementRef);
            hashInput.Append(request.CancelUrl);
            hashInput.Append(request.ClientLanguage);
            // Add encryption key at the end of string to be hashed
            hashInput.Append(Account.EncryptionKey);
            // Create a hash string from the parameters
            string hash;

            MD5Hash.Hash(hashInput.ToString(), out hash);

            // Invoke Initialize method on external PayEx PxOrder web service
            var payexOrder = GetPxOrderClient();
            var xmlReturn  = await payexOrder.Initialize8Async(
                Account.AccountNumber,
                request.PurchaseOperation.ToPayEx(),
                usedPrice,
                priceArgList,
                request.CurrencyCode,
                vat,
                request.OrderID,
                request.ProductNumber ?? "",
                request.Description ?? "",
                request.ClientIPAddress ?? "",
                clientIdentifier,
                additionalValues,
                externalID,
                request.ReturnURL ?? "",
                view,
                request.AgreementRef ?? "",
                request.CancelUrl ?? "",
                request.ClientLanguage ?? "",
                hash);

            // Parse the result
            var result = ResultParser.ParseInitializeResult(xmlReturn);

            return(result);
        }