123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- namespace App\Http\Controllers\Gateway;
- use App\Models\Payment;
- use Auth;
- use Illuminate\Http\JsonResponse;
- use Illuminate\Http\Request;
- use Response;
- use Illuminate\Support\Facades\Http;
- use Illuminate\Support\Facades\Log;
- class bypay extends AbstractPayment
- {
- public function purchase(Request $request): JsonResponse
- {
- $payment = $this->creatNewPayment(Auth::id(), $request->input('id'), $request->input('amount'));
-
- $key = "e7c359e9146345de86a41a099cacd67f";
- $data = [
- 'merchantId' => "1654045635701374976",
- 'merchantOrderId' => $payment->trade_no,
- 'payCode' => "wgzfbsm",
- 'amount' => $payment->amount,
- 'userLocalIp' => "192.178.31.34",
- 'notifyUrl' => route('invoice'),
- 'callbackUrl' => route('payment.notify', ['method' => 'bypay']),
- ];
-
- $str = $this->sortParams($data) . '&key=' . $key;
- Log::info("[BYPZ]请求支付接口". var_export($str, true));
-
- $StringC = strtolower($this->sha256($str));
- Log::info("[BYPZ]请求支付接口". var_export($StringC, true));
- $data['sign'] = strtoupper($StringC);
-
- Log::info("[BYPZ]请求支付接口". var_export($data, true));
- $postData = json_encode($data);
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, "https://bjzf88.vip/api/unifiedOrder/v2");
- curl_setopt($curl, CURLOPT_HEADER, 0);
- 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, $postData);
- curl_setopt($curl, CURLOPT_HTTPHEADER, array(
- 'Content-Type: application/json',
- 'Content-Length: ' . strlen($postData))
- );
-
- $res = curl_exec($curl);
- curl_close($curl);
- $result = json_decode($res, true);
- if (!$result) {
- return Response::json(['status' => 'success', 'url' => "", 'message' => '网络异常!']);
- }
- Log::info("[BYPZ]请求支付接口". var_export($result, true));
- if ($result["status"] != 200){
- return Response::json(['status' => 'fail', 'message' => '创建订单失败!'.$result['error']]);
- }
- return Response::json(['status' => 'success', 'url' => $result['data'], 'message' => '创建订单成功!']);
- }
- public function notify(Request $request): void
- {
- $postData = file_get_contents('php://input');
- $result = json_decode($postData,true);
- Log::info("[BYPZ]回调内容1". var_export($result, true));
- $notifyData = [
- 'merchantId'=>$result['merchantId'],
- 'merchantOrderId'=>$result['merchantOrderId'],
- 'orderId'=>$result['orderId'],
- 'amount'=>$result['amount'],
- 'factAmount'=>$result['factAmount'],
- 'ext'=>$result['ext'],
- 'state'=>$result['state'],
- 'sign'=>$result['sign']
- ];
- if ($this->verifySign($notifyData)){
- exit('fail');
- }
-
- Log::info("[BYPZ]回调内容". var_export($notifyData, true));
-
- if ($this->paymentReceived($notifyData['merchantOrderId']) ) {
- $this->addPamentCallback($notifyData['merchantOrderId'] ,$notifyData['orderId'] ,"");
- Log::info('【BYPZ】paymentReceived:'.var_export($request->all(), true));
- exit('success');
- }
- }
- function ASCII($params = array()){
-
- ksort($params);
-
- reset($params);
- $str = http_build_query($params);
- return $str;
- }
-
- function verifySign($params)
- {
- $key = "e7c359e9146345de86a41a099cacd67f";
- $sign = $params['sign'];
- unset($params['sign']);
- ksort($params);
- $str = $this->sortParams($params) . '&key='.$key;
- $StringC = strtolower($this->sha256($str));
- if ($sign !== strtoupper($StringC)) {
- return false;
- }
- return $str;
-
-
- }
- function sortParams($params) {
- ksort($params);
- $str = '';
- foreach ($params as $key => $value) {
- $str .= $key . '=' . $value . '&';
- }
-
- return rtrim($str, '&');
- }
- function sha256($str) {
- $hash = hash('sha256', $str, true);
- $encdeStr = bin2hex($hash);
- return strtolower($encdeStr);
- }
- }
|