问题描述
我有一个拥有3个页面的会员资格的网站,一个用于展示会员资格(带有我的PayPal按钮),一个带有注册表格(用于将会员添加到我的数据库中),最后一个在完成所有操作后说(例如“您现在可以登录”)。
我刚刚在YouTube上观看了一个视频,添加了“贝宝”按钮,供我的会员在开始填写注册表之前先付款。
我正在使用PayPal SDK。这是我的代码:
第一页(account-step1.PHP)
<!DOCTYPE html>
<html lang="en">
<?PHP
// Include HEADER
include ("includes/head.PHP");
?>
<!-- SPECIFIC CSS -->
<link href="css/booking-sign_up.css" rel="stylesheet">
<style type="text/css">
.circle {
width: 70px;
height: 70px;
line-height: 70px;
border-radius: 50%;
font-size: 24px;
color: red;
text-align: center;
background: gold;
font-weight: bold;
}
/* Media query for mobile viewport */
@media screen and (max-width: 400px) {
#paypal-button-container {
width: 100%;
}
}
/* Media query for desktop viewport */
@media screen and (min-width: 400px) {
#paypal-button-container {
width: 250px;
}
}
</style>
<script src="https://www.paypal.com/sdk/js?client-id=sb¤cy=USD"></script>
</head>
<body>
<header class="header_in clearfix">
<div class="container">
<div id="logo">
<a href="index.PHP">
<img src="img/logo_sticky.svg" width="250" height="40" alt="" class="logo_sticky">
</a>
</div>
<?PHP include ("includes/topmenu.PHP"); ?>
<a href="#0" class="open_close">
<i class="icon_menu"></i><span>Menu</span>
</a>
<?PHP include ("includes/menu.PHP"); ?>
</div>
</header>
<!-- /header -->
<main class="bg_gray pattern">
<div class="container margin_60_40">
<div class="main_title center">
<span><em></em></span>
<h2><?PHP echo _e("Our Subscription Plan"); ?></h2>
<p>VIP 2021 - 2022</p>
</div>
<div class="row justify-content-center">
<div class="col-lg-12">
<div class="sign_up">
<div class="head">
<div class="title">
<h3>1 YEAR SUBSCRIPTION</h3>
</div>
</div>
<!-- /head -->
<div class="main text-center" style="font-size:16px;">
<p><i class="icon_star"></i> <?PHP echo _e("VIP Membership Card"); ?> <i class="icon_star"></i></p>
<p><i class="icon_star"></i> <?PHP echo _e("Take advantage of original discounts at all our partners everywhere near you"); ?> <i class="icon_star"></i></p>
<p><i class="icon_star"></i> <?PHP echo _e("Your subscription pays for itself in just 1 or 2 uses of discounts"); ?> <i class="icon_star"></i></p>
<p><i class="icon_star"></i> <?PHP echo _e("Each year your easy-to-use privileges are recharged in the application to make you enjoy again"); ?> <i class="icon_star"></i></p>
<p><i class="icon_star"></i> <?PHP echo _e("Contribute to better nutritional health for our children with $ 1 donated to the Club des petits déjeuners by subscription"); ?> <i class="icon_star"></i></p>
<p class="text-center">
<center>
<div class="circle">20$</div>
</center>
</p>
<hr>
<center><div id="paypal-button-container"></div></center>
</div>
</div>
<!-- /Box_booking -->
</div>
<!-- /col -->
</div>
<!-- /row -->
</div>
<!-- /container -->
</main>
<!-- /main -->
<?PHP include ("includes/footer.PHP"); ?>
<div id="toTop"></div><!-- Back to top button -->
<div class="layer"></div><!-- Opacity Mask Menu Mobile -->
<?PHP include("includes/signin-modal.PHP"); ?>
<!-- COMMON SCRIPTS -->
<script src="js/common_scripts.min.js"></script>
<script src="js/common_func.js"></script>
<script src="assets/validate.js"></script>
<!-- Include the PayPal JavaScript SDK -->
<script src="https://www.paypal.com/sdk/js?client-id=AeQdfTQ2soaOdV7vgLYyTsnsSC3ptpSJJWFsDGwkTXmQocaCBfYPYWV7mHO-iPGX3_ZiLdCqxmqY9h6H¤cy=USD"></script>
<script>
paypal.Buttons({
style: {
color: 'gold',shape: 'pill',label: 'pay'
},createOrder: function(data,actions) {
// This function sets up the details of the transaction,including the amount and line item details.
return actions.order.create({
purchase_units: [{
amount: {
value: '20.00'
}
}]
});
},onApprove: function(data,actions) {
// This function captures the funds from the transaction.
return actions.order.capture().then(function() {
window.location = "transaction-completed.PHP?&orderID="+data.orderID;
});
}
}).render('#paypal-button-container');
//This function displays Smart Payment Buttons on your web page.
</script>
</body>
</html>
transaction-completed.PHP
<?PHP
namespace Sample;
require __DIR__ . '/vendor/autoload.PHP';
//1. Import the PayPal SDK client that was created in `Set up Server-Side SDK`.
use Sample\PayPalClient;
use PayPalCheckoutSdk\Orders\OrdersGetRequest;
require 'paypal-client.PHP';
$orderID = $_GET['orderID'];
class Getorder
{
// 2. Set up your server to receive a call from the client
/**
*You can use this function to retrieve an order by passing order ID as an argument.
*/
public static function getorder($orderId)
{
// 3. Call PayPal to get the transaction details
$client = PayPalClient::client();
$response = $client->execute(new OrdersGetRequest($orderId));
// Transaction Details
$orderID = $response->result->id;
$email = $response->result->payer->email_address;
$pre_name = $response->result->payer->given_name;
$last_name = $response->result->payer->surname;
$full_name = $pre_name.$last_name;
// INSERT DATA INTO DATABASE
include('db_paypal.PHP'); //CONNECT DATABASE
// PREPARE AND BIND
$stmt = $con->prepare("INSERT INTO payments (fullname,orderID) VALUES (?,?) ");
$stmt->bind_param("ss",$name,$orderID);
$stmt->execute();
//TRY TO EXECUTE QUERY
if (!$stmt) {
echo 'Error sql' .MysqLi_error($con);
}
else{
session_start(); //START SESSION
$_SESSION['orderID'] = $orderID; //STORE orderID INTO SESSION VAR
header("Location:account-step3.PHP"); //REDIRECT TO NEXT STEP
}
$stmt->close(); //CLOSE STATEMENT
$con->close(); //CLOSE CONNECTION sql
}
}
/**
*This driver function invokes the getorder function to retrieve
*sample order details.
*
*To get the correct order ID,this sample uses createOrder to create an order
*and then uses the newly-created order ID with Getorder.
*/
if (!count(debug_backtrace()))
{
Getorder::getorder($orderID,true);
}
?>
paypal-client.PHP
<?PHP
namespace Sample;
use PayPalCheckoutSdk\Core\PayPalHttpClient;
// GO LIVE \LiveEnvironment
use PayPalCheckoutSdk\Core\SandBoxEnvironment;
ini_set('error_reporting',E_ALL); // or error_reporting(E_ALL);
ini_set('display_errors','1');
ini_set('display_startup_errors','1');
class PayPalClient
{
/**
* Returns PayPal HTTP client instance with environment that has access
* credentials context. Use this instance to invoke PayPal APIs,provided the
* credentials have access.
*/
public static function client()
{
return new PayPalHttpClient(self::environment());
}
/**
* Set up and return PayPal PHP SDK environment with PayPal access credentials.
* This sample uses SandBoxEnvironment. In production,use \LiveEnvironment.
*/
public static function environment()
{
$clientId = getenv("CLIENT_ID") ?: "AeQdfTQ2soaOdV7vgLYyTsnsSC3ptpSJJWFsDGwkTXmQocaCBfYPYWV7mHO-iPGX3_ZiLdCqxmqY9h6H";
$clientSecret = getenv("CLIENT_SECRET") ?: "ECMtmwnFims09g_UFAGBAabjkOgykKORvIC9Y9xW2Mn2aZRx0bH7DpHyXDFg3klRkzV9nUI8T6HRI1e8";
return new SandBoxEnvironment($clientId,$clientSecret);
}
}
db_paypal.PHP
<!DOCTYPE html>
<html lang="en">
<?PHP
// Include HEADER
include ("includes/head.PHP");
?>
<?PHP if (empty($_SESSION['orderID'])) {
echo "<script type='text/javascript'>location.replace('../index.PHP');</script>";
}else{}
?>
<!-- SPECIFIC CSS -->
<link href="css/booking-sign_up.css" rel="stylesheet">
</head>
<body>
<header class="header_in clearfix">
<div class="container">
<div id="logo">
<a href="index.PHP">
<img src="img/logo_sticky.svg" width="250" height="40" alt="" class="logo_sticky">
</a>
</div>
<?PHP include ("includes/topmenu.PHP"); ?>
<a href="#0" class="open_close">
<i class="icon_menu"></i><span>Menu</span>
</a>
<?PHP include ("includes/menu.PHP"); ?>
</div>
</header>
<!-- /header -->
<main class="bg_gray pattern">
<div class="container margin_60_40">
<div class="row justify-content-center">
<div class="col-lg-4">
<div class="sign_up">
<div class="head">
<div class="title">
<h3><?PHP echo _e("Registration") ?></h3>
</div>
</div>
<!-- /head -->
<div class="main">
<form action="includes/register.PHP" method="post" id="payment-form">
<h6><?PHP echo _e("Personal details") ?></h6>
<div class="form-group">
<input class="form-control" placeholder="<?PHP echo _e("First and Last Name");?>" id="full_name" name="full_name" required>
<i class="icon_pencil"></i>
</div>
<div class="form-group">
<input class="form-control" placeholder="<?PHP echo _e("Apartment (Optional)");?>" id="apartment" name="apartment">
<i class="icon_house_alt"></i>
</div>
<div class="form-group">
<input class="form-control" placeholder="<?PHP echo _e("Adress");?>" id="adress" name="adress" required>
<i class="icon_house_alt"></i>
</div>
<div class="form-group">
<input class="form-control" placeholder="<?PHP echo _e("City");?>" id="city" name="city" required>
<i class="icon_house_alt"></i>
</div>
<div class="form-group">
<input class="form-control" placeholder="<?PHP echo _e("Postal Code");?>" id="postalcode" name="postalcode" maxlength="6" minlength="6" required>
<i class="icon_house_alt"></i>
</div>
<div class="form-group">
<input class="form-control" placeholder="<?PHP echo _e("Phone (888-888-8888)");?>" id="phone" name="phone" type="tel" pattern="[0-9]{3}[0-9]{3}[0-9]{4}" maxlength="10" minlength="10" required>
<i class="icon_phone"></i>
</div>
<div class="form-group">
<input class="form-control" placeholder="<?PHP echo _e("Email Address");?>" id="email" name="email" required>
<i class="icon_mail"></i>
</div>
<div class="form-group add_bottom_15">
<input class="form-control" placeholder="<?PHP echo _e("Password (8 character minimum)");?>" id="password" name="password" minlength="8" required>
<i class="icon_lock"></i>
</div>
<input type="hidden" class="form-control" name="refer" id="refer" value="<?PHP if(!empty($_SESSION['pid'])){echo $_SESSION['pid'];}else{}?>">
<input type="submit" class="btn_1 full-width mb_5" name="submit" value="<?PHP echo _e("Sign up Now");?>">
</form>
</div>
</div>
<!-- /Box_booking -->
</div>
<!-- /col -->
</div>
<!-- /row -->
</div>
<!-- /container -->
</main>
<!-- /main -->
<?PHP include ("includes/footer.PHP"); ?>
<div id="toTop"></div><!-- Back to top button -->
<div class="layer"></div><!-- Opacity Mask Menu Mobile -->
<?PHP include("includes/signin-modal.PHP"); ?>
<!-- COMMON SCRIPTS -->
<script src="js/common_scripts.min.js"></script>
<script src="js/common_func.js"></script>
<script src="assets/validate.js"></script>
</body>
</html>
当我尝试使用SandBox个人或企业帐户付款时,一切都工作正常,我得到了一个错误...
这是我得到的错误:
致命错误:未捕获的PayPalHttp \ HttpException: {“名称”:“ RESOURCE_NOT_FOUND”,“详细信息”:[{“位置”:“路径”,“问题”:“ INVALID_RESOURCE_ID”,“说明”:“已指定 资源ID不存在。请检查资源ID并尝试 再次。“}],”消息“:”指定的资源不 存在。“,” debug_id“:” 17a56f649394f“,”链接“:[{” href“:” https://developer.paypal.com/docs/apI/Orders/v2/#error-INVALID_RESOURCE_ID“,” rel“ :“ information_link”,“方法”:“ GET”}]} /home/superrab/public_html/beta/vendor/paypal/paypalhttp/lib/PayPalHttp/HttpClient.PHP:215 堆栈跟踪:#0 /home/superrab/public_html/beta/vendor/paypal/paypalhttp/lib/PayPalHttp/HttpClient.PHP(100): PayPalHttp \ HttpClient-> parseResponse(Object(PayPalHttp \ Curl))#1 /home/superrab/public_html/beta/transaction-completed.PHP(24): PayPalHttp \ HttpClient-> execute(对象(PayPalCheckoutSdk \ Orders \ OrdersGetRequest)) #2 /home/superrab/public_html/beta/transaction-completed.PHP(65):Sample \ Getorder :: getorder('7KM15574UC31371 ...',true)#3 {main} throw 在 /home/superrab/public_html/beta/vendor/paypal/paypalhttp/lib/PayPalHttp/HttpClient.PHP 在第215行
感谢您的帮助
解决方法
我看到这是用于支付创建和捕获的仅客户端集成,然后在该客户端捕获之后,您使用server = side AI调用来获取订单的详细信息。那是一个非常奇怪的混合体,有人想知道为什么不集成“设置事务”和“捕获事务on the server side(并与approval JS for a server side integration配对)以获得该健壮性的所有好处,然后您不需要额外的API调用即可获取订单的详细信息,因为在服务器端捕获后就已经拥有了该订单,并能够自动进行存储。基本上,似乎您真的应该切换到该位置,而不要尝试这种不必要的混合动力。
但是,如果您坚持要做没人建议的奇怪而不可取的事情,那么服务器端代码不会创建订单ID(例如7KM15574UC313712P),因此无法通过这种方式进行访问。您可以尝试的是从purchase_units[0].captures.id
中查找JS中成功的Capture ID(而不是Order ID),并将该实际的PayPal交易ID传递给服务器代码。然后使用它获取v2 / payment捕获对象,该对象将使用不同的API / SDK调用-在您的情况下,此对象为:https://github.com/paypal/Checkout-PHP-SDK/blob/develop/lib/PayPalCheckoutSdk/Payments/CapturesGetRequest.php
(但同样,理想的是整个段落都没有意义,您应该更加注意第1段)