目前,我在线上有一个在线现场应用程序,因此这对于处理付款至关重要。
许多试图完成付款的人都与我联系,有的人说他们的URL不完全合格,有的人说也没有进入PayPal页面并且无法正确加载。
我认为已经通过添加if不存在子句(以前在SDK GitHub上找到)来解决了合格的URL问题。
由于我是使用PayPal Rest API的新手,因此我不确定对其中可能存在的问题有任何其他结论
我从start.php
开始,这很好并且保留了我的PayPal凭据。没问题。
此过程中的下一个是checkout.php
use PayPal\Api\Payer;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Details;
use PayPal\Api\Amount;
use PayPal\Api\Transaction;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Payment;
include '../src/func/manage.php';
include '../src/func/pay.php';
include '../src/func/getQnA.php';
require 'start.php';
// Getting information for user
$draw = filter_input(INPUT_POST, "drawID", FILTER_DEFAULT);
$quantity = filter_input(INPUT_POST, "quantity", FILTER_DEFAULT);
$phone = filter_input(INPUT_POST, "phone", FILTER_DEFAULT);
$fname = filter_input(INPUT_POST, "fname", FILTER_DEFAULT);
$lname = filter_input(INPUT_POST, "lname", FILTER_DEFAULT);
$email = filter_input(INPUT_POST, "email", FILTER_DEFAULT);
$selectedAns = filter_input(INPUT_POST, "ansID", FILTER_DEFAULT);
$questionID = filter_input(INPUT_POST, "questionID", FILTER_DEFAULT);
$product = getTitle($draw);
$price = getPrice($draw);
$total = $quantity*$price;
$ticketsLeft = ticketsLeft($draw);
$corrAns = checkQuestion($questionID, $selectedAns);
if($corrAns == "false"){
header("Location: /payment/pay.php?success=false&wrong=true");
die();
}
if($ticketsLeft <= 0){
header("Location: /payment/pay.php?success=false&sold=true");
die();
}
$payer = new Payer();
$payer->setPaymentMethod('paypal');
$item = new Item();
$item->setName($product)
->setCurrency('GBP')
->setQuantity($quantity)
->setPrice($price);
$itemList = new ItemList();
$itemList->setItems([$item]);
$details = new Details();
$details->setShipping(0.00)
->setSubtotal($price*$quantity);
$amount = new Amount();
$amount->setCurrency('GBP')
->setTotal($total)
->setDetails($details);
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription(SITE_NAME)
->setInvoiceNumber(uniqid());
$successUrl = SITE_URL . "payment/pay?draw=$draw&quantity=$quantity&fname=$fname&lname=$lname&phone=$phone&email=$email";
$cancelUrl = SITE_URL . "payment/pay?success=false";
$redirectUrls = new redirectUrls();
$redirectUrls->setReturnUrl($successUrl)
->setCancelUrl($cancelUrl);
$payment = new Payment();
$payment->setIntent('sale')
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions([$transaction]);
try {
$payment->create($apiContext);
} catch (Exception $e) {
$e->getCode();
$e->getData();
}
$approvalUrl = $payment->getApprovalLink();
header("Location: {$approvalUrl}");
处理完checkout.php
后,我希望它会转到我的pay.php
页,如下所示,然后在经过完整确认后从该页面退回。
use PayPal\Api\Payment;
use PayPal\Api\PaymentExecution;
include '../src/func/pay.php';
include '../src/func/manage.php';
require 'start.php';
require_once '../vendor/autoload.php';
$paymentId = filter_input(INPUT_GET, "paymentId", FILTER_DEFAULT);
//$success = filter_input(INPUT_GET, "success", FILTER_DEFAULT);
$success = TRUE;
$drawID = filter_input(INPUT_GET, "draw", FILTER_DEFAULT);
$wrong = filter_input(INPUT_GET, "wrong", FILTER_DEFAULT);
$sold = filter_input(INPUT_GET, "sold", FILTER_DEFAULT);
$quantity = filter_input(INPUT_GET, "quantity", FILTER_DEFAULT);
$token = filter_input(INPUT_GET, "token", FILTER_DEFAULT);
$payerID = filter_input(INPUT_GET, "PayerID", FILTER_DEFAULT);
$phone = filter_input(INPUT_GET, "phone", FILTER_DEFAULT);
$fname = filter_input(INPUT_GET, "fname", FILTER_DEFAULT);
$lname = filter_input(INPUT_GET, "lname", FILTER_DEFAULT);
$email = filter_input(INPUT_GET, "email", FILTER_DEFAULT);
$title = getTitle($drawID);
$ticketsLeft = ticketsLeft($drawID);
$paymentFeedback = "";
$tableNumbers = "";
if(!isset($success) || !isset($drawID)){
header("Location: ../index.php#draw");
}
if($success = true){
$paymentFeedback = "Thanks for purchasing your ticket(s).";
} else {
$paymentFeedback = "Sorry, an error occured. Please try again.";
}
if($wrong == true) {
$paymentFeedback = "You selected a wrong answer and cannot process further. Please try again later.";
header("Location:" . SITE_URL . "payment/pay-complete?text=" . $paymentFeedback . "#entry");
}
if($sold == true && $wrong = false || $ticketsLeft <= 0){
$paymentFeedback = "Sorry, all tickets have now sold out. Check out our other competitions";
header("Location:" . SITE_URL . "payment/pay-complete?text=" . $paymentFeedback . "#entry");
}
$payment = Payment::get($paymentId, $apiContext);
$execute = new PaymentExecution();
$execute->setPayerId($payerID);
try {
$result = $payment->execute($execute, $apiContext);
} catch(Exception $e) {
$data = json_decode($e->getData());
echo $data->message;
}
一个不相关的问题是,人们实际上在加载网站时遇到了问题,但是每次在桌面和移动设备上完美加载时都会出现问题。即使在删除缓存之后。
先谢谢大家。非常感谢您的帮助。