我如何在PHP中使用razorpay付款网关

时间:2019-10-03 06:58:43

标签: php razorpay

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

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

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

1 个答案:

答案 0 :(得分:0)

您可以按照以下步骤进行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 integration