您好我正在为网站创建付款方式,但我遇到了一个绊脚石。
我有一个HTML表单,如下所示:
<form method="post" action="member/payment.php">
<div class="field">
<label class="label">Voor- en achternaam</label>
<div class="control">
<input class="input" name="nameField" id="nameField" type="text" placeholder="Vul hier uw naam in">
</div>
</div>
<div class="field">
<label class="label">Email</label>
<div class="control has-icons-left has-icons-right">
<input class="input" name="mailField" id="mailField" type="email" placeholder="voorbeeld@voorbeeld.nl">
<span class="icon is-small is-left">
<i class="fas fa-envelope"></i>
</span>
<span class="icon is-small is-right">
<i class="fas fa-exclamation-triangle"></i>
</span>
</div>
<!-- <p class="help is-danger">This email is invalid</p> -->
</div>
<div class="field">
<label class="label">Gebruikersnaam</label>
<div class="control">
<input class="input" type="text" name="usernameField" id="usernameField" placeholder="Vul hier uw gebruikersnaam in">
</div>
</div>
<div class="field">
<label class="label">Wachtwoord</label>
<div class="control">
<input class="input" type="password" name="passwordField" id="passwordField" placeholder="Vul hier uw wachtwoord in">
</div>
</div>
<div class="field">
<label class="label">Wachtwoord</label>
<div class="control">
<input class="input" type="password" name="passwordField2" id="passwordField2" placeholder="Vul hier uw wachtwoord nogmaals in">
</div>
</div>
<center><a class="button is-red is-large" type="submit" href="member/payment.php">Betalen</a></center>
</form>
现在,一旦我点击了我想要的submit
按钮,就会转到此文件:
<?php
use PayPal\Api\Payer;
use PayPal\Api\Details;
use PayPal\Api\Amount;
use PayPal\Api\Transaction;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Exception\PPConnectionExeption;
require '../src/start.php';
$payer = new Payer();
$details = new Details();
$amount = new Amount();
$transaction = new Transaction();
$payment = new Payment();
$redirectUrls = new RedirectUrls();
//Payer
$payer->setPaymentMethod("paypal");
// Details
$details->setShipping('0.00')
->setTax('2.60')
->setSubtotal('12.40');
$amount->setCurrency('EUR')
->setTotal('15.00')
->setDetails($details);
//Transaction
$transaction->setAmount($amount)
->setDescription('Een licensie voor kilometro');
//Payment
$payment->setIntent('sale')
->setPayer($payer)
->setTransactions([$transaction]);
//RedirectUrls
$redirectUrls->setReturnUrl('http://localhost/payments/paypal/pay.php?approved=true')
->setCancelUrl('http://localhost/payments/paypal/pay.php?approved=false');
$payment->setRedirectUrls($redirectUrls);
try{
$payment->create($api);
//Generate/store hash
$hash = md5($payment->getId());
$_SESSION['paypal_hash'] = $hash;
//Prepare and execute transaction storage
$store = $db->prepare("
INSERT INTO transactions (user_id, payment_id, hash, complete)
VALUES (:user_id, :payment_id, :hash, 0)
");
$store->execute([
'user_id'=> $_SESSION['user_id'],
'payment_id' => $payment->getId(),
'hash' => $hash
]);
} catch(PPConnectionExeption $e){
//Perhaps log an error
header('Location: ../paypal/error.php');
}
foreach ($payment->getLinks() as $link) {
# code...
if($link->getRel() == 'approval_url'){
$redirectUrls = $link->getHref();
}
}
header('Location: ' . $redirectUrls);
?>
(payment.php
)。
此文件需要此文件:
<?php
use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
session_start();
$_SESSION['user_id'];
$_SESSION['name'] = $_POST['nameField'];
$_SESSION['username'] = $_POST['usernameField'];
$_SESSION['email'] = $_POST['mailField'];
$_SESSION['password'] = $_POST['passwordField'];
$_SESSION['password2'] = $_POST['passwordField2'];
require __DIR__ . '/../vendor/autoload.php';
$db = new PDO('mysql:host=localhost;dbname=kilometers', 'root', '');
$addMember = $db->prepare("
INSERT INTO transactions (username, password, naam, email, license)
VALUES(:username, :password, :name, :email, 0)
");
$addMember->execute([
'username' => $_SESSION['username'],
'password' => $_SESSION['password'],
'name' => $_SESSION['name'],
'email' => $_SESSION['email']
]);
//API
$api = new ApiContext(
new OAuthTokenCredential(
'-',
'-'
)
);
$api->setConfig([
'mode' => 'sandbox',
'http.ConnectionTimeOut' => 30,
'log.LogEnabled' => false,
'log.FileName' => '',
'log.LogLevel' => 'FINE',
'validation.level' => 'log'
]);
$getUser = $db->prepare("
SELECT * FROM customers
WHERE name = :name
AND username = :username
AND password = :password
AND email = :email
");
$getUser->execute([
'username' => $_SESSION['username'],
'password' => $_SESSION['password'],
'name' => $_SESSION['name'],
'email' => $_SESSION['email']
]);
$user = $getUser->fetchObject();
$_SESSION['user_id']=$user->id;
//}
?>
(start.php
)
现在,如果我提交表单,它可以正常运行,但它不会在数据库中添加成员。正如它应该在line 20-30
的{{1}}那样做。可能是什么问题?