首页 > 解决方案 > 我如何在 PHP 中使用 razorpay 支付网关

问题描述

我正在尝试集成“Razorpay”,我的源代码 zip 文件,现在没有得到任何响应,这是我的代码,我哪里错了?

<?php
require_once ('Razorpay.php');
use Razorpay\Api\Api;

$api = new Api($api_key, $api_secret);
echo "<pre>";print_R($api);
?>

标签: phprazorpay

解决方案


您可以按照以下步骤在 PHP 中集成 Razorpay

步骤 1.在 Razorpay 中创建一个帐户以获取 API。

步骤 2.从顶部导航栏测试或实时模式中选择一种模式,然后转到设置 -> API 密钥。

步骤 3.在您的项目中添加 Razorpay PHP 客户端。

composer require razorpay/razorpay:2.*

步骤 4.创建新的 index.php 文件并在该文件中添加以下代码

<?php
    require_once('razorpay-php/Razorpay.php');
    use Razorpay\Api\Api;
    use Razorpay\Api\Errors\SignatureVerificationError;
    $keyId = 'rzp_test_ZN5pRVLs4tU7YF';
    $keySecret = 'YOUR API Keys';
    $displayCurrency = 'INR';
    $api = new Api($keyId, $keySecret);
    //
    // We create an razorpay order using orders api
    // Docs: https://docs.razorpay.com/docs/orders
    //
    $orderData = [
        'receipt'         => 3456,
        'amount'          => 2000 * 100, // 2000 rupees in paise
        'currency'        => 'INR',
        'payment_capture' => 1 // auto capture
    ];

    $razorpayOrder = $api->order->create($orderData);
    $razorpayOrderId = $razorpayOrder['id'];
    $_SESSION['razorpay_order_id'] = $razorpayOrderId;
    $displayAmount = $amount = $orderData['amount'];

    if ($displayCurrency !== 'INR'){
        $url = "https://api.fixer.io/latest?symbols=$displayCurrency&base=INR";
        $exchange = json_decode(file_get_contents($url), true);
        $displayAmount = $exchange['rates'][$displayCurrency] * $amount / 100;
    }

    $checkout = 'automatic';

    if (isset($_GET['checkout']) and in_array($_GET['checkout'], ['automatic', 'manual'], true))
    {
        $checkout = $_GET['checkout'];
    }
    $data = [
        "key"               => $keyId,
        "amount"            => $amount,
        "name"              => "Aneh Thakur",
        "description"       => "Happy to help :)",
        "image"             => "https://s29.postimg.org/r6dj1g85z/daft_punk.jpg",
        "prefill"           => [
            "name"              => "Aneh Thakur",
            "email"             => "customer email",
            "contact"           => "customer mobile",
        ],
        "notes"             => [
            "address"           => "Customer Address",
            "merchant_order_id" => "12312321",
        ],
        "theme"             => [
            "color"             => "#F37254"
        ],
        "order_id"          => $razorpayOrderId,
    ];

    if ($displayCurrency !== 'INR')
    {
        $data['display_currency']  = $displayCurrency;
        $data['display_amount']    = $displayAmount;
    }
    $json = json_encode($data);
?>
<script src="https://checkout.razorpay.com/v1/checkout.js"></script>
<form name='razorpayform' action="verify.php" method="POST">
    <input type="hidden" name="razorpay_payment_id" id="razorpay_payment_id">
    <input type="hidden" name="razorpay_signature"  id="razorpay_signature" >
</form>
<script>
    // Checkout details as a json
    var options = <?=$json?>;
    /**
    * The entire list of Checkout fields is available at
    * https://docs.razorpay.com/docs/checkout-form#checkout-fields
    */
    options.handler = function (response){
        document.getElementById('razorpay_payment_id').value = response.razorpay_payment_id;
        document.getElementById('razorpay_signature').value = response.razorpay_signature;
        document.razorpayform.submit();
    };
    // Boolean whether to show image inside a white frame. (default: true)
    options.theme.image_padding = false;
    options.modal = {
        ondismiss: function() {
        console.log("This code runs when the popup is closed");
        window.location = 'cancel.php';
        },
        // Boolean indicating whether pressing escape key
        // should close the checkout form. (default: true)
        escape: true,
        // Boolean indicating whether clicking translucent blank
        // space outside checkout form should close the form. (default: false)
        backdropclose: false
    };
    var rzp = new Razorpay(options);
    rzp.open();
</script>

并且还创建另外 2 个文件,一个 verify.php 和 cancel.php。您可以在此处阅读完整的集成教程:- Razorpay PHP 集成


推荐阅读