rvmehta745/payment-wallet

使用此包轻松集成 Paytm 钱包。此包使用官方的 Paytm PHP SDK

dev-master 2019-04-05 07:41 UTC

This package is auto-updated.

Last update: 2024-09-05 20:28:01 UTC


README

适用于 Laravel 5.0 及以上版本

介绍

使用此包将 Paytm 钱包集成到您的 Laravel 应用程序中。

许可证

Laravel Paytm Wallet 是一个开源软件,许可协议为 MIT 许可协议

开始使用

要开始使用,请使用以下命令将此包添加到您的 composer.json 文件中。

composer require rvmehta745/payment-wallet

配置

注意:对于 Laravel 5.5 及以上版本,自动发现会处理以下配置。

当 composer 成功安装 Laravel Paytm Wallet 库后,在您的 config/app.php 配置文件中注册 rvmehta745\PaymentWallet\PaytmWalletServiceProvider

'providers' => [
    // Other service providers...
    rvmehta745\PaymentWallet\PaytmWalletServiceProvider::class,
],

此外,将 PaytmWallet 门面添加到您的 app 配置文件中的 aliases 数组

'aliases' => [
    // Other aliases
    'PaytmWallet' => rvmehta745\PaymentWallet\Facades\PaytmWallet::class,
],

再走一步....

在您的 config/services.php 中添加以下配置

'paytm-wallet' => [
    'env' => 'production', // values : (local | production)
    'merchant_id' => 'YOUR_MERCHANT_ID',
    'merchant_key' => 'YOUR_MERCHANT_KEY',
    'merchant_website' => 'YOUR_WEBSITE',
    'channel' => 'YOUR_CHANNEL',
    'industry_type' => 'YOUR_INDUSTRY_TYPE',
],

注意:所有提到的凭据均由 Paytm 在作为商家注册后提供。

用法

进行交易

<?php

namespace App\Http\Controllers;

use PaytmWallet;

class OrderController extends Controller
{
    /**
     * Redirect the user to the Payment Gateway.
     *
     * @return Response
     */
    public function order()
    {
        $payment = PaytmWallet::with('receive');
        $payment->prepare([
          'order' => $order->id,
          'user' => $user->id,
          'mobile_number' => $user->phonenumber,
          'email' => $user->email,
          'amount' => $order->amount,
          'callback_url' => 'http://example.com/payment/status'
        ]);
        return $payment->receive();
    }

    /**
     * Obtain the payment information.
     *
     * @return Object
     */
    public function paymentCallback()
    {
        $transaction = PaytmWallet::with('receive');
        
        $response = $transaction->response() // To get raw response as array
        //Check out response parameters sent by paytm here -> http://paywithpaytm.com/developer/paytm_api_doc?target=interpreting-response-sent-by-paytm
        
        if($transaction->isSuccessful()){
          //Transaction Successful
        }else if($transaction->isFailed()){
          //Transaction Failed
        }else if($transaction->isOpen()){
          //Transaction Open/Processing
        }
        $transaction->getResponseMessage(); //Get Response Message If Available
        //get important parameters via public methods
        $transaction->getOrderId(); // Get order id
        $transaction->getTransactionId(); // Get transaction id
    }    
}

确保您在接收付款时在 routes.php 文件中提到的 callback_urlpost,例如以下示例

Route::post('/payment/status', 'OrderController@paymentCallback');

重要:必须将 callback_url 的 csrf 保护关闭 请查看这里了解如何操作

使用订单 ID 获取交易状态/信息

<?php

namespace App\Http\Controllers;

use PaytmWallet;

class OrderController extends Controller
{
    /**
    * Obtain the transaction status/information.
    *
    * @return Object
    */
    public function statusCheck(){
        $status = PaytmWallet::with('status');
        $status->prepare(['order' => $order->id]);
        $status->check();
        
        $response = $status->response() // To get raw response as array
        //Check out response parameters sent by paytm here -> http://paywithpaytm.com/developer/paytm_api_doc?target=txn-status-api-description
        
        if($status->isSuccessful()){
          //Transaction Successful
        }else if($status->isFailed()){
          //Transaction Failed
        }else if($status->isOpen()){
          //Transaction Open/Processing
        }
        $status->getResponseMessage(); //Get Response Message If Available
        //get important parameters via public methods
        $status->getOrderId(); // Get order id
        $status->getTransactionId(); // Get transaction id
    }
}

启动退款

<?php

namespace App\Http\Controllers;

use PaytmWallet;

class OrderController extends Controller
{
    /**
    * Initiate refund.
    *
    * @return Object
    */
    public function refund(){
        $refund = PaytmWallet::with('refund');
        $refund->prepare([
            'order' => $order->id,
            'reference' => "refund-order-4", // provide refund reference for your future reference (should be unique for each order)
            'amount' => 300, // refund amount 
            'transaction' => $order->transaction_id // provide paytm transaction id referring to this order 
        ]);
        $refund->initiate();
        $response = $refund->response() // To get raw response as array
        
        if($refund->isSuccessful()){
          //Refund Successful
        }else if($refund->isFailed()){
          //Refund Failed
        }else if($refund->isOpen()){
          //Refund Open/Processing
        }else if($refund->isPending()){
          //Refund Pending
        }
    }
}

检查退款状态

<?php

namespace App\Http\Controllers;

use PaytmWallet;

class OrderController extends Controller
{
    /**
    * Initiate refund.
    *
    * @return Object
    */
    public function refund(){
        $refundStatus = PaytmWallet::with('refund_status');
        $refundStatus->prepare([
            'order' => $order->id,
            'reference' => "refund-order-4", // provide reference number (the same which you have entered for initiating refund)
        ]);
        $refundStatus->check();
        
        $response = $refundStatus->response() // To get raw response as array
        
        if($refundStatus->isSuccessful()){
          //Refund Successful
        }else if($refundStatus->isFailed()){
          //Refund Failed
        }else if($refundStatus->isOpen()){
          //Refund Open/Processing
        }else if($refundStatus->isPending()){
          //Refund Pending
        }
    }
}

自定义正在处理的交易页面

考虑到现代应用程序的用户界面,默认的“正在处理的交易页面”太无聊了,这是随此包一起提供的。如果您想修改它,您有这个选项。下面是如何操作:您只需要在 OrderController 的代码中更改一行。

<?php

namespace App\Http\Controllers;

use PaytmWallet;

class OrderController extends Controller
{
    /**
     * Redirect the user to the Payment Gateway.
     *
     * @return Response
     */
    public function order()
    {
        $payment = PaytmWallet::with('receive');
        $payment->prepare([
          'order' => $order->id,
          'user' => $user->id,
          'mobile_number' => $user->phonenumber,
          'email' => $user->email,
          'amount' => $order->amount,
          'callback_url' => 'http://example.com/payment/status'
        ]);
        return $payment->view('your_custom_view')->receive();
    }

在这里,$payment->receive() 被替换为 $payment->view('your_custom_view')->receive()。将 your_custom_view 替换为您的视图名称,该视图位于您的 resources/views/your_custom_view.blade.php 中。

并且确保在您的视图文件中在 </body>(即关闭 body 标签之前)之前添加了以下代码行,该代码将重定向到支付网关。

@yield('payment_redirect')

以下是一个示例自定义视图

<html>
<head>
</head>
<body>
    <h1>Custom payment message</h1>
    @yield('payment_redirect')
</body>
</html>

就是这样!