我是第一次使用 Webhooks 进行测试。我想设置一个 Webhook(使用 GoCardless.com)。
我在我的沙盒帐户中创建了访问令牌和 Webhook 端点。URL - http://test.com/remote_gocardless
我的路线是
Route::post('/remote_gocardless','remotegocardlessController@remote')->name('remote');
控制器是,
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Input;
class remotegocardlessController extends Controller
{
public function remote()
{
$token ="tunnelnew";
$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) {
switch ($event["resource_type"]) {
case "mandates":
process_mandate_events($event);
break;
case "payments":
process_payment_events($event);
break;
case "subscriptions":
process_subscription_events($event);
break;
}
}
header("HTTP/1.1 200 OK");
} else {
header("HTTP/1.1 498 Invalid Token");
}
}
}
沙盒帐户中的响应类似于 500 Internal Server Error。
为了在 laravel 中实现 webhook,我还需要添加其他代码吗?我们可以将 url 作为 http 而不是 https 吗?