|
|
@@ -0,0 +1,97 @@
|
|
|
+<?php
|
|
|
+
|
|
|
+namespace App\Services\Payment;
|
|
|
+
|
|
|
+use GuzzleHttp\Client;
|
|
|
+use GuzzleHttp\Exception\RequestException;
|
|
|
+use GuzzleHttp\Psr7\Response;
|
|
|
+use App\Services\BaseService;
|
|
|
+
|
|
|
+class SanJinService extends BaseService
|
|
|
+{
|
|
|
+ const REQUEST_URL = 'https://jkapi-sanjin.jkccb.com/';
|
|
|
+
|
|
|
+ const PRODUCT_TEST = 'T888'; // 测试支付通道
|
|
|
+
|
|
|
+ // 获取商户ID
|
|
|
+ public static function getMerchantId()
|
|
|
+ {
|
|
|
+ return config('app.tree_pay_mch_id');
|
|
|
+ }
|
|
|
+ // 获取商户秘钥
|
|
|
+ public static function getSecret()
|
|
|
+ {
|
|
|
+ return config('app.tree_pay_key');
|
|
|
+ }
|
|
|
+
|
|
|
+ // 获取异步的通知地址
|
|
|
+ public static function getNotifyUrl()
|
|
|
+ {
|
|
|
+ return 'https://botpc28.testx2.cc/api/pay/harvest';
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ /**
|
|
|
+ * @description: 获取请求客户端
|
|
|
+ * @return {*}
|
|
|
+ */
|
|
|
+ public static function getClient(): Client
|
|
|
+ {
|
|
|
+ return new Client([
|
|
|
+ 'base_uri' => self::REQUEST_URL,
|
|
|
+ 'timeout' => 5.0,
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 签名
|
|
|
+ public static function signature($params = [],$must = [])
|
|
|
+ {
|
|
|
+ if($must){
|
|
|
+ foreach($params as $k => $v){
|
|
|
+ if(!in_array($k,$must)){
|
|
|
+ unset($params[$k]);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ ksort($params, SORT_STRING);
|
|
|
+ $parts = [];
|
|
|
+ foreach($params as $k => $v){
|
|
|
+ array_push($parts,$k.'='.$v);
|
|
|
+ }
|
|
|
+ $mch_key = self::getSecret();
|
|
|
+ $parts[] = "key=".$mch_key;
|
|
|
+ $sign = md5(implode('&',$parts));
|
|
|
+ return $sign;
|
|
|
+ }
|
|
|
+
|
|
|
+ public static function pay($amount,$orderNo,$type = self::PRODUCT_TEST)
|
|
|
+ {
|
|
|
+
|
|
|
+ $must = ['mchId','productId','outTradeNo','amount','reqTime','notifyUrl'];
|
|
|
+ $mch_id = self::getMerchantId();
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ $data = [];
|
|
|
+ $data['mchId'] = $mch_id;
|
|
|
+ $data['amount'] = $amount;
|
|
|
+ $data['outTradeNo'] = $orderNo;
|
|
|
+ $data['notifyUrl'] = self::getNotifyUrl();
|
|
|
+ $data['reqTime'] = microtime(true) * 1000;
|
|
|
+ $data['productId'] = $type;
|
|
|
+
|
|
|
+
|
|
|
+ $data['sign'] = self::signature($data,$must);
|
|
|
+ $client = self::getClient();
|
|
|
+ $response = $client->post('api/pay', [
|
|
|
+ 'json' => $data,
|
|
|
+ 'headers' => [
|
|
|
+ 'Content-Type' => 'application/json',
|
|
|
+ ]
|
|
|
+ ]);
|
|
|
+
|
|
|
+ $body = $response->getBody();
|
|
|
+ return json_decode($body->getContents(), true);
|
|
|
+ }
|
|
|
+}
|