예제 #1
0
        /// <summary>
        /// Makes a transaction when there exist a verified agreement between the client and the merchant.
        /// In case of payment failure: Please use a minimum of 30 minutes delay between the first try and the second. If the
        /// transaction still fails, please wait a couple of hours before the next try. After a total period of 8 hours, you should
        /// stop trying to charge the customer.
        /// Documentation: http://www.payexpim.com/technical-reference/pxagreement/autopay/
        /// </summary>
        /// <param name="request">The parameters to the AutoPay request</param>
        public async Task <AutoPayResult> AutoPay(AutoPayRequest request)
        {
            // Validation
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request), "request is required");
            }

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

            hashInput.Append(Account.AccountNumber);
            hashInput.Append(request.AgreementRef);
            hashInput.Append(request.Amount.ToPayEx());
            hashInput.Append(request.ProductNumber);
            hashInput.Append(request.Description);
            hashInput.Append(request.OrderID);
            hashInput.Append(request.PurchaseOperation.ToPayEx());
            hashInput.Append(request.CurrencyCode);
            // 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 payexAgreement = GetPxAgreementClient();
            var xmlReturn      = await payexAgreement.AutoPay3Async(
                Account.AccountNumber,
                request.AgreementRef ?? "",
                request.Amount.ToPayEx(),
                request.ProductNumber ?? "",
                request.Description ?? "",
                request.OrderID ?? "",
                request.PurchaseOperation.ToPayEx(),
                request.CurrencyCode ?? "",
                hash);

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

            return(result);
        }