Wordpress自定义页面模板Stripe集成显示-“ PHP致命错误:未被捕获的错误:找不到类'Stripe \ Stripe'”“错误”

时间:2019-10-19 20:21:05

标签: php wordpress stripe-payments

我在Payment.php页面模板中使用了以下代码...

    <?php
/*
Template Name: Page: Donate Success
*/  
// Check whether stripe token is not empty

    if(!empty($_POST['stripeToken'])){ 

// Retrieve stripe token, card and user info from the submitted form data 
$token  = $_POST['stripeToken']; 
$name = $_POST['forename']." ". $_POST['surname']; 
$email = $_POST['email']; 
$card_number = $_POST['card_number']; 
$card_exp_month = $_POST['card_exp_month']; 
$card_exp_year = $_POST['card_exp_year']; 
$card_cvc = $_POST['card_cvc']; 

// Include Stripe PHP library 
require_once(site_url().'/stripe-php/init.php'); 

\Stripe\Stripe::setApiKey("sk_test_my_secret_key");    

// Add customer to stripe 
$customer = \Stripe\Customer::create(array( 
    'email' => $email, 
    'source'  => $token 
)); 

// Unique order ID 
$orderID = strtoupper(str_replace('.','',uniqid('', true))); 

// Convert price to cents 
$itemPrice = ($itemPrice*100); 

// Charge a credit or a debit card 
$charge = \Stripe\Charge::create(array( 
    'customer' => $customer->id, 
    'amount'   => $itemPrice, 
    'currency' => $currency, 
    'description' => $itemName, 
    'metadata' => array( 
        'order_id' => $orderID 
    ) 
)); 

// Retrieve charge details 
$chargeJson = $charge->jsonSerialize(); 

// Check whether the charge is successful 
if($chargeJson['amount_refunded'] == 0 && empty($chargeJson['failure_code']) && $chargeJson['paid'] == 1 && $chargeJson['captured'] == 1){ 
    // Order details  
    $transactionID = $chargeJson['balance_transaction']; 
    $paidAmount = $chargeJson['amount']; 
    $paidCurrency = $chargeJson['currency']; 
    $payment_status = $chargeJson['status'];  


    // If the order is successful 
    if($payment_status == 'succeeded'){ 
        $ordStatus = 'success'; 
        $statusMsg = 'Your Payment has been Successful!'; 
    }else{ 
        $statusMsg = "Your Payment has Failed!"; 
    } 
}else{ 

    $statusMsg = "Transaction has been failed!"; 
} 
}else{ 
$statusMsg = "Error on form submission."; 
} 
?>

<div class="container">
<div class="status">
        <h1 class="<?php echo $ordStatus; ?>"><?php echo $statusMsg; ?></h1>

        <h4>Payment Information</h4>
        <p><b>Reference Number:</b> <?php echo $payment_id; ?></p>
        <p><b>Transaction ID:</b> <?php echo $transactionID; ?></p>
        <p><b>Paid Amount:</b> <?php echo $paidAmount.' '.$paidCurrency; ?></p>
        <p><b>Payment Status:</b> <?php echo $payment_status; ?></p>

        <h4>Product Information</h4>
        <p><b>Name:</b> <?php echo $itemName; ?></p>
        <p><b>Price:</b> <?php echo $itemPrice.' '.$currency; ?></p>
    </div>
    <a href="index.php" class="btn-link">Back to Payment Page</a>
</div>

我将条纹sdk文件包括在Wordpress的根文件夹中...

令牌是从付款表单生成的,可以在上面的模板中找到,但是我在下面的代码\ Stripe \ Stripe :: setApiKey(“ sk_test_my_secret_key”)中发现了错误。名为“ PHP致命错误:未捕获错误:未找到类'Stripe \ Stripe'的错误” ...

谢谢..

1 个答案:

答案 0 :(得分:0)

以下两行代码应排在第一位:

// Include Stripe PHP library 
require_once(site_url().'/stripe-php/init.php'); 

\Stripe\Stripe::setApiKey("sk_test_my_secret_key");    

示例:

<?php
// Include Stripe PHP library 
require_once(site_url().'/stripe-php/init.php'); 
\Stripe\Stripe::setApiKey("sk_test_my_secret_key");    


/*
Template Name: Page: Donate Success
*/  
// Check whether stripe token is not empty

    if(!empty($_POST['stripeToken'])){ 

// Retrieve stripe token, card and user info from the submitted form data 
$token  = $_POST['stripeToken']; 
$name = $_POST['forename']." ". $_POST['surname']; 
$email = $_POST['email']; 
$card_number = $_POST['card_number']; 
$card_exp_month = $_POST['card_exp_month']; 
$card_exp_year = $_POST['card_exp_year']; 
$card_cvc = $_POST['card_cvc']; 


// Add customer to stripe 
$customer = \Stripe\Customer::create(array( 

.... 

看看这是否能解决您的问题。