gpt4 book ai didi

php - 如何使用 paypal webhooks 处理多个环境 : dev, 登台和生产?

转载 作者:行者123 更新时间:2023-12-02 02:46:03 29 4
gpt4 key购买 nike

我想要三个环境,可以在其中测试和使用 paypal webhooks。我当前的方法是为 paypal 开发人员门户中的每个环境创建新的 webhook,并在请求到达时检查它是否适用于该环境。 paypal 如何处理多个 webhook url 以及如果环境不正确应返回什么状态代码?

最佳答案

创建一个如下所示的类以保留在应用程序的开头。让它初始化一次。

class App {
private static $env_status = null;
private static $paypal_settings = [];

const ENV_PRODUCTION = "production";
const ENV_STAGING = "staging";
const ENV_DEV = "development";

public static function init() {
// Set environment status.
// You can also parse domain name and depending on your domain, you can set the environment status.
self::$env_status = getenv("ENV_STATUS"); // getenv() Gets from Environment variable. You'll need set clear_env = no in php config for this to work.

switch(self::$env_status) {
case App::ENV_PRODUCTION:
self::$paypal_settings = [
"mode" => "live"
"clientID" => "PRODUCTION_CLIENT_ID" ,
"secret" => "PRODUCTION_SECRET" ,
"currency" => "USD",
"webhook" => "https://example.com/live_webhook_endpoint"
];

break;

case App::ENV_STAGING:
self::$paypal_settings = [
"mode"=> "sandbox"
"clientID"=> "STAGING_CLIENT_ID" ,
"secret"=> "STAGING_SECRET" ,
"currency"=> "USD",
"webhook" => "https://example.com/staging_webhook_endpoint"
];

break;

default:
// ENV_DEV settings
self::$paypal_settings = [
"mode"=> "sandbox"
"clientID"=> "DEVELOPMENT_CLIENT_ID" ,
"secret"=> "DEVELOPMENT_SECRET" ,
"currency"=> "USD",
"webhook" => "https://example.com/development_webhook_endpoint"
];
break;
}
}

public static function env_status() {
return self::$env_status;
}

public static function paypal_settings() {
return self::$paypal_settings;
}

// You can also create seprate function if you just want webhook URL.
// You can define in different variable also if that's the case.
public static function paypal_webhook_url() {
return self::$paypal_settings['webhook'];
}

} App::init();

然后,每当您想要获取 PayPal 设置时,您都可以从应用程序中的任何位置调用它。

$paypay_settings = App::paypal_settings();

或者如果您只需要 paypal webhook URL

$paypal_webhook_url = App::paypal_webhook_url();

这样您就不必在代码的其他部分保留任何条件。所有条件都将放在一个位置,以后更新起来会更容易。

paypal 如何处理多个 webhook url。

您需要点击 PayPal Sandbox URL 才能进入临时/开发环境。

如果环境不正确,应该返回什么状态代码?

HTTP 400。因为这将是一个无效请求。

Ref: https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400

The HyperText Transfer Protocol (HTTP) 400 Bad Request response status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

关于php - 如何使用 paypal webhooks 处理多个环境 : dev, 登台和生产?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62775393/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com