| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- <?php
- declare(strict_types=1);
- namespace App\Repositories\Service\Payment;
- use Illuminate\Support\Facades\Http;
- abstract class PaymentInterface
- {
- protected $_lastError = null;
- /**
- * 根据class,调用指定的实现类.
- * @param string $flag
- * @param string $payType
- */
- public static function factory($flag)
- {
- $className = self::_convertUnderline($flag);
- $className = "App\\Repositories\\Service\\Payment\\{$className}";
- if (class_exists($className)) {
- return $className::getInstance();
- } else {
- return new ErrorPayment();
- }
- }
- /**
- * 获取最后一次错误信息
- * @return
- */
- public function getLastError()
- {
- return $this->_lastError;
- }
- /**
- * @param array $data
- * @return string 成功时 返回支付链接、失败时返回空字符串 并给 lastError 赋值
- */
- abstract public function createPayLink($data = array());
- /**
- * 查单是否支付
- * @param array $data
- * @param string $orderSn
- * @param float $money
- * @param string $partnerId
- * @return bool
- */
- abstract public function queryOrder($data, &$orderSn, &$money, $partnerId);
- /**
- * @param string $orderSn 我方订单号
- * @param string $tradeNo 支付方单号
- * @param float $money 支付金额
- * @param string $partnerId 商户号
- * @return bool
- */
- abstract public function notify(&$orderSn, &$tradeNo, &$money, $partnerId);
- /**
- * 根据支付方要求输出指定内容
- */
- abstract public function stopNotify();
- /**
- * 发起http get 请求
- * @param $url
- * @return string
- */
- protected function httpGet($url, $data,)
- {
- try {
- $result = Http::withHeaders([
- 'timeout' => 20, // 超时,单位秒.
- 'verify' => false, // 是否校验SSL证书.
- 'http_errors' => false, // 如果跟随http异常头部是否中断.
- 'debug' => false, // 是否启用调试模式.
- 'allow_redirects' => true, // 不跟随301/302跳转.
- ])->get($url, $data);
- if ($result->successful()) {
- return $result->body();
- }
- } catch (\Exception $e) {
- return $e->getMessage();
- }
- return '';
- }
- /**
- * 发起 http post 请求
- * @param $url
- * @param $data
- * @param $formType 1.post;2.json;
- * @return string
- */
- protected function httpPost($url, $data, $formType = 1)
- {
- switch ($formType) {
- case 1:
- $asForm = 'asForm';
- break;
- case 2:
- $asForm = 'asJson';
- break;
- }
- try {
- $result = Http::withHeaders([
- 'timeout' => 20, // 超时,单位秒.
- 'verify' => false, // 是否校验SSL证书.
- 'http_errors' => false, // 如果跟随http异常头部是否中断.
- 'debug' => false, // 是否启用调试模式.
- 'allow_redirects' => true, // 不跟随301/302跳转.
- ])->$asForm()->post($url, $data);
- if ($result->successful()) {
- return $result->body();
- }
- } catch (\Exception $e) {
- logger($e->getMessage());
- }
- return '';
- }
- private static function _convertUnderline($str)
- {
- $string = '';
- $arr = explode('_', $str);
- foreach ($arr as $value) {
- $string .= ucfirst(strtolower($value));
- }
- return $string . 'Payment';
- }
- /**
- * 签名算法
- *
- * @param $data
- * @return string
- */
- public function getSign($data, $splicing = true)
- {
- ksort($data);
- $md5str = "";
- foreach ($data as $key => $val) {
- $md5str = $md5str . $key . "=" . $val . "&";
- }
- if ($splicing === false) {
- $md5str = substr($md5str, 0, -1);
- }
- return $md5str;
- }
- /**
- * 构建rsa私钥
- *
- * @param $privateKey
- * @return false|OpenSSLAsymmetricKey
- */
- public function setupPrvKeySign($privateKey)
- {
- $pem = chunk_split($privateKey, 64, "\n");
- $pem = "-----BEGIN RSA PRIVATE KEY-----\n" . $pem . "-----END RSA PRIVATE KEY-----\n";
- $key = openssl_pkey_get_private($pem);
- return $key;
- }
- /**
- * 构建rsa共钥
- *
- * @param $publicKey
- * @return false|OpenSSLAsymmetricKey
- */
- public function setupPubKey($publicKey)
- {
- $pem = chunk_split($publicKey, 64, "\n");
- $pem = "-----BEGIN PUBLIC KEY-----\n" . $pem . "-----END PUBLIC KEY-----";
- $key = openssl_pkey_get_public($pem);
- return $key;
- }
- }
|