Пользовательский шаблон страницы Wordpress, показывающий интеграцию Stripe - «PHP Fatal error: Uncaught Error: класс« Stripe \ Stripe »не найден» ERROR » - PullRequest
0 голосов
/ 19 октября 2019

Я использовал следующий код в шаблоне страницы 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' не найден" ERROR "...

Спасибо ..

1 Ответ

0 голосов
/ 20 октября 2019

Эти две строки кода должны стоять первыми:

// 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( 

.... 

Посмотрите, решит ли это вашу проблему.

...