예제 #1
0
        private IPaymentData CallInsertOrderWithPayment(PaymentRequest paymentRequest)
        {
            Guard.Against.Null(paymentRequest.Payment.PurchaseOrder.BillingAddress, "PurchaseOrder.BillingAddress must be supplied for Global Collect payments. Please make sure that you update the property prior to initiating payment either by using the API TransactionLibrary.EditBillingInformation() or setting the property directly.");
            var payment           = paymentRequest.Payment;
            var paymentMethod     = payment.PaymentMethod;
            var paymentProductId  = TryGetPaymentProductIdFromPayment(payment);
            var billingAddress    = paymentRequest.PurchaseOrder.BillingAddress;
            var shipment          = paymentRequest.PurchaseOrder.Shipments.FirstOrDefault();
            var amount            = paymentRequest.Amount.Value.ToCents();
            var currency          = paymentRequest.Amount.Currency.ISOCode;
            var language          = paymentRequest.Amount.Culture.TwoLetterISOLanguageName;
            var country           = TryGetCountryFromCustomerOrPayment(paymentRequest);
            var merchantReference = GetMerchantReference(payment);
            var ipAddress         = GetUserIpAddress();

            bool useAuthenticationIndicator = Force3DSecure(paymentProductId, paymentRequest);

            // NB! When I used the character "&" to add the "ADDITIONALREFERENCE" value, the parsing of the resulting XML would fail :-S
            // I changed it to "&" instead, and it appears to be ok. But since we have never seen a response yet, I do not know if the value actually would be added.
            var returnUrl = AbstractPageBuilder.GetCallbackUrl(paymentMethod.DynamicProperty <string>().CallbackUrl, paymentRequest.Payment);

            var globalCollectBillingAddress = new GlobalCollect.Api.Parts.Address()
            {
                FirstName   = billingAddress.FirstName,
                LastName    = billingAddress.LastName,
                PhoneNumber = billingAddress.PhoneNumber,
                StreetLine1 = billingAddress.Line1,
                StreetLine2 = billingAddress.Line2,
                Zip         = billingAddress.PostalCode,
                Email       = billingAddress.EmailAddress,
                IpAddress   = ipAddress,
                State       = billingAddress.State,
                City        = billingAddress.City,
                CompanyName = billingAddress.CompanyName,
                CountryCode = billingAddress.Country.TwoLetterISORegionName
            };

            var globalCollectShippingAddress = new GlobalCollect.Api.Parts.Address();

            if (shipment != null)
            {
                var shippingAddress = shipment.ShipmentAddress;
                if (shippingAddress != null)
                {
                    globalCollectShippingAddress.FirstName   = shippingAddress.FirstName;
                    globalCollectShippingAddress.LastName    = shippingAddress.LastName;
                    globalCollectShippingAddress.StreetLine1 = shippingAddress.Line1;
                    globalCollectShippingAddress.StreetLine2 = shippingAddress.Line2;
                    globalCollectShippingAddress.Zip         = shippingAddress.PostalCode;
                    globalCollectShippingAddress.State       = shippingAddress.State;
                    globalCollectShippingAddress.Email       = shippingAddress.EmailAddress;
                    globalCollectShippingAddress.City        = shippingAddress.City;
                    globalCollectShippingAddress.CountryCode = shippingAddress.Country.TwoLetterISORegionName;
                    globalCollectShippingAddress.CompanyName = shippingAddress.CompanyName;
                }
            }

            var createPaymentRequest = new CreatePaymentRequestDto(
                amount,
                currency,
                country,
                language,
                merchantReference,
                paymentProductId,
                returnUrl,
                globalCollectBillingAddress,
                globalCollectShippingAddress,
                useAuthenticationIndicator
                );

            var payments = _globalCollectService.CreatePayment(payment.PaymentMethod, createPaymentRequest).ToList();

            if (payments.Count() != 1)
            {
                throw new SecurityException("Did not receive the expected number of payments from Global Collect");
            }

            var paymentData = payments.Single();

            LogPaymentDataReceived(paymentData);

            payment["Mac"]              = paymentData.Mac;
            payment["Ref"]              = paymentData.Ref;
            payment["ReturnMac"]        = paymentData.ReturnMac;
            payment["OrderId"]          = paymentData.OrderId.ToString(CultureInfo.InvariantCulture);
            payment["PaymentProductId"] = paymentProductId.ToString(CultureInfo.InvariantCulture);
            payment["PaymentReference"] = paymentData.PaymentReference;
            payment.TransactionId       = paymentData.OrderId.ToString(CultureInfo.InvariantCulture);

            payment.Save();

            return(paymentData);
        }
        public virtual IEnumerable <IPaymentData> CreatePayment(PaymentMethod paymentMethod, CreatePaymentRequestDto request)
        {
            var insertOrder = new InsertOrderWithPayment
            {
                Meta =
                {
                    MerchantId = paymentMethod.DynamicProperty <int>().MerchantId
                },
                Order =
                {
                    Amount            = request.Amount,
                    CurrencyCode      = request.CurrencyCode,
                    CountryCode       = request.CountryCode,
                    LanguageCode      = _languageCodeMapper.Convert(request.LanguageCode),
                    MerchantReference = request.MerchantReference,

                    //Address fields
                    BillingAddress  = request.BillingAddress,
                    ShippingAddress = request.ShippingAddress
                },
                Payment =
                {
                    Amount                     = request.Amount,
                    CurrencyCode               = request.CurrencyCode,
                    CountryCode                = request.CountryCode,
                    LanguageCode               = request.LanguageCode,
                    PaymentProductId           = request.PaymentProductId,
                    ReturnUrl                  = request.ReturnUrl,
                    UseAuthenticationIndicator = request.UseAuthenticationIndicator
                }
            };

            var responseText = SendTextAndCheckResponseForErrors(GetServiceUrl(paymentMethod), insertOrder.ToString());

            var response = new InsertOrderWithPayment();

            response.FromModifiedXml(new ModifiedXmlDocument(responseText), string.Empty);

            return(new List <PaymentData>(response.Response.PaymentRows));
        }