【问题标题】:How can i use razorpay payment gateway in Php我如何在 PHP 中使用 razorpay 支付网关
【发布时间】:2020-02-01 11:03:12
【问题描述】:

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

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

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

【问题讨论】:

  • 文档上写着Call $api-&gt;class-&gt;function() to access the API。您创建了一个对象,但没有调用任何 api。更多信息请参考文档:github.com/razorpay/razorpay-php
  • @catcon:我的代码是对还是错?请告诉我
  • @catcon: 刚刚尝试(调用 api)但 api 不工作,显示空白页

标签: php razorpay


【解决方案1】:

您可以按照这些步骤在 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>

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

【讨论】:

    猜你喜欢
    • 2017-02-28
    • 2020-09-26
    • 2020-05-15
    • 2019-11-09
    • 2020-10-23
    • 2022-07-28
    • 2020-07-28
    • 2020-08-16
    • 2021-05-04
    相关资源
    最近更新 更多