diff --git a/app/Services/Gateway/MalioPay.php b/app/Services/Gateway/MalioPay.php index c3b7c90a..558d6494 100644 --- a/app/Services/Gateway/MalioPay.php +++ b/app/Services/Gateway/MalioPay.php @@ -118,6 +118,23 @@ class MalioPay extends AbstractPayment ); } return json_encode($return); + case ('materialpay'): + $materialpay = new MaterialPay(Config::get('materialpay_secret')); + $result = $materialpay->purchase_maliopay($type, $price); + if ($result['errcode'] == 0) { + $return = array( + 'ret' => 1, + 'type' => 'url', + 'tradeno' => $result['tradeno'], + 'url' => $result['url'] + ); + } else { + $return = array( + 'ret' => 0, + 'msg' => $result['errmsg'] + ); + } + return json_encode($return); } } else if ($type == 'wechat') { $payment_system = MalioConfig::get('mups_wechat'); @@ -173,6 +190,23 @@ class MalioPay extends AbstractPayment ); } return json_encode($return); + case ('materialpay'): + $materialpay = new MaterialPay(Config::get('materialpay_secret')); + $result = $materialpay->purchase_maliopay($type, $price); + if ($result['errcode'] == 0) { + $return = array( + 'ret' => 1, + 'type' => 'qrcode', + 'tradeno' => $result['tradeno'], + 'url' => $result['url'] + ); + } else { + $return = array( + 'ret' => 0, + 'msg' => $result['errmsg'] + ); + } + return json_encode($return); } } } @@ -358,6 +392,33 @@ class MalioPay extends AbstractPayment echo '錯誤'; } } + case ('materialpay'): + $data = array(); + $data['tradeStatus'] = $request->getParam('tradeStatus'); + $data['payAmount'] = number_format($request->getParam('payAmount'),2); + $data['tradeNo'] = $request->getParam('tradeNo'); + $data['payType'] = $request->getParam('payType'); + $data['outTradeNo'] = $request->getParam('outTradeNo'); + + $pl = Paylist::where('tradeno',$data['outTradeNo'])->first(); + if ($pl->status == 1) { + echo 'success'; + return; + } + + $materialpay = new MaterialPay(Config::get('materialpay_secret')); + // 准备待签名数据 + $str_to_sign = $materialpay->prepareSign($data); + // 验证签名 + $resultVerify = $materialpay->verify($str_to_sign, $request->getParam('sign')); + if ($resultVerify) { + $materialpay->postPaymentMaliopay($data['outTradeNo'], 'MaterialPay - ' . $data['outTradeNo']); + echo 'success'; + return; + } else { + echo 'fail'; + return; + } default: return 'failed'; } diff --git a/app/Services/Gateway/MaterialPay.php b/app/Services/Gateway/MaterialPay.php new file mode 100644 index 00000000..2f9188df --- /dev/null +++ b/app/Services/Gateway/MaterialPay.php @@ -0,0 +1,202 @@ +appSecret = $appSecret; + $this->gatewayUri = 'https://www.materialpay.com/api/payment/'; + } + + public function prepareSign($data) + { + ksort($data); + return http_build_query($data); + } + + public function sign($data) + { + return strtolower(md5(md5($data) . $this->appSecret)); + } + + public function verify($data, $signature) + { + $mySign = $this->sign($data); + return $mySign === $signature; + } + + public function post($data, $type = 'create') + { + if ($type == 'create') { + $this->gatewayUri .= 'create'; + } else { + $this->gatewayUri .= 'query'; + } + + $headerArray = array("Content-type:application/json;charset='utf-8'","Accept:application/json"); + + $curl = curl_init(); + curl_setopt($curl, CURLOPT_URL, $this->gatewayUri); + curl_setopt($curl, CURLOPT_HTTPHEADER,$headerArray); + curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); + curl_setopt($curl, CURLOPT_POST, 1); + curl_setopt($curl, CURLOPT_POSTFIELDS, json_encode($data)); + $data = curl_exec($curl); + curl_close($curl); + return $data; + } + + public function purchase($request, $response, $args) + { + $price = $request->getParam('price'); + $type = $request->getParam('type'); + + if ($price <= 0) { + return json_encode(['code' => -1, 'errmsg' => '非法的金额.']); + } + $user = Auth::getUser(); + $pl = new Paylist(); + $pl->userid = $user->id; + $pl->total = $price; + $pl->tradeno = self::generateGuid(); + $pl->save(); + + $data['appId'] = Config::get('materialpay_appid'); + $data['outTradeNo'] = $pl->tradeno; + $data['payAmount'] = (float)$price; + $data['payType'] = $type; + $params = $this->prepareSign($data); + $data['sign'] = $this->sign($params); + $data['returnUrl'] = Config::get('baseUrl') . '/user/code'; + switch ($type) { + case ('WXPAY'): + $result = json_decode($this->post($data), true); + break; + default: + $result = json_decode($this->post($data), true); + } + $result['pid'] = $pl->tradeno; + return json_encode($result); + } + + public function purchase_maliopay($type, $price) + { + if ($price <= 0) { + return json_encode(['code' => -1, 'errmsg' => '非法的金额.']); + } + if ($type == 'wechat') { + $type = 'WXPAY'; + } + if ($type == 'alipay') { + $type = 'ALIPAY_WEB'; + } + $user = Auth::getUser(); + $pl = new Paylist(); + $pl->userid = $user->id; + $pl->total = $price; + $pl->tradeno = self::generateGuid(); + $pl->save(); + + $data['appId'] = Config::get('materialpay_appid'); + $data['outTradeNo'] = $pl->tradeno; + $data['payAmount'] = (float)$price; + $data['payType'] = $type; + $params = $this->prepareSign($data); + $data['sign'] = $this->sign($params); + $data['returnUrl'] = Config::get('baseUrl') . '/user/code'; + switch ($type) { + case ('WXPAY'): + $result = json_decode($this->post($data), true); + break; + default: + $result = json_decode($this->post($data), true); + } + $result['pid'] = $pl->tradeno; + return array('url' => $result['result']['url'], 'errcode' => 0, 'tradeno' => $pl->tradeno); + } + + public function query($tradeNo) + { + $data['appId'] = Config::get('materialpay_appid'); + $data['outTradeNo'] = $tradeNo; + $params = $this->prepareSign($data); + $data['sign'] = $this->sign($params); + return json_decode($this->post($data, $type = 'query'), true); + } + + public function notify($request, $response, $args) + { + $data = array(); + $data['tradeStatus'] = $request->getParam('tradeStatus'); + $data['payAmount'] = number_format($request->getParam('payAmount'),2); + $data['tradeNo'] = $request->getParam('tradeNo'); + $data['payType'] = $request->getParam('payType'); + $data['outTradeNo'] = $request->getParam('outTradeNo'); + + // 准备待签名数据 + $str_to_sign = $this->prepareSign($data); + // 验证签名 + $resultVerify = $this->verify($str_to_sign, $request->getParam('sign')); + if ($resultVerify) { + $this->postPayment($data['outTradeNo'], 'MaterialPay - ' . $data['outTradeNo']); + echo 'success'; + } else { + echo 'fail'; + } + } + + public function getPurchaseHTML() + { + return View::getSmarty()->fetch('user/materialpay.tpl'); + } + + public function getReturnHTML($request, $response, $args) + { + $pid = $_GET['outTradeNo']; + $p = Paylist::where('tradeno', '=', $pid)->first(); + $money = $p->total/1000; + if ($p->status == 1) { + $success = 1; + } else { + $data = array(); + $data['tradeStatus'] = $request->getParam('tradeStatus'); + $data['payAmount'] = number_format($request->getParam('payAmount'),2); + $data['tradeNo'] = $request->getParam('tradeNo'); + $data['payType'] = $request->getParam('payType'); + $data['outTradeNo'] = $request->getParam('outTradeNo'); + // 准备待签名数据 + $str_to_sign = $this->prepareSign($data); + // 验证签名 + $resultVerify = $this->verify($str_to_sign, $request->getParam('sign')); + if ($resultVerify) { + $this->postPayment($data['outTradeNo'], 'MaterialPay'); + $success = 1; + } else { + $success = 0; + } + } + return View::getSmarty()->assign('money', $money)->assign('success', $success)->fetch('user/pay_success.tpl'); + } + + public function getStatus($request, $response, $args) + { + $return = []; + $p = Paylist::where('tradeno', $_POST['pid'])->first(); + $return['ret'] = 1; + $return['result'] = $p->status; + return json_encode($return); + } +} diff --git a/config/.config.example.php b/config/.config.example.php index 850e2895..0b68ef5c 100644 --- a/config/.config.example.php +++ b/config/.config.example.php @@ -300,6 +300,10 @@ $_ENV['zfbjk_qrcodeurl']=''; # 客服和技术 24x7 在线支持: https://t.me/joinchat/GLKSKhUnE4GvEAPgqtChAQ $_ENV['bitpay_secret'] = ''; +# materialpay +$_ENV['materialpay_appid']=''; //AppID +$_ENV['materialpay_secret']=''; //AppSecret + #PayJs $_ENV['payjs_mchid'] = ''; diff --git a/config/.malio_config.example.php b/config/.malio_config.example.php index 912d9e96..25118571 100644 --- a/config/.malio_config.example.php +++ b/config/.malio_config.example.php @@ -40,9 +40,9 @@ $Malio_Config['only_one_lang'] = 'none'; // 强制使用某种语言,支持 ####### 支付系统 ####### // 支付系统写 none 意味着不启用此收款方式 -// 支付宝目前支持 bitpayx | stripe | tomatopay | f2fpay | wolfpay +// 支付宝目前支持 bitpayx | stripe | tomatopay | f2fpay | wolfpay | materialpay $Malio_Config['mups_alipay'] = 'bitpayx'; // Malio 聚合支付系统里面的 支付宝 要用的支付平台 -// 微信支付目前支持 bitpayx | stripe +// 微信支付目前支持 bitpayx | stripe | materialpay $Malio_Config['mups_wechat'] = 'bitpayx'; // Malio 聚合支付系统里面的 微信支付 要用的支付平台 $Malio_Config['mups_minimum_amount'] = 0; // Malio 聚合支付系统限制最低的充值金额