Esempio n. 1
0
        /// <summary>
        /// Creates a new agreement between the merchant and the client. Before any AutoPay transactions can take place the client
        /// has to complete a purchase with the agreement reference. The agreement will be set to verified when this is done.
        /// Documentation: http://www.payexpim.com/technical-reference/pxagreement/createagreement3/
        /// </summary>
        /// <param name="request">The parameters to the CreateAgreement request</param>
        public async Task <CreateAgreementResult> CreateAgreement(CreateAgreementRequest request)
        {
            // Validation
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request), "request is required");
            }

            var notifyUrl = string.Empty; // Deprecated, leave blank

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

            hashInput.Append(Account.AccountNumber);
            hashInput.Append(request.MerchantRef);
            hashInput.Append(request.Description);
            hashInput.Append(request.PurchaseOperation.ToPayEx());
            hashInput.Append(request.MaxAmount.ToPayEx());
            hashInput.Append(notifyUrl);
            hashInput.Append(request.StartDate.ToPayEx());
            hashInput.Append(request.StopDate.ToPayEx());
            // 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.CreateAgreement3Async(
                Account.AccountNumber,
                request.MerchantRef ?? "",
                request.Description ?? "",
                request.PurchaseOperation.ToPayEx(),
                request.MaxAmount.ToPayEx(),
                notifyUrl,
                request.StartDate.ToPayEx(),
                request.StopDate.ToPayEx(),
                hash);

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

            return(result);
        }