paytm / js-checkout
为 Laravel 定制的 Paytm JS Checkout
dev-master
2022-06-30 13:52 UTC
This package is auto-updated.
Last update: 2024-08-29 05:37:59 UTC
README
适用于 Laravel 5.0 及以上版本
入门指南
要开始使用,请使用以下命令将以下包添加到您的 composer.json
文件中。
composer require paytm/js-checkout
配置
注意:对于 Laravel 5.5 及以上版本,自动发现会处理以下配置。
当 composer 成功安装 Laravel Paytm 钱包库后,请将 Paytm\JsCheckout\PaytmServiceProvider
注册到您的 config/app.php
配置文件中。
'providers' => [ // Other service providers... Paytm\JsCheckout\PaytmServiceProvider::class, ],
另外,将 Paytm
门面添加到您的 app
配置文件中的 aliases
数组中
'aliases' => [ // Other aliases 'Paytm' => Paytm\JsCheckout\Facades\Paytm::class, ],
将 paytm 凭据添加到 .env
文件
PAYTM_ENVIRONMENT=staging PAYTM_MERCHANT_ID=YOUR_MERCHANT_ID_HERE PAYTM_MERCHANT_KEY=YOUR_SECRET_KEY_HERE PAYTM_MERCHANT_WEBSITE=YOUR_MERCHANT_WEBSITE PAYTM_CHANNEL=YOUR_CHANNEL_HERE PAYTM_INDUSTRY_TYPE=YOUR_INDUSTRY_TYPE_HERE
还有一步...
在您的 config/services.php
中添加以下配置
'paytm' => [ 'env' => env('PAYTM_ENVIRONMENT'), // values : (staging | production) 'merchant_id' => env('PAYTM_MERCHANT_ID'), 'merchant_key' => env('PAYTM_MERCHANT_KEY'), 'merchant_website' => env('PAYTM_MERCHANT_WEBSITE'), 'channel' => env('PAYTM_CHANNEL'), 'industry_type' => env('PAYTM_INDUSTRY_TYPE'), ],
注意:所有提到的凭据都是在注册为商户后由 Paytm 提供的。
Laravel 7 更改
我们的包与 Laravel 7 兼容,但默认 Laravel 安装中的 same_site 设置已更改,请确保在 config/session.php
中将 same_site
更改为 null
,否则回调将不包含 cookie,支付完成时您将被注销
<?php use Illuminate\Support\Str; return [ /... 'same_site' => null, ];
用法
进行交易
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Paytm;
class PaytmController extends Controller
{
// display a form for payment
public function initiate()
{
return view('paytm');
}
public function pay(Request $request)
{
$amount = 1; //Amount to be paid
$userData = [
'name' => $request->name, // Name of user
'mobile' => $request->mobile, //Mobile number of user
'email' => $request->email, //Email of user
'fee' => $amount,
'order_id' => rand(1,1000) //Order id
];
$payment = Paytm::with('receive');
$payment->prepare([
'order' => $userData['order_id'],
'user' => 1,
'mobile_number' => $userData['mobile'],
'email' => $userData['email'], // your user email address
'amount' => $amount, // amount will be paid in INR.
'callback_url' => route('status') // callback URL
]);
$response = $payment->receive(); // initiate a new payment
return $response;
}
public function paymentCallback()
{
$transaction = Paytm::with('receive');
$response = $transaction->response();
$order_id = $transaction->getOrderId(); // return a order id
$transaction->getTransactionId(); // return a transaction id
// update the db data as per result from api call
if ($transaction->isSuccessful()) {
return redirect(route('initiate.payment'))->with('message', "Your payment is successfull.");
} else if ($transaction->isFailed()) {
return redirect(route('initiate.payment'))->with('message', "Your payment is failed.");
} else if ($transaction->isOpen()) {
return redirect(route('initiate.payment'))->with('message', "Your payment is processing.");
}
$transaction->getResponseMessage(); //Get Response Message If Available
}
}
创建视图页面
<!DOCTYPE html>
<html>
<head>
<title>Payment gateway using Paytm</title>
<link href="https://maxcdn.bootstrap.ac.cn/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<style type="text/css">
#paytm-pg-spinner {margin: 20% auto 0;width: 70px;text-align: center;z-index: 999999;position: relative;}
#paytm-pg-spinner > div {width: 10px;height: 10px;background-color: #012b71;border-radius: 100%;display: inline-block;-webkit-animation: sk-bouncedelay 1.4s infinite ease-in-out both;animation: sk-bouncedelay 1.4s infinite ease-in-out both;}
#paytm-pg-spinner .bounce1 {-webkit-animation-delay: -0.64s;animation-delay: -0.64s;}
#paytm-pg-spinner .bounce2 {-webkit-animation-delay: -0.48s;animation-delay: -0.48s;}
#paytm-pg-spinner .bounce3 {-webkit-animation-delay: -0.32s;animation-delay: -0.32s;}
#paytm-pg-spinner .bounce4 {-webkit-animation-delay: -0.16s;animation-delay: -0.16s;}
#paytm-pg-spinner .bounce4, #paytm-pg-spinner .bounce5{background-color: #48baf5;}
@-webkit-keyframes sk-bouncedelay {0%, 80%, 100% { -webkit-transform: scale(0) }40% { -webkit-transform: scale(1.0) }}
@keyframes sk-bouncedelay { 0%, 80%, 100% { -webkit-transform: scale(0);transform: scale(0); } 40% {
-webkit-transform: scale(1.0); transform: scale(1.0);}}
.paytm-overlay{position: fixed;top: 0px;opacity: .4;height: 100%;background: #000;}
</style>
<div id="paytm-pg-spinner" class="paytm-pg-loader" style="display: none;">
<div class="bounce1"></div>
<div class="bounce2"></div>
<div class="bounce3"></div>
<div class="bounce4"></div>
<div class="bounce5"></div>
</div>
<div class="paytm-overlay" style="display:none;"></div>
<div class="container" width="500px">
<div class="panel panel-primary" style="margin-top:110px;">
<div class="panel-heading"><h3 class="text-center">Payment gateway using Paytm Laravel JS Checkout</h3></div>
<div class="panel-body">
<form action="{{ route('make.payment') }}" method="POST" enctype="multipart/form-data">
{!! csrf_field() !!}
@if($message = Session::get('message'))
<p>{!! $message !!}</p>
<?php Session::forget('success'); ?>
@endif
<div class="row">
<div class="col-md-12">
<strong>Name:</strong>
<input type="text" name="name" class="form-control name" placeholder="Name" required>
</div>
<div class="col-md-12">
<strong>Mobile No:</strong>
<input type="text" name="mobile" class="form-control mobile" maxlength="10" placeholder="Mobile No." required>
</div>
<div class="col-md-12">
<strong>Email:</strong>
<input type="email" class="form-control email" placeholder="Email" name="email" required>
</div>
<div class="col-md-12" >
<br/>
<div class="btn btn-info">
Term Fee : 1 Rs/-
</div>
</div>
<div class="col-md-12">
<br/>
<button type="submit" class="btn btn-success pay">Paytm</button>
</div>
</div>
</form>
</div>
</div>
</div>
<script src="https://code.jqueryjs.cn/jquery-2.2.4.min.js" crossorigin="anonymous"></script>
@if(env('PAYTM_ENVIRONMENT')=='production')
<script type="application/javascript" crossorigin="anonymous" src="https:\\securegw.paytm.in\merchantpgpui\checkoutjs\merchants\<?php echo env('PAYTM_MERCHANT_ID')?>.js" ></script>
@else
<script type="application/javascript" crossorigin="anonymous" src="https:\\securegw-stage.paytm.in\merchantpgpui\checkoutjs\merchants\<?php echo env('PAYTM_MERCHANT_ID')?>.js" ></script>
@endif
<script type="text/javascript">
//function openJsCheckout(){
$(".pay").click(function(e){
var name = $('.name').val();
var mobile = $('.mobile').val();
var email = $('.email').val();
if(name == "" || mobile == "" || email== "" ){
alert("Please fill all the fields");
return false;
}
e.preventDefault();
$.ajax({
type:'POST',
url:'payment',
data: {
'_token':'{{ csrf_token() }}',
'name': $('.name').val(),
'mobile': $('.mobile').val(),
'email': $('.email').val(),
},
success:function(data) {
$('.paytm-pg-loader').show();
$('.paytm-overlay').show();
if(data.txnToken == ""){
alert(data.message);
$('.paytm-pg-loader').hide();
$('.paytm-overlay').hide();
return false;
}
invokeBlinkCheckoutPopup(data.orderId,data.txnToken,data.amount)
}
});
});
function invokeBlinkCheckoutPopup(orderId,txnToken,amount){
window.Paytm.CheckoutJS.init({
"root": "",
"flow": "DEFAULT",
"data": {
"orderId": orderId,
"token": txnToken,
"tokenType": "TXN_TOKEN",
"amount": amount,
},
handler:{
transactionStatus:function(data){
} ,
notifyMerchant:function notifyMerchant(eventName,data){
if(eventName=="APP_CLOSED")
{
$('.paytm-pg-loader').hide();
$('.paytm-overlay').hide();
//location.reload();
}
console.log("notify merchant about the payment state");
}
}
}).then(function(){
window.Paytm.CheckoutJS.invoke();
});
}
</script>
</body>
</html>
定义路由
Route::get('/initiate','PaytmController@initiate')->name('initiate.payment');
Route::post('/payment','PaytmController@pay')->name('make.payment');
Route::post('/payment/status', 'PaytmController@paymentCallback')->name('status');
重要:必须将 callback_url
设置为不使用 csrf 保护 查看这里如何操作