174 lines
5.7 KiB
PHP
174 lines
5.7 KiB
PHP
<?php
|
|
|
|
namespace sdkwarpper;
|
|
|
|
require 'BaseSdk.php';
|
|
|
|
use phpcommon;
|
|
use classes;
|
|
|
|
class UnifiedWeiXin extends BaseSdk {
|
|
|
|
function __construct()
|
|
{
|
|
parent::__construct();
|
|
$this->conf_appid = '';
|
|
$this->conf_mch_id = '';
|
|
$this->conf_pay_secret_key = '';
|
|
}
|
|
|
|
public function setChannel($channel)
|
|
{
|
|
parent::setChannel($channel);
|
|
$this->unified_channel = $this->channel;
|
|
}
|
|
|
|
private function loadConfig()
|
|
{
|
|
$config_name = "../config/game{$this->gameid}/{$this->channel}/config.php";
|
|
$this->config = require($config_name);
|
|
$this->conf_appid = $this->config['appid'];
|
|
$tmp_str = explode('|', $this->config['pay_secret_key']);
|
|
$this->conf_mch_id = $tmp_str[0];
|
|
$this->conf_pay_secret_key = $tmp_str[1];
|
|
}
|
|
|
|
public function preOrder()
|
|
{
|
|
if (!($_REQUEST['unified_trade_type'] == 'MWEB' || $_REQUEST['unified_trade_type'] == 'JSAPI')) {
|
|
phpcommon\sendError(1, '参数错误,unified_trade_type必须为MWEB或JSAPI');
|
|
return;
|
|
}
|
|
if (!phpcommon\isValidSessionId($_REQUEST['account_id'], $_REQUEST['session_id'])) {
|
|
phpcommon\sendError(1, 'session无效');
|
|
return;
|
|
}
|
|
$this->gameid = phpcommon\extractGameId($_REQUEST['account_id']);
|
|
$this->real_channel = phpcommon\extractChannel($_REQUEST['account_id']);
|
|
$this->loadConfig();
|
|
|
|
$cp_orderid = $this->order_ctrl->genOrderId($_REQUEST['account_id']);
|
|
{
|
|
$this->order_ctrl->addPreOrder(
|
|
array(
|
|
'orderid' => $cp_orderid,
|
|
'account_id' => $_REQUEST['account_id'],
|
|
'roleid' => '',
|
|
'server_id' => 0,
|
|
'itemid' => 1,
|
|
'price' => 101,
|
|
'poly_sdk_channel' => $this->poly_sdk_channel,
|
|
'unified_channel' => $this->unified_channel,
|
|
'ipv4' => phpcommon\getIPv4(),
|
|
)
|
|
);
|
|
}
|
|
|
|
$openid = phpcommon\extractOpenId($_REQUEST['account_id']);
|
|
$params = array(
|
|
'appid' => $this->conf_appid,
|
|
'mch_id' => $this->conf_mch_id,
|
|
'nonce_str' => md5($cp_orderid . time()),
|
|
'body' => '测试产品',
|
|
'out_trade_no' => $cp_orderid,
|
|
'total_fee' => '101',
|
|
'spbill_create_ip' => phpcommon\getIPv4(),
|
|
'notify_url' => 'https://gamepay.kingsome.cn/webapp/paynotify/unified_weixin.php',
|
|
'trade_type' => $_REQUEST['unified_trade_type'],
|
|
//'scene_info' => '',
|
|
);
|
|
if ($_REQUEST['unified_trade_type'] == 'JSAPI') {
|
|
$params['openid'] = $openid;
|
|
}
|
|
if (SERVER_ENV != _ONLINE) {
|
|
$params['notify_url'] = 'https://gamepay-test.kingsome.cn/webapp/paynotify/unified_weixin.php';
|
|
}
|
|
$sign = phpcommon\wxPaySign($params,
|
|
$this->conf_pay_secret_key
|
|
);
|
|
$params['sign'] = strtoupper($sign);
|
|
error_log(json_encode($params));
|
|
error_log(phpcommon\objectToXml($params));
|
|
$response = '';
|
|
if (!phpcommon\HttpClient::postContent(
|
|
'https://api.mch.weixin.qq.com/pay/unifiedorder',
|
|
phpcommon\objectToXml($params),
|
|
$response
|
|
)) {
|
|
phpcommon\sendError(1, '服务器内部错误');
|
|
return;
|
|
}
|
|
error_log($response);
|
|
$xml = new \SimpleXMLElement($response);
|
|
if ($xml->return_code != 'SUCCESS') {
|
|
phpcommon\sendError(2, '' .$xml->return_msg);
|
|
return;
|
|
}
|
|
if ($xml->err_code != 'SUCCESS') {
|
|
phpcommon\sendError(2, '' .$xml->err_code_des);
|
|
return;
|
|
}
|
|
if ($_REQUEST['unified_trade_type'] == 'MWEB') {
|
|
echo json_encode(array(
|
|
'errcode' => 0,
|
|
'errmsg' => '',
|
|
'cp_orderid' => $cp_orderid,
|
|
'prepayid' => '' . $xml->prepay_id,
|
|
'pay_jump_url' => '' . $xml->mweb_url
|
|
));
|
|
} else {
|
|
echo json_encode(array(
|
|
'errcode' => 0,
|
|
'errmsg' => '',
|
|
'cp_orderid' => $cp_orderid,
|
|
'prepayid' => $this->genJSApiData(
|
|
$this->conf_appid,
|
|
$cp_orderid,
|
|
$xml->prepay_id)
|
|
));
|
|
}
|
|
}
|
|
|
|
private function genJSApiData($appid, $cp_orderid, $prepay_id)
|
|
{
|
|
$data = array(
|
|
'appId' => $appid,
|
|
'timeStamp' => time(),
|
|
'nonceStr' => md5($cp_orderid . uniqid()),
|
|
'package' => 'prepay_id=' . $prepay_id,
|
|
);
|
|
$data['paySign'] = phpcommon\wxPaySign($data,
|
|
$this->conf_pay_secret_key
|
|
);
|
|
$data['signType'] = 'MD5';
|
|
return json_encode($data);
|
|
}
|
|
|
|
public function payNotify()
|
|
{
|
|
$raw_data = file_get_contents('php://input');
|
|
error_log($_REQUEST);
|
|
error_log($raw_data);
|
|
$xml = new \SimpleXMLElement($raw_data);
|
|
if ($xml->return_code == 'SUCCESS') {
|
|
$this->spPayConfirm(
|
|
$xml->out_trade_no,
|
|
$xml->transaction_id,
|
|
$xml->total_fee
|
|
);
|
|
$data = array(
|
|
'return_code' => 'SUCCESS',
|
|
'return_msg' => 'OK'
|
|
);
|
|
echo phpcommon\objectToXml($params);
|
|
} else {
|
|
$data = array(
|
|
'return_code' => 'FAIL',
|
|
'return_msg' => '交易失败'
|
|
);
|
|
echo phpcommon\objectToXml($params);
|
|
}
|
|
}
|
|
|
|
}
|