Esempio n. 1
0
        public string Tokenize(bool validateCard, Address billingAddress, Customer customerData, string configName = "default")
        {
            TransactionType type = validateCard ? TransactionType.Verify : TransactionType.Tokenize;

            var builder = new AuthorizationBuilder(type, this)
                          .WithRequestMultiUseToken(validateCard)
                          .WithPaymentMethodUsageMode(PaymentMethodUsageMode.Multiple);

            if (billingAddress != null)
            {
                builder = builder.WithAddress(billingAddress);
            }
            if (customerData != null)
            {
                builder = builder.WithCustomerData(customerData);
            }

            var response = builder.Execute(configName);

            return(response.Token);
        }
Esempio n. 2
0
        protected async override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            string response = await request.Content.ReadAsStringAsync();

            // gather information
            var json        = JsonDoc.Parse(response, JsonEncoders.Base64Encoder);
            var timestamp   = json.GetValue <string>("TIMESTAMP");
            var merchantId  = json.GetValue <string>("MERCHANT_ID");
            var account     = json.GetValue <string>("ACCOUNT");
            var orderId     = json.GetValue <string>("ORDER_ID");
            var amount      = json.GetValue <string>("AMOUNT");
            var currency    = json.GetValue <string>("CURRENCY");
            var autoSettle  = json.GetValue <int>("AUTO_SETTLE_FLAG") == 1;
            var requestHash = json.GetValue <string>("SHA1HASH");

            // check hash
            var newhash = GenerationUtils.GenerateHash(_sharedSecret, timestamp, merchantId, orderId, amount, currency);

            if (!newhash.Equals(requestHash))
            {
                return(BadRequest("Incorrect hash. Please check your code and the Developers Documentation."));
            }

            // configure the container
            ServicesContainer.ConfigureService(new GatewayConfig {
                MerchantId   = merchantId,
                AccountId    = account,
                ServiceUrl   = "https://api.sandbox.realexpayments.com/epage-remote.cgi",
                SharedSecret = _sharedSecret
            }, "realexResponder");

            // gather additional information
            var shippingCode    = json.GetValue <string>("SHIPPING_CODE");
            var shippingCountry = json.GetValue <string>("SHIPPING_CO");
            var billingCode     = json.GetValue <string>("BILLING_CODE");
            var billingCountry  = json.GetValue <string>("BILLING_CO");

            // build request
            AuthorizationBuilder gatewayRequest = null;

            if (amount == null)
            {
                var validate = json.GetValue <int>("VALIDATE_CARD_ONLY") == 1;
                if (validate)
                {
                    gatewayRequest = _card.Verify();
                }
                else
                {
                    gatewayRequest = _card.Verify().WithRequestMultiUseToken(true);
                }
            }
            else
            {
                if (autoSettle)
                {
                    gatewayRequest = _card.Charge(amount.ToAmount());
                }
                else
                {
                    gatewayRequest = _card.Authorize(amount.ToAmount());
                }
            }

            try {
                gatewayRequest.WithCurrency(currency).WithOrderId(orderId).WithTimestamp(timestamp);
                if (billingCode != null || billingCountry != null)
                {
                    gatewayRequest.WithAddress(new Address {
                        PostalCode = billingCode, Country = billingCountry
                    });
                }
                if (shippingCode != null || shippingCountry != null)
                {
                    gatewayRequest.WithAddress(new Address {
                        PostalCode = shippingCode, Country = shippingCountry
                    }, AddressType.Shipping);
                }

                var gatewayResponse = gatewayRequest.Execute("realexResponder");
                if (gatewayResponse.ResponseCode.Equals("00"))
                {
                    return(BuildResponse(HttpStatusCode.OK, ConvertResponse(json, gatewayResponse)));
                }
                else
                {
                    return(BadRequest(gatewayResponse.ResponseMessage));
                }
            }
            catch (ApiException exc) {
                return(ServerError(exc.Message));
            }
        }
Esempio n. 3
0
        protected async override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            string response = await request.Content.ReadAsStringAsync();

            // gather information
            var json           = JsonDoc.Parse(response, JsonEncoders.Base64Encoder);
            var timestamp      = json.GetValue <string>("TIMESTAMP");
            var merchantId     = json.GetValue <string>("MERCHANT_ID");
            var account        = json.GetValue <string>("ACCOUNT");
            var orderId        = json.GetValue <string>("ORDER_ID");
            var amount         = json.GetValue <string>("AMOUNT");
            var currency       = json.GetValue <string>("CURRENCY");
            var autoSettle     = json.GetValue <int>("AUTO_SETTLE_FLAG") == 1;
            var description    = json.GetValue <string>("COMMENT1");
            var shaHashTagName = _shaHashType + "HASH";
            var requestHash    = json.GetValue <string>(shaHashTagName);

            // gather additional information
            var shippingCode    = json.GetValue <string>("SHIPPING_CODE");
            var shippingCountry = json.GetValue <string>("SHIPPING_CO");
            var billingCode     = json.GetValue <string>("BILLING_CODE");
            var billingCountry  = json.GetValue <string>("BILLING_CO");
            var fraudFilterMode = json.GetValue <string>("HPP_FRAUDFILTER_MODE");


            List <string> hashParam = new List <string>
            {
                timestamp,
                merchantId,
                orderId,
                amount,
                currency
            };

            //create the card/APM/LPM/OB object
            if (json.Has("PM_METHODS"))
            {
                string[] apmTypes = json.GetValue <string>("PM_METHODS").Split("|");
                string   apmType  = apmTypes[0];

                //OB
                if (apmTypes.Contains(HostedPaymentMethods.OB.ToString()))
                {
                    var card = new BankPayment {
                        SortCode        = json.GetValue <string>("HPP_OB_DST_ACCOUNT_SORT_CODE"),
                        AccountNumber   = json.GetValue <string>("HPP_OB_DST_ACCOUNT_NUMBER"),
                        AccountName     = json.GetValue <string>("HPP_OB_DST_ACCOUNT_NAME"),
                        BankPaymentType = (BankPaymentType)(Enum.Parse(typeof(BankPaymentType), json.GetValue <string>("HPP_OB_PAYMENT_SCHEME"))),
                        Iban            = json.GetValue <string>("HPP_OB_DST_ACCOUNT_IBAN"),
                        ReturnUrl       = json.GetValue <string>("MERCHANT_RESPONSE_URL"),
                        StatusUpdateUrl = json.GetValue <string>("HPP_TX_STATUS_URL")
                    };

                    paymentMethod = card;

                    if (!string.IsNullOrEmpty(card.SortCode))
                    {
                        hashParam.Add(card.SortCode);
                    }
                    if (!string.IsNullOrEmpty(card.AccountNumber))
                    {
                        hashParam.Add(card.AccountNumber);
                    }
                    if (!string.IsNullOrEmpty(card.Iban))
                    {
                        hashParam.Add(card.Iban);
                    }
                }
                else
                {
                    AlternativePaymentMethod apm = new AlternativePaymentMethod();
                    apm.AlternativePaymentMethodType = (AlternativePaymentType)(Enum.Parse(typeof(AlternativePaymentType), apmType));
                    apm.ReturnUrl       = json.GetValue <string>("MERCHANT_RESPONSE_URL");
                    apm.StatusUpdateUrl = json.GetValue <string>("HPP_TX_STATUS_URL");

                    if (apmType.Equals(AlternativePaymentType.PAYPAL.ToString()))
                    {
                        apm.CancelUrl = "https://www.example.com/failure/cancelURL";
                    }
                    apm.Country           = json.GetValue <string>("HPP_CUSTOMER_COUNTRY");
                    apm.AccountHolderName = json.GetValue <string>("HPP_CUSTOMER_FIRSTNAME") + " " + json.GetValue <string>("HPP_CUSTOMER_LASTNAME");

                    paymentMethod = apm;
                }
            }
            else
            {
                CreditCardData card = new CreditCardData {
                    Number         = "4111111111111111",
                    ExpMonth       = 12,
                    ExpYear        = 2025,
                    Cvn            = "123",
                    CardHolderName = "John Smithe"
                };

                paymentMethod = card;
            }

            //for stored card
            if (json.Has("OFFER_SAVE_CARD"))
            {
                if (json.Has("PAYER_REF"))
                {
                    hashParam.Add(json.GetValue <string>("PAYER_REF"));
                }
                if (json.Has("PMT_REF"))
                {
                    hashParam.Add(json.GetValue <string>("PMT_REF"));
                }
            }

            if (json.Has("HPP_FRAUDFILTER_MODE"))
            {
                hashParam.Add(json.GetValue <string>("HPP_FRAUDFILTER_MODE"));
            }

            // check hash
            var newhash = GenerationUtils.GenerateHash(_sharedSecret, hashParam.ToArray());

            if (!newhash.Equals(requestHash))
            {
                return(BadRequest("Incorrect hash. Please check your code and the Developers Documentation."));
            }

            // configure the container
            ServicesContainer.ConfigureService(new GpEcomConfig {
                MerchantId    = merchantId,
                AccountId     = account,
                SharedSecret  = _sharedSecret,
                RequestLogger = new RequestConsoleLogger()
            }, "realexResponder");

            // build request
            AuthorizationBuilder gatewayRequest = null;

            if (amount.ToAmount().Equals(0m) || amount == null)
            {
                var validate = json.GetValue <int>("VALIDATE_CARD_ONLY") == 1;
                if (validate)
                {
                    gatewayRequest = ((CreditCardData)paymentMethod).Verify();
                }
                else
                {
                    gatewayRequest = ((CreditCardData)paymentMethod).Verify().WithRequestMultiUseToken(true);
                }
            }
            else
            {
                if (autoSettle)
                {
                    if (paymentMethod is CreditCardData)
                    {
                        gatewayRequest = ((CreditCardData)paymentMethod).Charge(amount.ToAmount());
                    }
                    if (paymentMethod is AlternativePaymentMethod)
                    {
                        gatewayRequest = ((AlternativePaymentMethod)paymentMethod).Charge(amount.ToAmount());
                    }
                    if (paymentMethod is BankPayment)
                    {
                        var gatewayBankRequest = AddRemittanceRef(((BankPayment)paymentMethod).Charge(amount.ToAmount())
                                                                  .WithCurrency(currency)
                                                                  .WithDescription(description), json);
                        var gatewayResponse = gatewayBankRequest.Execute();
                        if (gatewayResponse.BankPaymentResponse.PaymentStatus.Equals("PAYMENT_INITIATED"))
                        {
                            return(BuildResponse(HttpStatusCode.OK, ConvertResponse(json, gatewayResponse)));
                        }
                        else
                        {
                            return(BadRequest(gatewayResponse.ResponseMessage));
                        }
                    }
                }
                else
                {
                    gatewayRequest = ((CreditCardData)paymentMethod).Authorize(amount.ToAmount());
                }
            }

            try {
                gatewayRequest.WithCurrency(currency).WithOrderId(orderId).WithTimestamp(timestamp);
                if (billingCode != null || billingCountry != null)
                {
                    gatewayRequest.WithAddress(new Address {
                        PostalCode = billingCode, Country = billingCountry
                    });
                }
                if (shippingCode != null || shippingCountry != null)
                {
                    gatewayRequest.WithAddress(new Address {
                        PostalCode = shippingCode, Country = shippingCountry
                    }, AddressType.Shipping);
                }

                if (fraudFilterMode != null)
                {
                    gatewayRequest.WithFraudFilter((FraudFilterMode)Enum.Parse(typeof(FraudFilterMode), fraudFilterMode), getFraudFilterRules(json));
                }

                var gatewayResponse = gatewayRequest.Execute("realexResponder");
                if (gatewayResponse.ResponseCode.Equals("00") || gatewayResponse.ResponseCode.Equals("01"))
                {
                    return(BuildResponse(HttpStatusCode.OK, ConvertResponse(json, gatewayResponse)));
                }
                else
                {
                    return(BadRequest(gatewayResponse.ResponseMessage));
                }
            }
            catch (ApiException exc) {
                return(ServerError(exc.Message));
            }
        }