Sina-Soltani / Parbad

A free, open-source, integrated and extensible library which connects your web applications to online payment gateways. Gateways can be added or developed by you.
GNU Lesser General Public License v3.0
378 stars 83 forks source link

طریقه کانفیگ کل پرباد به اپلیکیشن کور3 #136

Closed Mansoun closed 4 years ago

Mansoun commented 4 years ago

salam. mishe chizi shabih be populate ro tooye ConfigureService Core3 ajnam dad? too mvc rahat zadam parbod ro ama nemikham startup sholoogh beshe, tooye mesal haa didam ke oomadin melat ro configure kardid vali man daram dynamic tanzimat ro migiram. mamnoon. classe config ro gozashtam. mamnoon

`namespace Acsan.Web.Language.Models {

public class SamanAccountSource : IGatewayAccountSource<SamanGatewayAccount>
{
    private readonly IBankManagementRepository _bankmanagement;
    public SamanAccountSource(IBankManagementRepository bankManagement)
    {
        _bankmanagement = bankManagement;
    }

    public async Task AddAccountsAsync(IGatewayAccountCollection<SamanGatewayAccount> accounts)
    {
        var settings = await _bankmanagement.findByEnumName(OnlinePaymentBankEnum.Saman);

        accounts.Add(new SamanGatewayAccount
        {
            MerchantId = settings.MerchantId,
            Name = settings.Name,
            Password = settings.Password
        });
    }
}
public class PasargadAccountSource : IGatewayAccountSource<PasargadGatewayAccount>
{
    private readonly IBankManagementRepository _bankmanagement;
    public PasargadAccountSource(IBankManagementRepository bankManagement)
    {
        _bankmanagement = bankManagement;
    }
    public async Task AddAccountsAsync(IGatewayAccountCollection<PasargadGatewayAccount> accounts)
    {
        var settings = await _bankmanagement.findByEnumName(OnlinePaymentBankEnum.Pasargad);

        accounts.Add(new PasargadGatewayAccount
        {
            MerchantCode = settings.MerchantId,
            Name = settings.Name,
            PrivateKey = settings.PrivateKey,
            TerminalCode = settings.TerminalKey
        });
    }
}
public class ParsianAccountSourse : IGatewayAccountSource<ParsianGatewayAccount>
{
    private readonly IBankManagementRepository _bankmanagement;
    public ParsianAccountSourse(IBankManagementRepository bankManagement)
    {
        _bankmanagement = bankManagement;
    }

    public async Task AddAccountsAsync(IGatewayAccountCollection<ParsianGatewayAccount> accounts)
    {
        var settings = await _bankmanagement.findByEnumName(OnlinePaymentBankEnum.Parsian);

        accounts.Add(new ParsianGatewayAccount
        {
            LoginAccount = settings.UserName,
            Name = settings.Name
        });
    }
}
public class IranKishAccountSourse : IGatewayAccountSource<IranKishGatewayAccount>
{
    private readonly IBankManagementRepository _bankmanagement;
    public IranKishAccountSourse(IBankManagementRepository bankManagement)
    {
        _bankmanagement = bankManagement;
    }
    public async Task AddAccountsAsync(IGatewayAccountCollection<IranKishGatewayAccount> accounts)
    {
        var settings = await _bankmanagement.findByEnumName(OnlinePaymentBankEnum.IranKish);

        accounts.Add(new IranKishGatewayAccount
        {
            MerchantId = settings.MerchantId,
            Name = settings.Name
        });
    }
}
public class AsanPardakhtAccountSource : IGatewayAccountSource<AsanPardakhtGatewayAccount>
{
    private readonly IBankManagementRepository _bankmanagement;
    public AsanPardakhtAccountSource(IBankManagementRepository bankmanagement)
    {
        _bankmanagement = bankmanagement;
    }
    public async Task AddAccountsAsync(IGatewayAccountCollection<AsanPardakhtGatewayAccount> accounts)
    {
        var settings = await _bankmanagement.findByEnumName(OnlinePaymentBankEnum.AsanPardakht);

        accounts.Add(new AsanPardakhtGatewayAccount
        {
            IV = settings.IV,
            Key = settings.PrivateKey,
            MerchantConfigurationId = settings.MerchantId,
            Name = settings.Name,
            Password = settings.Password,
            UserName = settings.UserName
        });
    }
}
public class MelliAccountSource : IGatewayAccountSource<MelliGatewayAccount>
{
    private readonly IBankManagementRepository _bankmanagement;
    public MelliAccountSource(IBankManagementRepository bankmanagement)
    {
        _bankmanagement = bankmanagement;
    }
    public async Task AddAccountsAsync(IGatewayAccountCollection<MelliGatewayAccount> accounts)
    {
        var settings = await _bankmanagement.findByEnumName(OnlinePaymentBankEnum.Melli);

        accounts.Add(new MelliGatewayAccount
        {
            MerchantId = settings.MerchantId,
            Name = settings.Name,
            TerminalId = settings.TerminalID,
            TerminalKey = settings.TerminalKey
        });
    }
}
public class MellatAccountSource : IGatewayAccountSource<MellatGatewayAccount>
{
    private readonly IBankManagementRepository _bankmanagement;

    public MellatAccountSource(IBankManagementRepository bankmanagement)
    {
        _bankmanagement = bankmanagement;
    }

    public async Task AddAccountsAsync(IGatewayAccountCollection<MellatGatewayAccount> accounts)
    {
        var settings = await _bankmanagement.findByEnumName(OnlinePaymentBankEnum.Mellat);

        accounts.Add(new MellatGatewayAccount
        {
            TerminalId = Convert.ToInt64(settings.TerminalID),
            UserName = settings.UserName,
            UserPassword = settings.Password
        });
    }

}
public class ZarrinPalSource : IGatewayAccountSource<ZarinPalGatewayAccount>
{
    private readonly IBankManagementRepository _bankmanagement;
    public ZarrinPalSource(IBankManagementRepository bankmanagement)
    {
        _bankmanagement = bankmanagement;
    }
    public async Task AddAccountsAsync(IGatewayAccountCollection<ZarinPalGatewayAccount> accounts)
    {
        var settings = await _bankmanagement.findByEnumName(OnlinePaymentBankEnum.ZarinPal);

        accounts.Add(new ZarinPalGatewayAccount
        {
            Name = settings.Name,
            MerchantId = settings.MerchantId,
            IsSandbox = true
        });
    }
}

public static class ParbadConfig
{
    public static IParbadBuilder Configure()
    {
        return ParbadBuilder.CreateDefaultBuilder()
            .ConfigureGateways(gateways =>
            {
                gateways.AddMellat()
                 .WithAccounts(source =>
                 {
                     source.Add<MellatAccountSource>(ServiceLifetime.Transient);
                 });
                gateways.AddZarinPal()
                .WithAccounts(source =>
                {
                    source.Add<ZarrinPalSource>(ServiceLifetime.Transient);
                });
                gateways.AddMelli()
                .WithAccounts(source =>
                {
                    source.Add<MelliAccountSource>(ServiceLifetime.Transient);
                });
                gateways.AddIranKish()
                .WithAccounts(source =>
                {
                    source.Add<IranKishAccountSourse>(ServiceLifetime.Transient);
                });
                gateways.AddSaman()
                .WithAccounts(source =>
                {
                    source.Add<SamanAccountSource>(ServiceLifetime.Transient);
                });
                gateways.AddParsian()
                .WithAccounts(source =>
                {
                    source.Add<ParsianAccountSourse>(ServiceLifetime.Transient);
                });
                gateways.AddAsanPardakht()
                .WithAccounts(source =>
                {
                    source.Add<AsanPardakhtAccountSource>(ServiceLifetime.Transient);
                });
                gateways.AddPasargad()
                .WithAccounts(source =>
                {
                    source.Add<PasargadAccountSource>(ServiceLifetime.Transient);
                });
                gateways
                     .AddParbadVirtual()
                     .WithOptions(options => options.GatewayPath = "/MyVirtualGateway");
            })
            .ConfigureHttpContext(builder => builder.UseDefaultAspNetCore())
            .ConfigureStorage(builder => builder.UseMemoryCache());
    }
}

} `

Sina-Soltani commented 4 years ago

درود

متوجه منظور شما از populate نشدم دقیقا

اگر منظورتون این هست که در ASP.NET CORE -> Startup.cs میخواین تنظیم پَرباد شلوغ نباشه، میتونید به راحتی یک اکستنشن متد بنویسید یا حتی یک کلاس ساده استاتیک در هر جایی از پروژتون که میخواین

مثال:

public static class ParbadConfigExtensions
{
     public static void ConfigureParbad(this IServiceCollection services)
     {
         // All Parbad settings go here
     }
}

و بعد در Startup به صورت زیر استفادش کنید

public class Startup
{
     public void ConfigureServices(IServiceCollection services)
     {
         services.AddMvc();

         services.ConfigureParbad();
     }
}

شما همچنین نیازی به نوشتن یک کلاس به ازای هر درگاه ندارید میتونید همه رو داخل یک کلاس کاملا تمیز و در یک مکان پیاده سازی کنید مثال:

public class GatewaySources :
        IGatewayAccountSource<MellatGatewayAccount>,
        IGatewayAccountSource<ParsianGatewayAccount>,
        IGatewayAccountSource<SamanGatewayAccount>
{
     public Task AddAccountsAsync(IGatewayAccountCollection<MellatGatewayAccount> accounts)
     {
     }

     public Task AddAccountsAsync(IGatewayAccountCollection<ParsianGatewayAccount> accounts)
     {
     }

     public Task AddAccountsAsync(IGatewayAccountCollection<SamanGatewayAccount> accounts)
     {
     }
}

هر چند که در تنظیمات باید به ازای هر درگاه،‌ یکبار این کلاس رو معرفی کنید. البته ویژگی جدیدی در حال توسعه هست که این مورد آخر رو هم ساده تر و به صورت یکبار استفاده،‌ میسر میکنه

موفق باشید

Mansoun commented 4 years ago

بابت ویژگی و آپدیت هایی که میذارید ممنون. آره این نوع از پیاده سازی قطعا تمیزتره. ممنون