-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from NimaQu/yftpay
加入易付通支付
- Loading branch information
Showing
13 changed files
with
704 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
namespace App\Controllers; | ||
|
||
class PayConfig{ | ||
|
||
public $pay_config; | ||
|
||
public function init() | ||
{ | ||
$this->pay_config = [ | ||
"notify_url" => "/yft/notify", | ||
"return_url" => "/yft/notify", | ||
"type" => "aliPay" | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
<?php | ||
|
||
namespace App\Controllers; | ||
|
||
use app\Utils\YftOrderNumUtil; | ||
|
||
class QuickPayFunction{ | ||
public function md5Verify($p1, $p2,$p3,$p4,$sign) { | ||
$preStr = $p1.$p2.$p3.$p4; | ||
$mySign = md5($preStr); | ||
// echo $mySign; | ||
if($mySign == $sign) { | ||
return true; | ||
}else { | ||
return false; | ||
} | ||
} | ||
|
||
/** | ||
* 建立请求,以表单HTML形式构造(默认) | ||
* @param $para_temp 请求参数数组 | ||
* | ||
*/ | ||
public function buildRequestForm($para_temp,$ss_order_no,$pay_config) { | ||
//待请求参数数组 | ||
$para = QuickPayFunction::buildRequestPara($para_temp); | ||
|
||
|
||
$sHtml = "<form id='paysubmit' name='paysubmit' action='https://pay.anypay.me/pay/subOrder/zfb' accept-charset='utf-8' method='POST'>"; | ||
while (list ($key, $val) = each ($para)) { | ||
$sHtml.= "<input type='hidden' name='".$key."' value='".$val."'/>"; | ||
} | ||
if ($pay_config->pay_config["type"] == "aliPay"){ | ||
$sHtml .= "<input type='hidden' name='subject' value='余额充值'/>"; | ||
}else{ | ||
$sHtml .= "<input type='hidden' name='trade_no' value='".$ss_order_no."'/>"; | ||
} | ||
//submit按钮控件请不要含有name属性 | ||
$sHtml = $sHtml."</form>"; | ||
|
||
$sHtml = $sHtml."<script>document.forms['paysubmit'].submit();</script>"; | ||
|
||
return $sHtml; | ||
} | ||
/** | ||
* 生成要请求给云通付的参数数组 | ||
* @param $para_temp 请求前的参数数组 | ||
* @return 要请求的参数数组 | ||
*/ | ||
static function buildRequestPara($para_temp) { | ||
//除去待签名参数数组中的空值和签名参数 | ||
$para_filter = QuickPayFunction::paraFilter($para_temp); | ||
//生成签名结果 | ||
$mysign = QuickPayFunction::buildRequestMysign($para_filter); | ||
|
||
//签名结果与签名方式加入请求提交参数组中 | ||
$para_filter['sign'] = $mysign; | ||
|
||
return $para_filter; | ||
} | ||
/** | ||
* 除去数组中的空值和签名参数 | ||
* @param $para 签名参数组 | ||
* return 去掉空值与签名参数后的新签名参数组 | ||
*/ | ||
static function paraFilter($para) { | ||
$para_filter = array(); | ||
while (list ($key, $val) = each ($para)) { | ||
if($key == "sign" || $val == "")continue; | ||
else $para_filter[$key] = $para[$key]; | ||
} | ||
return $para_filter; | ||
} | ||
/** | ||
* 生成签名结果 | ||
* @param $para_filter 要签名的数组 | ||
* return 签名结果字符串 | ||
*/ | ||
static function buildRequestMysign($para_filter) { | ||
//把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串 | ||
$prestr = QuickPayFunction::createLinkstring($para_filter); | ||
$mysign = MD5($prestr); | ||
return $mysign; | ||
} | ||
static function md5Sign($prestr) { | ||
return md5($prestr); | ||
} | ||
/** | ||
* 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串 | ||
* @param $para 需要拼接的数组 | ||
* return 拼接完成以后的字符串 | ||
*/ | ||
static function createLinkstring($para) { | ||
$arg = ""; | ||
while (list ($key, $val) = each ($para)) { | ||
$arg.=$key."=".$val."&"; | ||
} | ||
//去掉最后一个&字符 | ||
$arg = substr($arg,0,count($arg)-2); | ||
|
||
//如果存在转义字符,那么去掉转义 | ||
if(get_magic_quotes_gpc()){$arg = stripslashes($arg);} | ||
|
||
return $arg; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
<?php | ||
/** | ||
* Created by 傲慢与偏见. | ||
* User: Administrator | ||
* Date: 2017/11/21 | ||
* Time: 8:57 | ||
* | ||
* 虽然代码开源,但是请别直接照搬好吗?直接照搬也别改的面目全非好吗?改的面目全非也别删除注释好吗? | ||
* | ||
* 抄代码之前想想为什么这样写,这样才能有提高。建议你去学学面向对象编程方式。 | ||
* | ||
* 另外,我看了你的代码,真的写的很烂,乱。 | ||
* | ||
*/ | ||
|
||
namespace App\Controllers; | ||
|
||
use App\Models\Payback; | ||
use App\Models\User; | ||
use App\Models\YftOrder; | ||
use App\Services\Config; | ||
use App\Utils\Telegram; | ||
|
||
class YFTPayCallBackController | ||
{ | ||
public function yft_notify($request) | ||
{ | ||
//价格 | ||
$total_fee = $request->getQueryParams()["total_fee"];//必填 | ||
//易付通返回的订单号 | ||
$yft_order_no = $request->getQueryParams()["trade_no"]; | ||
//面板生成的订单号 | ||
$ss_order_no = $request->getQueryParams()["out_trade_no"];//必填 | ||
//订单说明 | ||
$subject = $request->getQueryParams()["subject"];//必填 | ||
//付款状态 | ||
$trade_status = $request->getQueryParams()["trade_status"];//必填 | ||
//加密验证字符串 | ||
$sign = $request->getQueryParams()["sign"];//必填 | ||
|
||
$verifyNotify = YFTPayCallBackController::md5Verify(floatval($total_fee), $ss_order_no, $yft_order_no, $trade_status, $sign); | ||
if ($verifyNotify) {//验证成功 | ||
if ($trade_status == 'TRADE_SUCCESS') { | ||
/* | ||
加入您的入库及判断代码; | ||
>>>>>>>!!!为了保证数据传达到回调地址,会请求4次。所以必须要先判断订单状态,然后再插入到数据库,这样后面即使请求3次,也不会造成订单重复!!!!<<<<<<< | ||
判断返回金额与实金额是否想同; | ||
判断订单当前状态; | ||
完成以上才视为支付成功 | ||
*/ | ||
$orderInfo = new YftOrder(); | ||
$orderInfo = $orderInfo->where("ss_order", "=", $ss_order_no)->first(); | ||
if ($orderInfo == "" || $orderInfo == null) { | ||
return "订单不存在!"; | ||
} | ||
|
||
if ($orderInfo->price != $total_fee) { | ||
return "订单信息异常!"; | ||
} | ||
|
||
$userInfo = new User(); | ||
$userInfo = $userInfo->where("id", "=", $orderInfo->user_id)->first(); | ||
|
||
if (sizeof($orderInfo) != 0 && $orderInfo->state == 0) { | ||
$oldMoney = $userInfo->money; | ||
$userInfo->money = $total_fee + $oldMoney; | ||
//更新用户余额信息 | ||
$userInfo->save(); | ||
//更新订单信息 | ||
$orderInfo->yft_order = $yft_order_no; | ||
$orderInfo->state = 1; | ||
$orderInfo->save(); | ||
//充值返利处理 start | ||
if ($userInfo->ref_by != "" && $userInfo->ref_by != 0 && $userInfo->ref_by != null && Config::get('code_payback') != 0 && Config::get('code_payback') != null) { | ||
$gift_user = User::where("id", "=", $userInfo->ref_by)->first(); | ||
$gift_user->money = ($gift_user->money + ($total_fee * (Config::get('code_payback') / 100))); | ||
$gift_user->save(); | ||
|
||
$Payback = new Payback(); | ||
$Payback->total = $total_fee; | ||
$Payback->userid = $userInfo->id; | ||
$Payback->ref_by = $userInfo->ref_by; | ||
$Payback->ref_get = $total_fee * (Config::get('code_payback') / 100); | ||
$Payback->datetime = time(); | ||
$Payback->save(); | ||
} | ||
//充值返利处理 end | ||
//telegram提醒 | ||
if (Config::get('enable_donate') == 'true' && Config::get("enable_telegram") == 'true') { | ||
if ($userInfo->is_hide == 1) { | ||
Telegram::Send("感谢!一位不愿透露姓名的大老爷给我们捐了 " . $total_fee . " 元呢~"); | ||
} else { | ||
Telegram::Send("感谢!" . $userInfo->user_name . " 大老爷给我们捐了 " . $total_fee . " 元呢~"); | ||
} | ||
} | ||
} else { | ||
return "订单号异常或交易已完成!"; | ||
} | ||
header("location:" . $request->getUri()->getScheme() . "://" . $request->getUri()->getHost() . "/user/code"); | ||
return "支付成功"; | ||
} else { | ||
return "支付失败"; | ||
} | ||
} else { | ||
//验证失败 | ||
return "订单信息异常!请联系管理员"; | ||
} | ||
} | ||
|
||
/** | ||
* @param $p1 | ||
* @param $p2 | ||
* @param $p3 | ||
* @param $p4 | ||
* @param $sign 传入要比对的sign | ||
* @return boolean 返回比对结果 | ||
*/ | ||
private static function md5Verify($p1, $p2, $p3, $p4, $sign) | ||
{ | ||
$preStr = $p1 . $p2 . $p3 . $p4 . "yft"; | ||
$mySign = md5($preStr); | ||
if ($mySign == $sign) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
} |
Oops, something went wrong.