Exemplo n.º 1
0
        public static WebClient WePayWebClient(WePayPaymentSettings _wepayPaymentSettings)
        {
            WebClient client = new WebClient();

            client.Headers.Add("Authorization", "Bearer " + _wepayPaymentSettings.AccessToken);
            client.Headers.Add("Content-Type", "application/json");
            client.Headers.Add("User-Agent", "NopCommerce WePay Plugin");

            return(client);
        }
        public override void Install()
        {
            //settings
            var settings = new WePayPaymentSettings()
            {
                UseSandbox            = true,
                TransactMode          = TransactMode.Create,
                PdtValidateOrderTotal = true,
                CheckoutType          = "SERVICE",
                Currency = _currencyService.GetCurrencyById(_currencySettings.PrimaryStoreCurrencyId).CurrencyCode
            };

            _settingService.SaveSetting(settings);

            //locales

            this.AddOrUpdatePluginLocaleResource("Plugins.Payments.WePay.Fields.UseSandbox", "Use Sandbox");
            this.AddOrUpdatePluginLocaleResource("Plugins.Payments.WePay.Fields.UseSandbox.Hint", "Check to enable Sandbox (testing environment).");
            this.AddOrUpdatePluginLocaleResource("Plugins.Payments.WePay.Fields.TransactModeValues", "Transaction Mode");
            this.AddOrUpdatePluginLocaleResource("Plugins.Payments.WePay.Fields.TransactModeValues.Hint", "Choose transaction mode");
            this.AddOrUpdatePluginLocaleResource("Plugins.Payments.WePay.Fields.AccessToken", "Access Token");
            this.AddOrUpdatePluginLocaleResource("Plugins.Payments.WePay.Fields.AccessToken.Hint", "Specify your WePay Access Token.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Payments.WePay.Fields.AccountId", "Account ID");
            this.AddOrUpdatePluginLocaleResource("Plugins.Payments.WePay.Fields.AccountId.Hint", "Specify your WePay Account ID");
            this.AddOrUpdatePluginLocaleResource("Plugins.Payments.WePay.Fields.ClientSecret", "Client Secret");
            this.AddOrUpdatePluginLocaleResource("Plugins.Payments.WePay.Fields.ClientSecret.Hint", "Specify your WePay ClientSecret.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Payments.WePay.Fields.ClientId", "Client ID");
            this.AddOrUpdatePluginLocaleResource("Plugins.Payments.WePay.Fields.ClientId.Hint", "Specify your WePay Client ID.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Payments.WePay.Fields.PDTValidateOrderTotal", "Validate Order Total");
            this.AddOrUpdatePluginLocaleResource("Plugins.Payments.WePay.Fields.PDTValidateOrderTotal.Hint", "Check if PDT (payment data transfer) handler should validate order totals.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Payments.WePay.Fields.AdditionalFee", "Additional Fee");
            this.AddOrUpdatePluginLocaleResource("Plugins.Payments.WePay.Fields.AdditionalFee.Hint", "Enter additional fee to charge your customers.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Payments.WePay.Fields.AdditionalFeePercentage", "Additional Fee Use Percentage");
            this.AddOrUpdatePluginLocaleResource("Plugins.Payments.WePay.Fields.AdditionalFeePercentage.Hint", "Determines whether to apply a percentage additional fee to the order total. If not enabled, a fixed value is used.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Payments.WePay.Fields.RedirectUrl", "Redirect Url (After Payment)");
            this.AddOrUpdatePluginLocaleResource("Plugins.Payments.WePay.Fields.RedirectUrl.Hint", "Leave blank to use the default Redirect Url.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Payments.WePay.Fields.RedirecionTip", "You will be redirected to WePay site to complete the order.");

            this.AddOrUpdatePluginLocaleResource("Plugins.Payments.WePay.Fields.FeePayer", "WePay Fee Payer");
            this.AddOrUpdatePluginLocaleResource("Plugins.Payments.WePay.Fields.FeePayer.Hint", "Who will pay the WePay transaction fee?");
            this.AddOrUpdatePluginLocaleResource("Plugins.Payments.WePay.Fields.RequireShipping", "Require Shipping Address");
            this.AddOrUpdatePluginLocaleResource("Plugins.Payments.WePay.Fields.RequireShipping.Hint", "If set to true then the payer will be asked to enter a shipping address when they pay.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Payments.WePay.Fields.FundingSources", "Funding Sources");
            this.AddOrUpdatePluginLocaleResource("Plugins.Payments.WePay.Fields.FundingSources.Hint", "What funding sources you want to accept for this checkout.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Payments.WePay.Fields.CheckoutType", "Checkout Type");
            this.AddOrUpdatePluginLocaleResource("Plugins.Payments.WePay.Fields.CheckoutType.Hint", "The checkout type (one of the following: GOODS, SERVICE, DONATION, EVENT or PERSONAL)");

            base.Install();
        }
 public WePayPaymentProcessor(WePayPaymentSettings wepayStandardPaymentSettings,
                              ISettingService settingService, ICurrencyService currencyService,
                              CurrencySettings currencySettings, IWebHelper webHelper,
                              ICheckoutAttributeParser checkoutAttributeParser, ITaxService taxService,
                              IOrderTotalCalculationService orderTotalCalculationService, HttpContextBase httpContext,
                              IStoreService storeService, ILogger logger, IStoreContext storeContext, ICustomerService customerService)
 {
     this._wepayPaymentSettings         = wepayStandardPaymentSettings;
     this._settingService               = settingService;
     this._currencyService              = currencyService;
     this._currencySettings             = currencySettings;
     this._webHelper                    = webHelper;
     this._checkoutAttributeParser      = checkoutAttributeParser;
     this._taxService                   = taxService;
     this._orderTotalCalculationService = orderTotalCalculationService;
     this._httpContext                  = httpContext;
     this._storeService                 = storeService;
     this._logger          = logger;
     this._storeContext    = storeContext;
     this._customerService = customerService;
 }
        /// <summary>
        /// Process a payment
        /// </summary>
        /// <param name="processPaymentRequest">Payment info required for an order processing</param>
        /// <returns>Process payment result</returns>
        public ProcessPaymentResult ProcessPayment(ProcessPaymentRequest processPaymentRequest)
        {
            var result = new ProcessPaymentResult()
            {
                NewPaymentStatus = PaymentStatus.Pending
            };
            var customer = _customerService.GetCustomerById(processPaymentRequest.CustomerId);

            WebClient client      = WePayPaymentHelper.WePayWebClient(_wepayPaymentSettings);
            string    returnUrl   = _webHelper.GetStoreLocation(_storeContext.CurrentStore.SslEnabled) + "Plugins/PaymentWePay/PDTHandler";
            string    callbackUrl = _webHelper.GetStoreLocation(_storeContext.CurrentStore.SslEnabled) + "Plugins/PaymentWePay/WCBHandler";

            var orderTotal = Math.Round(processPaymentRequest.OrderTotal, 2);
            var shipping   = 0;

            CheckoutCreateCaptureRequest outrequest = new CheckoutCreateCaptureRequest
            {
                account_id        = _wepayPaymentSettings.AccountId,
                type              = _wepayPaymentSettings.CheckoutType,
                amount            = orderTotal,
                short_description = _storeService.GetStoreById(processPaymentRequest.StoreId).Name,
                long_description  = string.Format("{0} order #{1}", _storeService.GetStoreById(processPaymentRequest.StoreId).Name, processPaymentRequest.OrderGuid),
                reference_id      = processPaymentRequest.OrderGuid.ToString(),
                auto_capture      = (_wepayPaymentSettings.TransactMode == TransactMode.CreateAndCapture),
                redirect_uri      = returnUrl,
                callback_uri      = callbackUrl,
                fee_payer         = _wepayPaymentSettings.FeePayer,
                require_shipping  = _wepayPaymentSettings.RequireShipping,
                currency          = _wepayPaymentSettings.Currency,
                funding_sources   = _wepayPaymentSettings.FundingSources,
                shipping_fee      = shipping,
                prefill_info      = new PrefillInfo()
                {
                    name = customer.BillingAddress.FirstName + " " + customer.BillingAddress.LastName, email = customer.BillingAddress.Email
                }
            };
            string uriString = GetWePayUrl() + outrequest.actionUrl;
            var    data      = JsonConvert.SerializeObject(outrequest);

            try
            {
                var clientresp = client.UploadString(new Uri(uriString), "POST", data);

                dynamic response = JsonConvert.DeserializeObject(clientresp);
                var     settings = new WePayPaymentSettings()
                {
                    checkout_uri            = response.checkout_uri,
                    UseSandbox              = _wepayPaymentSettings.UseSandbox,
                    TransactMode            = _wepayPaymentSettings.TransactMode,
                    AccessToken             = _wepayPaymentSettings.AccessToken,
                    AccountId               = _wepayPaymentSettings.AccountId,
                    ClientSecret            = _wepayPaymentSettings.ClientSecret,
                    AdditionalFee           = _wepayPaymentSettings.AdditionalFee,
                    AdditionalFeePercentage = _wepayPaymentSettings.AdditionalFeePercentage,
                    ClientId = _wepayPaymentSettings.ClientId,
                    PdtValidateOrderTotal = _wepayPaymentSettings.PdtValidateOrderTotal,
                    FeePayer        = _wepayPaymentSettings.FeePayer,
                    RequireShipping = _wepayPaymentSettings.RequireShipping,
                    Currency        = _wepayPaymentSettings.Currency,
                    FundingSources  = _wepayPaymentSettings.FundingSources,
                    CheckoutType    = _wepayPaymentSettings.CheckoutType
                };
                _settingService.SaveSetting(settings);
            }
            catch (WebException we)
            {
                if (we.Status == WebExceptionStatus.ProtocolError)
                {
                    HttpWebResponse httpErrorResponse = (HttpWebResponse)we.Response as HttpWebResponse;

                    StreamReader reader       = new StreamReader(httpErrorResponse.GetResponseStream(), Encoding.UTF8);
                    string       responseBody = reader.ReadToEnd();
                    var          errResp      = JsonConvert.DeserializeObject <ErrorResponse>(responseBody);
                    result.AddError(string.Format("Error: {0};" + Environment.NewLine + " Error Message: {1};" + Environment.NewLine + " Error Description: {2}", errResp.error, errResp.error_description, we.Message));
                }
                else
                {
                    result.AddError(string.Format("Error: {0}", we.Message));
                }
            }
            return(result);
        }