Example #1
0
    public async Task TestCustomerProfileAsync(CustomerProfile profile, CancellationToken cancellationToken)
    {
        // 1. create customer / payment profile with validation enabled.
        var paymentProfiles = new Collection <CustomerPaymentProfileType>
        {
            new CustomerPaymentProfileType
            {
                CustomerType = profile.CustomerType,
                Payment      = new PaymentType
                {
                    CreditCard = new CreditCardType
                    {
                        CardNumber     = profile.CardNumber,
                        ExpirationDate = profile.ExpirationDate,
                        CardCode       = profile.CardCode
                    }
                },
                BillTo = new CustomerAddressType
                {
                    FirstName = profile.FirstName,
                    LastName  = profile.LastName,
                    Address   = profile.StreetLine,
                    Company   = profile.Company,
                    City      = profile.City,
                    State     = profile.StateOrProvice,
                    Zip       = profile.ZipCode,
                    Country   = profile.Country
                }
            }
        };

        var createRequest = new CreateCustomerProfileRequest
        {
            RefId          = profile.ReferenceId,
            ValidationMode = _options.ValidationMode,
            Profile        = new CustomerProfileType
            {
                Description        = profile.Description,
                Email              = profile.Email,
                MerchantCustomerId = profile.CustomerId,
                ProfileType        = profile.CustomerProfileType,
                PaymentProfiles    = paymentProfiles,
            },
        };

        var createResponse = await _customerProfileClient.CreateAsync(createRequest, cancellationToken);

        // validation list is in the same order as it has been submitted by the client code
        var parsedCreation = new PaymentGatewayResponse(createResponse.ValidationDirectResponseList[0]);

        // creation for the profile was successful
        if (createResponse.Messages.ResultCode == MessageTypeEnum.Ok)
        {
            var customerProfileId        = createResponse.CustomerProfileId;
            var customerPaymentProfileId = createResponse.CustomerPaymentProfileIdList[0];

            _logger.LogInformation(
                "CreateResponse - {customerProfileId} - {paymentProfile} - {asvCode}",
                customerProfileId,
                createResponse.CustomerPaymentProfileIdList[0],
                parsedCreation.AVSResponseText);

            DisplayResponse("CreateResponse", createResponse);

            // 2. create another payment profile
            var secondaryProfile = _sampleData.GetCustomerProfiles()[1];

            var secondaryProfileRequest = new CreateCustomerPaymentProfileRequest
            {
                RefId             = profile.ReferenceId,
                ValidationMode    = _options.ValidationMode,
                CustomerProfileId = customerProfileId,
                PaymentProfile    = new CustomerPaymentProfileType
                {
                    Payment = new PaymentType
                    {
                        CreditCard = new CreditCardType
                        {
                            CardCode       = secondaryProfile.CardCode,
                            CardNumber     = secondaryProfile.CardNumber,
                            ExpirationDate = secondaryProfile.ExpirationDate,
                        }
                    },
                    CustomerType = CustomerTypeEnum.Business,
                    BillTo       = new CustomerAddressType
                    {
                        FirstName = secondaryProfile.FirstName,
                        LastName  = secondaryProfile.LastName,
                        Address   = secondaryProfile.StreetLine,
                        Company   = secondaryProfile.Company,
                        City      = secondaryProfile.City,
                        State     = secondaryProfile.StateOrProvice,
                        Zip       = secondaryProfile.ZipCode,
                        Country   = secondaryProfile.Country
                    }
                }
            };

            var secondaryPaymentResponse = await _customerPaymentProfileClient.CreateAsync(secondaryProfileRequest, cancellationToken);

            var secondaryProfileId = secondaryPaymentResponse.CustomerPaymentProfileId;

            if (secondaryPaymentResponse.Messages.ResultCode == MessageTypeEnum.Ok)
            {
                var validateRequest = new ValidateCustomerPaymentProfileRequest
                {
                    CustomerPaymentProfileId = secondaryProfileId,
                    CustomerProfileId        = customerProfileId,
                    ValidationMode           = _options.ValidationMode,
                    RefId = profile.ReferenceId
                };

                var validateResponse = await _customerPaymentProfileClient.ValidateAsync(validateRequest, cancellationToken);

                var parsedValidation = new PaymentGatewayResponse(validateResponse?.DirectResponse !);

                if (validateResponse?.Messages.ResultCode == MessageTypeEnum.Error)
                {
                    _logger.LogWarning("{cardNumber}-{expDate}-{ccv}-{zip}-{parsed}", profile.CardNumber, profile.ExpirationDate, profile.CardCode, profile.ZipCode, parsedValidation.ResponseReasonText);
                    _logger.LogWarning(validateResponse.DirectResponse);
                }

                DisplayResponse("ValidationResponse", validateResponse !);

                // get customer profile
                var customerRequest = new GetCustomerProfileRequest
                {
                    CustomerProfileId = customerProfileId
                };

                var customerResponse = await _customerProfileClient.GetAsync(customerRequest, cancellationToken);

                DisplayResponse(" GetCustomerProfileResponse", customerResponse);

                var paymentRequest = new GetCustomerPaymentProfileRequest
                {
                    CustomerPaymentProfileId = customerPaymentProfileId,
                    CustomerProfileId        = customerProfileId,
                    UnmaskExpirationDate     = true
                };

                var paymentResponse = await _customerPaymentProfileClient.GetAsync(paymentRequest, cancellationToken);

                DisplayResponse(" GetCustomerPaymentProfileResponse", paymentResponse);
            }
        }

        _logger.LogWarning("CreateResponse - {responseCode}", parsedCreation.ResponseCode);

        // delete
        var deleteResponse = await _customerProfileClient.DeleteAsync(
            new DeleteCustomerProfileRequest
        {
            CustomerProfileId = createResponse.CustomerProfileId,
            RefId             = profile.ReferenceId
        },
            cancellationToken);

        DisplayResponse("DeleteResponse", deleteResponse);
    }
 public Task <ValidateCustomerPaymentProfileResponse> ValidateAsync(
     ValidateCustomerPaymentProfileRequest request,
     CancellationToken cancellationToken = default)
 {
     return(_validate.PostAsync(request, cancellationToken));
 }