canyin-project/ybcy/controllers/channel/CashierNotifyController.php

256 lines
13 KiB
PHP
Raw Normal View History

2024-11-01 16:07:54 +08:00
<?php
namespace app\controllers\channel;
use Illuminate\Support\Facades\DB;
use Yii;
use yii\web\Controller;
use app\models\common\WeChat;
use EasyWeChat\Factory;
use app\models\common\Bill;
use Yansongda\Pay\Pay;
//require('vendor/alipay/f2fpay/model/builder/AlipayTradePayContentBuilder.php');
class CashierNotifyController extends Controller{
public function actionPay(){
$result=axios_request();
//$storeId=$result['storeId']?:1;
$storeId='1';
$outTradeNo=$result['outTradeNo'];
$origin=1;
$uniacid = $this->wqData['uniacid'];
$auth_code=trim($result['auth_code']);
$this->payment($uniacid,$outTradeNo,$auth_code);
die;
if($auth_code){
$app=WeChat::cashierConfig($uniacid,$storeId,$origin);
$result = $app->order->unify([
'body' => '腾讯充值中心-QQ会员充值',
'out_trade_no' =>$outTradeNo,
'total_fee' => 1,
//'notify_url' => 'https://v2.bkycms.com/channel/cashier-notify/notify', // 支付结果通知网址,如果不设置则会使用配置里的默认地址
//'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
'auth_code'=>$auth_code
]);
if(!array_key_exists("return_code", $result) || !array_key_exists("result_code", $result)) {
echo json_encode(['code'=>1,'msg'=>'接口调用失败']);die;
}
//③、确认支付是否成功
$queryTimes = 10;
while($queryTimes > 0) {
$succResult = 0;
//③、确认支付是否成功
$queryResult = $this->queryOrder($uniacid,$storeId,$outTradeNo);
//如果需要等待1s后继续
if($queryResult['succCode'] == 2){
sleep(2);
continue;
} else if($queryResult['succCode'] == 1){//查询成功
$this->notify($outTradeNo);
} else {//订单交易失败
break;
}
}
echo json_encode(['code'=>1,'msg'=>'订单取消']);die;
}else{
echo json_encode(['code'=>1,'msg'=>'缺少付款码']);die;
}
}
/**
*
* 查询订单情况
* @param string $out_trade_no 商户订单号
* @param int $succCode 查询订单结果
* @return 0 订单不成功1表示订单成功2表示继续等待
*/
public function queryOrder($uniacid,$storeId,$outTradeNo){
try{
$origin=1;
$app=WeChat::cashierConfig($uniacid,$storeId,$origin);
$result=$app->order->queryByOutTradeNumber($outTradeNo);
} catch(Exception $e) {
}
if($result["return_code"] == "SUCCESS" && $result["result_code"] == "SUCCESS") {
//支付成功
if($result["trade_state"] == "SUCCESS"){
$result['succCode']=1;
return $result;
}
//用户支付中
else if($result["trade_state"] == "USERPAYING"){
$result['succCode']=2;
return false;
}
}
//如果返回错误码为“此交易订单号不存在”则直接认定失败
if($result["err_code"] == "ORDERNOTEXIST")
{
$succCode = 0;
} else{
//如果是系统错误,则后续继续
$succCode = 2;
}
return false;
}
public function notify($outTradeNo){
$order = (new \yii\db\Query())
->from('{{%ybwm_instore_order}}')
->where('outTradeNo=:outTradeNo', [':outTradeNo' => $outTradeNo])->one();
$res=Yii::$app->db->createCommand()->update('{{%ybwm_instore_order}}', ['state'=>2],'outTradeNo=:outTradeNo', [':outTradeNo' => $outTradeNo])->execute();
$transaction = Yii::$app->db->beginTransaction(); //开始事务
if($res) {
$bill = Bill::bill($order['uniacid'], 1, 1, $order['outTradeNo'], 1);
if (!$bill) {
$transaction->rollBack();//事务回滚
return false;
}
}
echo json_encode(['code'=>1,'msg'=>'支付成功']);die;
}
//-----------------------------支付宝条形码支付--------------------------------------------------
//支付宝条码支付直接调用ali_tiaoma_pay方法把条码和金额传进去就ok
function actionAlipay(){
$config =array(
'app_id' => '2019091867562464',
'ali_public_key' => 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjEKdm/ZchS/TmkEim2qrTfSFv4kq44KILM+7NaqsJTzrfZwWAWLeEqCHCM9n2lB/3cU0ImLPpHlzFLoy0hKgk+ZtbXhgfTLHtSErQL+WLqsNMe7mcrUYGO/9/51Z8X/b5UJgUzlVWoxnlROCfoGTQCmSmof5RtInWyXWk0CCj3u9jyzXsLhaonB3o1Y5rJEXyrQwCTyAgZlw9UesaKtq+l6J3ry2tHJSKt6tfH4nLA2F8CTteSOjmVd6y5RebqRhud6hBl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yFqN+xwZ9unJq83mv28qwbSY/TC4it6XSoq6cDgwhYloLWnj+D8Yejf8IiY1Ui/t23wZmbb96tZPywgmBu3zFAoGBAOBNZySYgjBoVMhJTMv3vtFdKKZZyScwCLA7jkwCmsA43biainv7vqpUnMVwZUUnsec2GHxrBrrt9Eh/xQYtoAKsnDroGCBzR/dajnJf9+gQH5M5iXHPZSX56Cx3hUzKmRyPlcMV5Fa/hmYEok7Um2pB20XqVL2MpolgkYjBCFqpAoGBAJLjnqx5ULlOihSX8+1DuOrgUaoVSpIgiD2kjdD2KGHd4J+1M2has/q0WJtgZEiiG/E73OX7uVGyqKFyiAc9UCLl7DX5MjKGCiy+8AxzypThhxW1AsOB8vBX7FuOQZ0q8NEhvxgkMKybvTBDHNqnA4saDK5Vdhx4AbG/D4wqs501AoGBAPKLLch4WckE9m+awheuuTp50ON8ghjwMvJg3u9DgDQber/8C9ig1nN1SQp9vXYSWW9sc70YvGpwBbD+V+XZD3yTbEiyt5ESVsFMhE19C2oBg9Dy64lJ8B4i1uJ9EhTeP3A8XjP2f0r8ClQwQoh0OL6rx7734ToZnUnnWOhxZ9Eh',
);
$order = [
'out_trade_no' => time(),
'total_amount' => '0.01',
'subject' => 'test subject-刷卡支付',
'auth_code' => '286636537649543523',
];
try {
$alipay = Pay::alipay($config)->pos($order);
}catch (\Exception $e){
//echo $e->getMessage();
// die(); // 终止异常
}
switch ($alipay->code) {
case "10000":
echo json_encode(['code'=>1,'msg'=>'支付成功']);die;
break;
case "10003":
echo json_encode(['code'=>2,'msg'=>'等待用户付款']);die;
break;
case "40004":
echo json_encode(['code'=>2,'msg'=>'支付失败']);die;
break;
default:
echo json_encode(['code'=>2,'msg'=>'不支持的交易状态,交易返回异常!!!']);die;
break;
}
}
public function actionCeshi(){
$config = [
'app_id' => 'wx2f926923ad50c5e5', // 小程序 APPID
'mch_id' => '1556224401',
'key' => 'ybkj888ybkj888ybkj888ybkj888ybkj',
];
$out_trade_no=2021030200009;
$order = [
'out_trade_no' =>$out_trade_no,
'total_fee' => 1,
'body' => 'test subject-微信刷卡支付',
'auth_code' => '134553238456978129',
];
try {
$wechat = Pay::wechat($config)->pos($order);
dd($wechat);die;
}catch (\Exception $e){
//echo $e->getMessage();
// die(); // 终止异常
}
//$result=Pay::wechat($config)->find($out_trade_no);
try {
$alipay = Pay::alipay($config)->pos($order);
dd($alipay);
}catch (\Exception $e){
//echo $e->getMessage();
// die(); // 终止异常
}
}
public function payment($uniacid,$outTradeNo,$auth_code){
$order=$order = (new \yii\db\Query())
->from('{{%ybwm_instore_order}}')
->where('outTradeNo=:outTradeNo', [':outTradeNo' => $outTradeNo])->one();
$money=0.01;
if(substr($auth_code , 0 , 2)=='28'){
$payType=2;
//$config=WeChat::payConfig($uniacid,$payType);
$config =array(
'app_id' => '2019091867562464',
'ali_public_key' => 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAjEKdm/ZchS/TmkEim2qrTfSFv4kq44KILM+7NaqsJTzrfZwWAWLeEqCHCM9n2lB/3cU0ImLPpHlzFLoy0hKgk+ZtbXhgfTLHtSErQL+WLqsNMe7mcrUYGO/9/51Z8X/b5UJgUzlVWoxnlROCfoGTQCmSmof5RtInWyXWk0CCj3u9jyzXsLhaonB3o1Y5rJEXyrQwCTyAgZlw9UesaKtq+l6J3ry2tHJSKt6tfH4nLA2F8CTteSOjmVd6y5RebqRhud6hBlVxoSV7FS7wTLVR3su8rgH6AAydB12IRd+TnGYYk5trNsqF2CLCk8sKcktL7OkUFiAHFooqcLqJpqTYCwIDAQAB',
'private_key' => 'MIIEpgIBAAKCAQEA6l/HNl169TQ+cwm80tHgoHF+FJtC5b26/Xw7dwy8UJiBB1p1WO1p9Si01WdSQbBwZcxUY+A347LE2R9FH70kc6d5Fan7vZTUAT+a6xF7+TwZshyTlNM2zB2/a07wXZqkrjuwIQ5AnqqbGhbyq6tqD4+H2J59SHgK21ZJpmbGoqDfOGJC79n8sGD2X9BGU9JS5STwqQN6lqY0/F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WJtgZEiiG/E73OX7uVGyqKFyiAc9UCLl7DX5MjKGCiy+8AxzypThhxW1AsOB8vBX7FuOQZ0q8NEhvxgkMKybvTBDHNqnA4saDK5Vdhx4AbG/D4wqs501AoGBAPKLLch4WckE9m+awheuuTp50ON8ghjwMvJg3u9DgDQber/8C9ig1nN1SQp9vXYSWW9sc70YvGpwBbD+V+XZD3yTbEiyt5ESVsFMhE19C2oBg9Dy64lJ8B4i1uJ9EhTeP3A8XjP2f0r8ClQwQoh0OL6rx7734ToZnUnnWOhxZ9Eh',
);
$order = [
'out_trade_no' =>$outTradeNo,
'total_fee' =>$money,
'body' => '收银台-支付宝当面付',
'auth_code' =>$auth_code,
];
try {
$alipay = Pay::alipay($config)->pos($order);
}catch (\Exception $e){
//echo $e->getMessage();
// die(); // 终止异常
}
switch ($alipay->code) {
case "10000":
echo json_encode(['code'=>1,'msg'=>'支付成功']);die;
break;
case "10003":
echo json_encode(['code'=>2,'msg'=>'等待用户付款']);die;
break;
case "40004":
echo json_encode(['code'=>2,'msg'=>'支付失败']);die;
break;
default:
echo json_encode(['code'=>2,'msg'=>'不支持的交易状态,交易返回异常!!!']);die;
break;
}
}else{
$payType=1;
$money=bcmul($money,100);
$config=WeChat::payConfig($uniacid,$payType);
$order = [
'out_trade_no' =>$outTradeNo,
'total_fee' =>$money,
'body' => '收银台-支付宝当面付',
'auth_code' =>$auth_code,
];
try {
$wechat = Pay::wechat($config)->pos($order);
}catch (\Exception $e){
//echo $e->getMessage();
// die(); // 终止异常
}
if($wechat->return_code=='SUCCESS'&&$wechat->return_code=='OK'&&$wechat->result_code=='SUCCESS'){
echo json_encode(['code'=>1,'msg'=>'支付成功']);die;
}else{
$wechat = Pay::wechat($config)->find($outTradeNo);
if($wechat->return_code=='SUCCESS'&&$wechat->return_code=='OK'&&$wechat->result_code=='SUCCESS'){
echo json_encode(['code'=>1,'msg'=>'支付成功']);die;
}else{
echo json_encode(['code'=>1,'msg'=>'订单取消']);die;
}
}
}
}
}