File: /www/wwwroot/cece.umchkw.com/1.php
<?php
/**
* 经典模式
* 技术联系人 邵亚男 18721792163 微信同号
* 文档地址 http://docs.melogoldpay.net/payment-en.html#payment-api
*
*/;
//TODO 请仔细查看TODO的注释 请仔细查看TODO的注释 请仔细查看TODO的注释
$isTest = 1; //是否是测试。1是。0否
$sandbox_url = 'http://pay.melogoldpay.net/'; //测试地址
$live_url = 'https://pay.melogoldpay.com/'; //正式地址
//秘钥 测试地址请用测试秘钥 正式地址用正式秘钥 请登录商户后台查看
$sandbox_key = '987aa9a72122611aaf15c991f2113a47b49efabd761a619b1ff49002d155b344'; //TODO 测试秘钥 商户后台查看
$live_key = 'd576824a6bd2712795bb20155429f7f4d1477937e47a129d75287a2f72e5b322'; //TODO 正式秘钥 商户后台查看(必须材料通过以后才能使用)
$trade_email = 'tech@test.com';//商户邮箱
$order = 'order' . rand(1000, 9999);//订单号
$amount = $_GET['amount'];
if(!isset($amount) || empty($amount))
{
return '金额不能为空';
exit;
}
//TODO 如果浏览器有缓存请在地址后面加上相关的随机数
//支付参数
$currency = 'EUR';
$form = [
'sandbox' => 0,//固定值
'request_time' => time(),
'request_type' => 3,//1from,3直连。
'trade_email' => $trade_email,//商户号
'order_id' => $order,
'amount' => $amount,
'currency' => $currency,
'3ds_mode' => 1,
'pay_method' => 'X02',
'notify_url' => getUrlAction() . '/notify.php?order_id=' . $order,//异步通知页面
'callback_url' => getUrlAction(),//重定向到结果页面
'customer' => [
'email' => rand(100000, 999999) . '@qq.com'
]
];
$url = $isTest ? $sandbox_url : $live_url;
$key = $live_key;
//TODO 加密签名字段
$sign = $key . $form['request_time'] . $form['trade_email'] . $form['order_id'] . $form['pay_method'] . $form['amount'] . $form['currency'];
$form['sign'] = hash('sha256', $sign);
echo date("Y-m-d H:i:s", time()) . '<br />';
echo $sign . '<br />';
echo $form['sign'] . '<br />';
try {
file_put_contents('request.log', var_export($url, true) . PHP_EOL, FILE_APPEND);
file_put_contents('request.log', var_export($form, true) . PHP_EOL, FILE_APPEND);
$post_url = $url . 'gateway/payment/transaction';
echo $post_url . '<br />';
$data = curl_request($post_url, 'post', http_build_query($form), $header = []);
$parseData = json_decode($data, true);
file_put_contents('request.log', var_export($data, true) . PHP_EOL, FILE_APPEND);
file_put_contents('request.log', var_export($parseData, true) . PHP_EOL, FILE_APPEND);
//TODO 当处理完毕后,重定向到支付页面
if ($parseData['code'] != 200) {
echo $parseData['message'];
die;
} else {
header('HTTP/1.1 301 Moved Permanently');
header('Location: ' . $parseData['pay_url']);
}
} catch (Exception $e) {
echo "<pre>";
print_r($e->getMessage());
echo "</pre>";
die;
}
function curl_request($url, $method = 'get', $data = null, $https = true)
{
//1.初识化curl
$ch = curl_init($url);
//2.根据实际请求需求进行参数封装
//返回数据不直接输出
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//如果是https请求
if ($https === true) {
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
}
//如果是post请求
if ($method === 'post') {
//开启发送post请求选项
curl_setopt($ch, CURLOPT_POST, true);
//发送post的数据
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
}
//3.发送请求
$result = curl_exec($ch);
if ($result === false) {
return curl_error($ch);
}
//4.返回返回值,关闭连接
curl_close($ch);
return $result;
}
function getUrlAction()
{
$scheme = 'https';
$host = $_SERVER['HTTP_HOST'];
if ($_SERVER['HTTP_HOST'] == 'pay.paymole.test') {
$scheme = 'http';
$host = "pay.paymole.test";
} else if (strpos($_SERVER['HTTP_HOST'], "pay.melogoldpay.net") !== false) {
$scheme = 'http';
$host = "pay.melogoldpay.net";
}
return $scheme . '://' . $host;
}