imlolman/ccavenue-php-sdk

非官方的 CCAvenue 支付网关实现

v1.0 2024-09-24 12:16 UTC

This package is auto-updated.

Last update: 2024-09-24 12:21:05 UTC


README

欢迎来到 CCAvenue 支付网关集成 指南!如果你在这里,你可能正在解决 CCAvenue 集成的迷宫。

阅读整个指南,以获得愉快和有趣的经验。如果喜欢,请给仓库加星。不用担心!我已经经历过这些困难,我现在在这里是为了让你的旅程更顺畅(也许甚至是有趣的)!

目录

挑战真的存在 😫

让我们说实话:集成 CCAvenue 可以是一场 噩梦。原因如下

  1. 糟糕的门户 UI:它笨拙、缓慢,感觉像是 2000 年初期的。

  2. 没有在线测试凭据:与现代网关不同,您不能直接生成测试凭据。您必须 通过电子邮件发送。是的,在 2023 年。

  3. 商户认证失败(错误 10002):这个错误会一直困扰你,直到你意识到你需要 将测试域名列入白名单,包括 localhost。再次,通过电子邮件。

  4. 聊天支持?嗯:它在那里,但别期望有奇迹。你通常需要拿起电话。

  5. 沟通是关键:您最好的选择是 直接打电话(+91 8801033323) 或通过电子邮件 service@ccavenue.com

  6. 测试和生产使用相同的凭据:请谨慎操作!您不希望混淆测试和实时交易。

入门 🚀

在深入之前,请确保您有

  • 一个 已验证的 CCAvenue 账户(耐心是一种美德)。
  • 测试凭据(记住,需要通过电子邮件获取这些凭据)。
  • 一个开发环境,带有 PHP(最新版本)Laravel

集成步骤 🛠️

1. 安装此库

首先,通过 Composer 安装 imlolman/ccavenue-php-sdk

composer require imlolman/ccavenue-php-sdk

2. 启动交易

这是如何通过 CCAvenue 启动交易的方法

<?php

require_once __DIR__ . '/vendor/autoload.php';
use Imlolman\CCAvenue\CCAvenue;

$merchantId = "your_merchant_id";
$accessCode = "your_access_code";
$workingKey = "your_working_key";
$mode = "DEV"; // Use "PROD" for production

$ccavenue = CCAvenue::init($merchantId, $accessCode, $workingKey, $mode);

$transaction = $ccavenue->getTransaction();

$compulsoryInfo = [
    'order_id' => 'ORDER12345',
    'amount' => '100.00',
    'currency' => 'INR',
    'redirect_url' => 'http://yourdomain.com/response.php',
    'cancel_url' => 'http://yourdomain.com/cancel.php',
    'language' => 'EN'
];

$billingInfo = [
    'billing_name' => 'Jane Doe',
    'billing_address' => '123 Main Street',
    'billing_city' => 'Mumbai',
    'billing_state' => 'Maharashtra',
    'billing_zip' => '400001',
    'billing_country' => 'India',
    'billing_tel' => '9876543210',
    'billing_email' => 'jane@example.com'
];

$shippingInfo = [
    'delivery_name' => 'Jane Doe',
    'delivery_address' => '456 Side Street',
    'delivery_city' => 'Delhi',
    'delivery_state' => 'Delhi',
    'delivery_zip' => '110001',
    'delivery_country' => 'India',
    'delivery_tel' => '9876543210'
];

$paymentUrl = $transaction->initiate($compulsoryInfo, $billingInfo, $shippingInfo);

// Redirect to the payment page
header('Location: ' . $paymentUrl);

// If you get 10002 error, you need to whitelist your domain bye contacting CCAvenue via call. Why don't you read entire guide? You will have fun, really. Also don't forget to star the repository.

3. 处理响应

交易后,CCAvenue 将将用户重定向到您的 response.php 页面

<?php

require_once __DIR__ . '/vendor/autoload.php';
use Imlolman\CCAvenue\CCAvenue;

$merchantId = "your_merchant_id";
$accessCode = "your_access_code";
$workingKey = "your_working_key";
$mode = "DEV"; // Use "PROD" for production

$ccavenue = CCAvenue::init($merchantId, $accessCode, $workingKey, $mode);

$transaction = $ccavenue->getTransaction();

try {
    $response = $transaction->verifyAndGetSuccessResponse();
    // Process successful response
    // For example, save order details to database
} catch (\Exception $e) {
    // Handle errors here
    echo $e->getMessage();
}

4. 设置 Webhook

为了接收自动支付更新,设置一个 webhook.php 文件

<?php

require_once __DIR__ . '/vendor/autoload.php';
use Imlolman\CCAvenue\CCAvenue;

$merchantId = "your_merchant_id";
$accessCode = "your_access_code";
$workingKey = "your_working_key";
$mode = "PROD"; // Webhooks are only available in production mode

$ccavenue = CCAvenue::init($merchantId, $accessCode, $workingKey, $mode);

$transaction = $ccavenue->getTransaction();

try {
    $response = $transaction->verifyAndGetSuccessResponse();
    // Process webhook response
    // For example, update order status in database
} catch (\Exception $e) {
    // Handle errors here
    echo $e->getMessage();
}

重要:通过电子邮件将您的 Webhook URL 发送给 CCAvenue,以便他们可以为您的账户启用 Webhook 支持。

可用方法 🧰

以下是 Imlolman\CCAvenue 库提供的便捷方法列表

initiate($compulsoryInfo, $billingInfo, $shippingInfo)

启动交易。提供订单详情、账单信息和运输信息。返回一个用于重定向客户的支付 URL。

$paymentUrl = $transaction->initiate($compulsoryInfo, $billingInfo, $shippingInfo);

verifyAndGetResponse()

验证来自 CCAvenue 的响应并返回响应数组。

$response = $transaction->verifyAndGetResponse();

verifyAndGetSuccessResponse()

验证响应并检查订单状态是否为 成功

try {
    $response = $transaction->verifyAndGetSuccessResponse();
    // Transaction is successful
} catch (\Exception $e) {
    // Transaction failed
}

checkIfOrderIsSuccess()

检查交易的订单状态是否成功。

$isSuccess = $transaction->checkIfOrderIsSuccess();

getOrderId()

从响应中检索订单 ID。

$orderId = $transaction->getOrderId();

getOrderAmount()

获取订单金额。

$amount = $transaction->getOrderAmount();

getOrderCurrency()

检索订单使用的货币。

$currency = $transaction->getOrderCurrency();

getPaymentMode()

获取使用的支付方式(例如,网上银行、信用卡)。

$paymentMode = $transaction->getPaymentMode();

getBankReferenceNumber()

检索交易的银行参考号。

$bankRefNo = $transaction->getBankReferenceNumber();

getTrackingId()

获取网关分配给交易的跟踪ID。

$trackingId = $transaction->getTrackingId();

getBillingInfo()

检索计费详情。

$billingInfo = $transaction->getBillingInfo();
// $billingInfo is an associative array

getDeliveryInfo()

检索发货详情。

$deliveryInfo = $transaction->getDeliveryInfo();
// $deliveryInfo is an associative array

getFailureMessage()

如果交易失败,获取失败信息。

$failureMessage = $transaction->getFailureMessage();

getMerchantParams()

检索交易过程中传递的任何商户特定参数。

$merchantParams = $transaction->getMerchantParams();
// $merchantParams is an associative array

理解流程 🧭

整个流程的工作方式如下:

最后思考 🤔

集成CCAvenue并非易事,但有了这份指南,您已迈向掌握它的道路。请记住

  • 白名单:向他们发送电子邮件以将您的测试域名和localhost加入白名单。
  • Webhooks:将您的Webhook URL发送给他们以接收自动更新。
  • 耐心:这是一个美德,尤其是在处理CCAvenue时。

快乐的编码!🎉