需要以json形式传递元数据.Paystack



我最近开始使用paystack,它类似于stripe,但我在传递元数据时遇到了问题。我不知道我是否正确,因为我没有能够从我的webhook url参考它。我已经按照指示将它添加到我的仪表板,尽管我被告知:

"如果使用。htaccess,记得在你设置的url后面加上/">

我不完全理解(我使用。htaccess)。例如,如果我有一个webhook www.example.com/webhook,我应该在末尾添加另一个/还是什么?

继续……我需要传递元数据,它看起来像这样在最后…

{
"event": "subscription.create",
"data": {
"domain": "test",
"status": "active",
"subscription_code": "SUB_vsyqdmlzble3uii",
"amount": 50000,
"cron_expression": "0 0 28 * *",
"next_payment_date": "2016-05-19T07:00:00.000Z",
"open_invoice": null,
"createdAt": "2016-03-20T00:23:24.000Z",
"plan": {
"name": "Monthly retainer",
"plan_code": "PLN_gx2wn530m0i3w3m",
"description": null,
"amount": 50000,
"interval": "monthly",
"send_invoices": true,
"send_sms": true,
},
"authorization": {
"authorization_code": "AUTH_96xphygz",
"bin": "539983",
"last4": "7357",
"exp_month": "10",
"exp_year": "2017",
"card_type": "MASTERCARD DEBIT",
"bank": "GTBANK",
"country_code": "NG",
"brand": "MASTERCARD",
"account_name": "BoJack Horseman"
},
"customer": {
"first_name": "BoJack",
"last_name": "Horseman",
"email": "bojack@horsinaround.com",
"customer_code": "CUS_xnxdt6s1zg1f4nx",
"phone": "",
"metadata": {},
"risk_action": "default"
},
"created_at": "2016-10-01T10:59:59.000Z"
}
}
在我的php脚本中,我添加了这样的元数据字段:
<?php
$url = "https://api.paystack.co/transaction/initialize";
$access = $_POST['access'];
$support = $_POST['support'];
$fields = [
'email' => "customer@email.com",
'amount' => "20000",
'metadata' => [
'access' => $access,
'support' => $support,
]
];
$fields_string = http_build_query($fields);
//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
"Authorization: Bearer SECRET_KEY",
"Cache-Control: no-cache",
));

//So that curl_exec returns the contents of the cURL; rather than echoing it
curl_setopt($ch,CURLOPT_RETURNTRANSFER, true); 

//execute post
if ($result) {
$authorization_data = json_decode($result);
$authorization_url = $authorization_data->data->authorization_url;
header("Location: ".$authorization_url);
exit();
}
?>

在我的webhook url..我收到响应,然后解码它($event = json_decode(inputrecieved))。我尝试接收它为:

$support = $event->customer->metadata->support;

但不确定我发送的方式是否可以这样访问

如果您的接收端期望json作为主体,则需要将其作为json发送—通过构建有效负载并仅json_encode它。

准备数据:

//break it out to make it easier on yourself
$customerMetaData = [ ..., 'support' => 'abc' ];
$customer = [ 'email' => ..., 'metadata' => $customerMetaData  ];
$payload = ['customer' => $customer];
$jsonString = json_encode($payload);
...
//add Content-Type: application/json;charset=utf-8 to HTTPHEADER array
curl_setopt($ch,CURLOPT_POSTFIELDS, $jsonString);

接收端:

$eventJson = file_get_contents('php://input');
$event = json_decode($eventJson);
$support = $event->customer->metadata->support;

最新更新