【问题标题】:Retrieve company name with GoCardless webhook使用 GoCardless webhook 检索公司名称
【发布时间】:2018-04-28 10:42:41
【问题描述】:

我正在为计费项目使用 GoCardless(沙盒帐户)网络挂钩。我想恢复选择使用 GoCardless 付款的客户。问题是我从 GoCardless 获得的 JSON 中没有这样的信息。

我需要公司名称才能将授权编号放入我所有客户所在的 sql 数据库中。

有没有办法在创建授权时获取公司名称?或者将授权编号与公司名称相关联?

这是我的代码:

<?php
putenv('GC_WEBHOOK_SECRET=my_secret...');
$token = getenv("GC_WEBHOOK_SECRET");
$raw_payload = file_get_contents('php://input');
$headers = getallheaders();
$provided_signature = $headers["Webhook-Signature"];
$calculated_signature = hash_hmac("sha256", $raw_payload, $token);
if ($provided_signature == $calculated_signature) {
  $payload = json_decode($raw_payload, true);
    foreach ($payload["events"] as $event) {
      if ($event["resource_type"]=="mandates" && $event["action"]=="created"){
                $mandates=$event["links"];
                $mandate=$mandates["mandate"];
                try
                {
                    $pdo_options[PDO::ATTR_ERRMODE] = PDO::ERRMODE_EXCEPTION;
                    $bdd = new PDO('sqlsrv...', $pdo_options);
                    $reqi="INSERT INTO goCardLess (client) VALUES ('".$mandate."')";
                    $req = $bdd->query($reqi);
                    $req->closeCursor();
                }
                catch (Exception $e)
                {
                    die('Erreur : ' . $e->getMessage());
                }
                header("HTTP/1.1 200 OK");
                }
              }
            }
 else {
    header("HTTP/1.1 498 Invalid Token test");
  }
?>

【问题讨论】:

  • 您能否举一些您尝试过的示例并指出您需要的信息应该如何看待?
  • 是的,我已编辑,希望对您有所帮助

标签: webhooks gocardless


【解决方案1】:

GoCardless webhook events 不包含任何敏感数据 - 它们提供资源 ID,因此您可以在需要时从 API 中检索它们。

“创建授权”事件包含授权 ID,API reference for mandates 表明授权响应包含客户 ID(客户资源是您可以找到他们公司名称的位置。)

这意味着你需要通过APIretrieve the mandate,然后retrieve the customer:

if ($event["resource_type"]=="mandates" && $event["action"]=="created") {
    $mandate_id = $event["links"]["mandate"];
    $mandate = $client->mandates()->get($mandate_id);

    $customer_id = $mandate->links->customer;
    $customer = $client->customers()->get($customer_id);

    $company_name = $customer->company_name;
}

这假设您已经将installed and initialised the client library 设为$client。

【讨论】:

    猜你喜欢
    • 2021-09-19
    • 1970-01-01
    • 1970-01-01
    • 2019-10-06
    • 2010-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    相关资源
    最近更新 更多