コード例 #1
0
        /// <summary>
        /// Credit a transaction after a completed purchase.
        /// The credit functionality can behave differently based on the payment instrument and/or if the financial institution
        /// accepts credit instructions (not all do). Also note that some financial institutions don’t support partial credit
        /// before 24 hours have past. Please contact PayEx Support for more information.
        /// </summary>
        /// <param name="transactionNumber">The transactionNumber of the transaction you wish to credit.</param>
        /// <param name="amount">The amount you wish to credit.</param>
        /// <param name="orderID">
        /// Order Id that will be presented in PayEx report. This value have to be numeric if merchant have
        /// chosen to send orderId to the aquiring institution.
        /// </param>
        public async Task <CreditResult> Credit(int transactionNumber, decimal amount, string orderID)
        {
            // Validation
            if (transactionNumber <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(transactionNumber), "transactionNumber is required");
            }
            if (amount <= decimal.Zero)
            {
                throw new ArgumentOutOfRangeException(nameof(amount), "amount must be non-zero.");
            }

            // Null not allowed
            orderID = orderID ?? string.Empty;
            var convAmount       = amount.ToPayEx();
            var vatAmount        = 0;
            var additionalValues = string.Empty;

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

            hashInput.Append(Account.AccountNumber);
            hashInput.Append(transactionNumber);
            hashInput.Append(convAmount);
            hashInput.Append(orderID);
            hashInput.Append(vatAmount);
            hashInput.Append(additionalValues);
            // 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.Credit5Async(
                Account.AccountNumber,
                transactionNumber,
                convAmount,
                orderID,
                vatAmount,
                additionalValues,
                hash);

            // Parse the result and retrieve code-node to figure out if the service method was invoked successfully.
            var result = ResultParser.ParseCreditResult(xmlReturn);

            return(result);
        }