简体   繁体   English

在Authorize.net SKD之上开发Laravel 4.2程序包-将常量放在何处?

[英]Laravel 4.2 package development on top of Authorize.net SKD - where to put Constant Variables?

I'm developing my first Laravel package in workbench to solve a business need which is to integrate the Authorize.net SDK for our systems billing transactions. 我正在工作台中开发我的第一个Laravel软件包,以解决业务需求,该需求是将Authorize.net SDK集成到我们的系统账单交易中。

I've got most of my code now working and I can call the SDK functions via my custom facade like so AuthorizeMe::authorizeAndCapture(); 现在我的大部分代码都可以使用,并且可以通过我的自定义外观调用SDK函数,例如AuthorizeMe :: authorizeAndCapture();。 My issue is the Authroize.net SDK requires use of defined variables. 我的问题是Authroize.net SDK需要使用定义的变量。 Their example usage is as follows: 其示例用法如下:

define("AUTHORIZENET_API_LOGIN_ID", "YOURLOGIN");
define("AUTHORIZENET_TRANSACTION_KEY", "YOURKEY");
define("AUTHORIZENET_SANDBOX", true);
$sale           = new AuthorizeNetAIM;
$sale->amount   = "5.99";
$sale->card_num = '6011000000000012';
$sale->exp_date = '04/15';
$response = $sale->authorizeAndCapture();
if ($response->approved) {
    $transaction_id = $response->transaction_id;
}

I would like to 1) store the defined variables in the packages config.php file, 2) I would like to make it so if I do publish the package other users can simply publish the config file to their app/config/packages folder so they can simply put in their unique api info. 我想1)将定义的变量存储在程序包config.php文件中,2)我要这样做,所以如果我发布程序包,其他用户可以简单地将配置文件发布到他们的app / config / packages文件夹中,这样他们可以简单地输入其独特的api信息。

I have tried to store the API variables in the config.php array as follows: 我试图将API变量存储在config.php数组中,如下所示:

return array(

    /*
    |--------------------------------------------------------------------------
    | Define credentials
    |--------------------------------------------------------------------------
    |
    | Your credentials for both live environment and sandbox
    |
    */

    // Live environment
    'LIVE_LOGIN_ID' => 'YOUR ID',
    'LIVE_TRANSACTION_KEY' => 'YOUR KEY',

    // Sandbox evnironment
    'SANDBOX_LOGIN_ID' => 'YOUR SANDBOX ID',
    'SANDBOX_TRANSACTION_KEY' => 'YOUR SANDBOX KEY',

    /*
    |--------------------------------------------------------------------------
    | Define environment
    |--------------------------------------------------------------------------
    |
    | Dictates if we're in sandbox mode or live mode
    |
    */

    'SANDBOX' => true,
);

I don't believe defined variables can be set from within a class so I'm not sure how to use the config file to accomplish my needs. 我不认为可以在一个类中设置定义的变量,所以我不确定如何使用配置文件来满足我的需求。

For example, something like this doesn't seem to work: 例如,类似这样的东西似乎不起作用:

class MyClass {
     public function __construct($app=null)
     {
       $this->app = $app ?: app();
     }

     public function billClient()
     {
       define("LIVE_LOGIN_ID", $this->app['config']->get('LIVE_LOGIN_ID'));
     }    
}

Lastly, I don't want to re-write the vendors package to simply work with my implementation as I feel it's important to make sure my package wrapper just sits on top so that it can pull in any future updates. 最后,我不想重写供应商软件包以仅与我的实现一起使用,因为我认为确保我的软件包包装程序位于最重要的位置,以便将来可以进行任何更新都是很重要的。

Store your static data that can change depending on what server your software is running on inside of the environment root-level configuration file . 将您的静态数据存储在环境根级别的配置文件中 ,该静态数据会根据您的软件在哪个服务器上运行而变化 I would suggest doing this also to avoid the possibility of your API keys getting committed into source control (which could potentially be damaging). 我建议这样做也是为了避免将API密钥提交到源代码管理中(这可能会造成破坏)。 You can also leverage this to have different configuration values in different environments. 您还可以利用它在不同的环境中具有不同的配置值。 Quoting from that linked to manual entry: 引用与手动输入相关的内容:

For "real" applications, it is advisable to keep all of your sensitive configuration out of your configuration files. 对于“实际”应用程序,建议将所有敏感配置都保留在配置文件之外。 Things such as database passwords, Stripe API keys, and encryption keys should be kept out of your configuration files whenever possible 尽可能将数据库密码,Stripe API密钥和加密密钥之类的内容排除在配置文件之外

The general practice is to keep these defined in a config file named .env.{environment name}.php which does not get checked in to source control. 通常的做法是将这些定义保存在名为.env.{environment name}.php的配置文件中,该文件不会签入源代码管理。 You can also specify them inside of the virtual host for the site if that works better for you, with eg SetEnv directives for Apache. 如果更适合您,您还可以在站点的虚拟主机内部指定它们,例如使用Apache的SetEnv指令。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM