我对编程很陌生,我在学校,我们有一个项目,我们需要为一家假公司创建一个网站。我们已经获得了一个可以使用的数据库,并且必须集成一个支付系统。我们决定使用mollie。我已经习惯了标准的示例付款创建,它工作正常,但现在我想将订单中的数据获取到我的数据库中。我已经使用 html 表单来获取所需的信息,现在只需要将其输入数据库,这就是我尝试过的方式,但还没有成功。
$mail_err=$firstname_err=$lastname_err=$city_err=$phone_err=$postal_err=$adres_err="";
$mail_err=$firstname=$lastname=$city=$phone=$postal=$adres="";
if(isset($_POST['Betalen'])) {
if(empty(trim($_POST['firstname']))){
$firstname_err = "Voer een voornaam in";
} else{
if(!ctype_alpha(str_replace(array(' ', "'", '-'),'',$_POST['firstname']))){
$firstname_err="De voornaam mag alleen letters bevatten m.u.v. ' en -";
} else{
$firstname=$_POST['firstname'];
}
}
if(empty(trim($_POST['lastname']))){
$lastname_err = "Voer een achternaam in";
} else{
if(!ctype_alpha(str_replace(array(' ', "'", '-'),'',$_POST['lastname']))){
$lastname_err="De achternaam mag alleen letters bevatten m.u.v. ' en -";
} else{
$lastname=$_POST['lastname'];
}
}
if(empty(trim($_POST['mail']))){
$mail_err = "Voer een emailadres in";
} else{
$email=$_POST['email'];
}
if(empty(trim($_POST['city']))){
$city_err = "Voer een stad in";
} else{
$city=$_POST['city'];
}
if(empty(trim($_POST['adres']))){
$adres_err = "Voer een adres in";
} else{
$adres=$_POST['adres'];
}
if(empty(trim($_POST['postcode']))){
$postal_err = "Voer een postcode in";
} else{
if(PostcodeCheck($_POST['postcode']) == false){
$postal_err="Ongeldige postcode";
} else{
$postal=$_POST['postcode'];
}
}
if(trim(!ctype_digit($_POST['phone']))){
$phone_err="Voer alleen cijfers in bijvoorbeeld 0612345678";
} else{
$phone=$_POST['phone'];
}
if(empty($mail_err) && empty($firstname_err) && empty($lastname_err) && empty($city_err) && empty($phone_err) && empty($postal_err) && empty($adres_err)) {
try {
/*
* Initialize the Mollie API library with your API key.
*
* See: https://www.mollie.com/dashboard/developers/api-keys
*/
require "initialize.php";
/*
* Generate a unique order id for this example. It is important to include this unique attribute
* in the redirectUrl (below) so a proper return page can be shown to the customer.
*/
$orderId = time();
$total = $_POST['total'];
/*
* Determine the url parts to these example files.
*/
$protocol = isset($_SERVER['HTTPS']) && strcasecmp('off', $_SERVER['HTTPS']) !== 0 ? "https" : "http";
$hostname = $_SERVER['HTTP_HOST'];
$path = dirname(isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $_SERVER['PHP_SELF']);
/*
* Payment parameters:
* amount Amount in EUROs. This example creates a € 10,- payment.
* description Description of the payment.
* redirectUrl Redirect location. The customer will be redirected there after the payment.
* webhookUrl Webhook location, used to report when the payment changes state.
* metadata Custom metadata that is stored with the payment.
*/
$payment = $mollie->payments->create([
"amount" => [
"currency" => "EUR",
"value" => "$total" // You must send the correct number of decimals, thus we enforce the use of strings
],
"description" => "Order #{$orderId}",
"redirectUrl" => "{$protocol}://{$hostname}{$path}/return.php?order_id={$orderId}",
"webhookUrl" => "{$protocol}://{$hostname}{$path}/payments/webhook.php",
"metadata" => [
"order_id" => $orderId,
],
]);
/*
* In this example we store the order with its payment status in a database.
*/
mysqli_query($conn, "INSERT INTO ordersprivate (OrderID, orderstatus, price, email, first_name, last_name, adres, postal, city, phone) VALUES ($orderId,$status,$total,$email,$firstname,$lastname,$adres,$postal,$city,$phone)");
//($conn, $orderId, $payment->status, $total, $email, $firstname, $lastname, $adres, $postal, $city, $phone);
/*
$sql1 = "INSERT INTO ordersprivate (OrderID, orderstatus, price, email, first_name, last_name, adres, postal, city, phone) VALUES (?,?,?,?,?,?,?,?,?,?)";
if($stmt1=mysqli_prepare($conn,$sql1)) {
mysqli_stmt_bind_param($stmt1, "isssssssss", $param_OrderID, $param_status, $param_price, $param_email, $param_firstname, $param_lastname, $param_adres, $param_postal, $param_city, $param_phone);
$param_email=$email;
$param_OrderID=$orderId;
$param_status=$status;
$param_price=$total;
$param_firstname=$firstname;
$param_lastname=$lastname;
$param_adres=$adres;
$param_postal=$postal;
$param_city=$city;
$param_phone=$phone;
mysqli_stmt_execute($stmt1);
} */
/*
* Send the customer off to complete the payment.
* This request should always be a GET, thus we enforce 303 http response code
*/
header("Location: " . $payment->getCheckoutUrl(), true, 303);
} catch (\Mollie\Api\Exceptions\ApiException $e) {
echo "API call failed: " . htmlspecialchars($e->getMessage());
}
}
您可以看到我尝试了几种不同的解决方案,但数据从未存储在数据库中。我可能很愚蠢,忽略了一些东西。我已经确保与数据库的连接正常工作,并且它存储在带有变量 $conn 的不同文件中,并且它在其他任何地方都可以工作。
在此先感谢 Niels van Dijk