关于使用DokPay支付的流程逻辑
程序员文章站
2022-03-07 09:05:11
...
1.dokpay 提供phpdemo
public function test()
{
$url = "https://gatewaybeat.dokypay.com/clientapi/unifiedorder";//测试
$merkey = "bc2d******5fc0";//测试
// $url = "https://gateway.dokypay.com/clientapi/unifiedorder";//正式
// $merkey = "38462ca8f*****1ef";//正式
//--------------------------------开始拼装数据-------------------------
$merTransNo = date('YmdHis',time()) . rand(10, 99);
$info["version"] = "1.0";
$info["appId"] = "10***2384";//测试
//$info["appId"] = "100**818";//正式
$info["prodName"] = "southeast.asia";
$info["amount"] = "1.0";
$info["currency"] = "IDR";
$info["prodName"] = "southeast.asia";
$info["merTransNo"] = $merTransNo;
$info["notifyUrl"] = "http://www.videochats.pro/static/uploads/platforms/register/male_b.png";
$info["returnUrl"] = "http://www.baidu.com/";
// $info["pmId"] = "doku.wallet"; //可不填
$info["version"] = "1.0";
//签名
ksort($info);
$str = '';
foreach ($info as $key => $val) {
$str .= ($key . '=' . $val . '&');
}
$str .= ('key=' . $merkey);
$sign = bin2hex(hash("sha256", $str, true));
$info["sign"] = $sign;
print_r($info);
$requestJsonData = json_encode($info);
echo "Request data:" . $requestJsonData . "\r\n";
echo "---------------------------------------------------" . "\r\n";
//--------------------------------拼装数据结束-------------------------
//---------------------------------开始请求------------------------
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $requestJsonData);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($requestJsonData))
);
$response = curl_exec($ch);
curl_close($ch);
$obj = json_decode($response, true);
echo "Response object:";
print_r($obj);
}
执行test(),结果
Array
(
[amount] => 1.0
[appId] => 1001412384
[currency] => INR
[merTransNo] => 2019121615251487
[notifyUrl] => http://www.videochats.pro/apolloplatform/pay/notify
[prodName] => southeast.asia
[returnUrl] => http://www.videochats.pro/apolloplatform/pay/synnotice
[version] => 1.0
[sign] => a24d8b1bdcbd71802ce26945bd1fd1dbea10640cb10d1126f2f18422017924dc
)
Request data:{"amount":"1.0","appId":"1001412384","currency":"INR","merTransNo":"2019121615251487","notifyUrl":"http:\/\/www.videochats.pro\/apolloplatform\/pay\/notify","prodName":"southeast.asia","returnUrl":"http:\/\/www.videochats.pro\/apolloplatform\/pay\/synnotice","version":"1.0","sign":"a24d8b1bdcbd71802ce26945bd1fd1dbea10640cb10d1126f2f18422017924dc"}
---------------------------------------------------
Response object:Array
(
[data] => Array
(
[amount] => 1.0
[currency] => INR
[doing] => redirect
[merTransNo] => 2019121615251487
[message] => success
[resultCode] => 0000
[sign] => de7f2d0f636e2a5eb138ca20270bed85808285890256320fcca6674119ca295b
[tradeNo] => 20191216152411871010478520
[url] => https://gatewaybeta.dokypay.com/cashierdesk/allfacility/07abb850e0f949c2b2024833a989b187
)
[code] => 200
[msg] => OK
[timestamp] => 1576481052018
)
2.完整 控制器方法 pay.php
<?php
/**
* Created by PhpStorm.
* User: kay
* Date: 2019/11/29
* Time: 2:33 PM
*/
//Dokypay 支付
class Pay extends Base_Controller
{
static $version = '1.0';//当前api版本
// static $url = 'https://gateway.dokypay.com/clientapi/unifiedorder';//生产环境::支付请求url
//static $appId = '10***18';// 正式应用id
//static $appKey = '729****0cb0ef';//应用key
static $url = 'https://gatewaybeta.dokypay.com/clientapi/unifiedorder';//测试环境::支付请求url
static $appId = '100***384';//测试应用id
static $appKey = 'bc2d5fc******4fd2d4a0b6b';//测试应用key
static $merchantId = '';
static $merchantKey = '';
static $prodName = 'southeast.asia';
static $country = '';
static $notifyUrl = 'http://www.**.pro/apolloplatform/pay/notify';//生产环境::异步回调url
static $returnUrl = 'http://www.**.pro/apolloplatform/pay/synnotice';//生产环境::同步通知地址
// static $notifyUrl = 'http://api.xyh.com/apolloplatform/pay/notify';//测试环境::异步回调url
// static $returnUrl = 'http://api.xyh.com/apolloplatform/pay/synnotice';//测试环境::同步通知地址
public $priceList = [
10512101 => [
'type' => 10512101,
'title' => '30 days',
'price' => '199',
],
10512102 => [
'type' => 10512102,
'title' => '90 days',
'price' => '499.0'
],
10512103 => [
'type' => 10512103,
'title' => '180 days',
'price' => '799.0'
],
];
public function __construct()
{
parent::__construct();
$this->load->model("apolloplatform/User_model", 'user');
}
//todo 处理订单 添加字段userid serverid 订单信息入库
public function handleOrder(){
$param=$this->input->post();
$userid = $param['userId'];
if(empty($userid)){
$returnData=array();
$returnData['msg']='fail';
$returnData['code']=0;
$returnData['data'] = '';
die(json_encode($returnData));
}
$goods["amount"] = $this->priceList[$param['serviceId']]['price'];
$goods["currency"] = "INR";
$goods['merTransNo'] = date('YmdHis',time()) . rand(10, 99);
$goods['appId'] = self::$appId;
//$goods['country'] = self::$country;
$goods['notifyUrl'] = self::$notifyUrl;
$goods['prodName'] = self::$prodName;
$goods['returnUrl'] = self::$returnUrl;
$goods['version'] = self::$version;
$sign = $this->setSign($goods,self::$appKey);
$goods['sign'] = $sign;
$res = $this->curlPay($goods);
$dokreturn=json_decode($res,true);
if ($dokreturn['data']['url']){//支付成功
//订单信息添加到数据库
$orderinfo = array(
'userid'=>$userid,
'serviceid'=>$param['serviceId'],
'amount'=>$goods["amount"],
'orderno'=>$goods['merTransNo'],
'status'=>0,
'start_time'=>time(),
'end_time'=>time() + intval($this->priceList[$param['serviceId']]['title'])*24*3600,
'created_at'=>time()
);
$this->user->addorder($orderinfo);
$returnData=array();
$returnData['msg']='success';
$returnData['code']=1;
$returnData['data']=$dokreturn['data']['url'];
print_r(json_encode($returnData,true));
}else{//支付失败页面
throw new HttpException(400, "参数有误");
}
}
//todo 生成签名
public function setSign($data, $secret)
{
ksort($data);
$str = '';
foreach ($data as $key => $val) {
$str .= ($key . '=' . $val . '&');
}
$str .= ('key=' . $secret);
return bin2hex(hash("sha256", $str, true));
}
//todo 发起支付
public function curlPay($data){
$jsondata = json_encode($data,JSON_UNESCAPED_UNICODE);
$ch = curl_init(self::$url);
$header[] = 'Content-Type: application/json';
$header[] = 'Content-Length:'.strlen($jsondata);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_POSTFIELDS,$jsondata);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,true);
$returndata = curl_exec($ch);
return $returndata;
}
//todo 接收dockpay会调通知 notifyUrl
public function notify(){
$params = $this->input->post();
$amount = isset($params['amount'])?$params['amount']:'';
$currency = isset($params['currency'])?$params['currency']:'';
$merTransNo = isset($params['merTransNo'])?$params['merTransNo']:'';
$processCurrency = isset($params['processCurrency'])?$params['processCurrency']:'';
$sign = isset($params['sign'])?$params['sign']:'';
$transNo = isset($params['transNo'])?$params['transNo']:'';
$transStatus = isset($params['transStatus'])?$params['transStatus']:'';
$processAmount = isset($params['processAmount'])?$params['processAmount']:'';
$createTime = isset($params['createTime'])?$params['createTime']:'';
$updateTime = isset($params['updateTime'])?$params['updateTime']:'';
$payerName = isset($params['payerName'])?$params['payerName']:'';
$payerEmail = isset($params['payerEmail'])?$params['payerEmail']:'';
$payerMobile = isset($params['payerMobile'])?$params['payerMobile']:'';
$singarr = array(
'amount'=>$amount,
'currency'=>$currency,
'merTransNo'=>$merTransNo,
'processCurrency'=>$processCurrency,
'transNo'=>$transNo,
'transStatus'=>$transStatus,
'processAmount'=>$processAmount,
'createTime'=>$createTime,
'updateTime'=>$updateTime
);
if($payerName){
$singarr['payerName'] = $payerName;
}
if($payerEmail){
$singarr['payerEmail'] = $payerEmail;
}
if($payerMobile){
$singarr['payerMobile'] = $payerMobile;
}
$signstr = $this->setSign($singarr,self::$merchantKey);
//签名不正确
if($signstr != $sign){
}
//支付成功修改订单状态
if($transStatus == 'success'){
$this->user->changeorder(['orderno'=>$transNo],['status'=>1]);
}
}
/**同步回调 returnurl
* User: lxw
* Date: 2019-12-07 16:19
*/
public function synnotice()
{
$params = $this->input->get();
if ($params['transStatus'] == 'success') {
$this->load->view('apolloplatform/payok', $params);
} else {
$this->load->view('apolloplatform/payerror', $params);
}
}
/**选择支付方式页面
* User: lxw
* Date: 2019-12-06 17:54
*/
public function replySelect()
{
$param=$this->input->get();
$param['price']=$this->priceList[$param['serviceId']]['price'];
$param['title']=$this->priceList[$param['serviceId']]['title'];
//TODO::上线改 Subscription google支付页面; Subscription2 google支付页面 和paytam 支付两者都有
$userInfo = $this->user->userinfo(['guid' => $param['userId']]);
// $this->dd($userInfo);
// if ($userInfo['platformInfo']=='77dde5df313b2633'){
// $this->load->view('apolloplatform/Subscription2', $param);
// }else{
// $this->load->view('apolloplatform/Subscription', $param);
// }
$this->load->view('apolloplatform/Subscription2', $param);
}
//google支付成功,保存订单信息,有信息传到服务端,就表示已经支付成功
public function googleorder(){
$params = $this->input->post();
if(empty($params)){
return die(json_encode(["msg" => "error", "isSucceed" => "-1"]));
}
$userid = isset($params['userId'])?$params['userId']:'';
$serviceid = isset($params['serviceId'])?$params['serviceId']:'';
$transactionId = isset($params['transactionId'])?$params['transactionId']:'';
if(empty($userid) || empty($serviceid) || empty($transactionId)){
$error = ["msg" => "error", "isSucceed" => "-1"];
die(json_encode($error));
}
$orderinfo = array(
'userid'=>$userid,
'serviceid'=>$serviceid,
'amount'=>'',
'orderno'=>$transactionId,
'status'=>1,
'start_time'=>time(),
'end_time'=>time() + intval($this->priceList[$params['serviceId']]['title'])*24*3600,
'created_at'=>time()
);
$res = $this->user->addorder($orderinfo);//订单信息增加成功后,将用户改为vip状态
if($res){
$this->user->updateuserinfo(['guid'=>$userid],['paid'=>1]);
die(json_encode(["msg"=>"ok","isSucceed" => "1"]));
}
die(json_encode(["msg"=>"error","isSucceed" => "-1"]));
}
}
3.github Yii 框架的使用者
下一篇: Moousture: 鼠标手势库