PaymentInterface.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Repositories\Service\Payment;
  4. use Illuminate\Support\Facades\Http;
  5. abstract class PaymentInterface
  6. {
  7. protected $_lastError = null;
  8. /**
  9. * 根据class,调用指定的实现类.
  10. * @param string $flag
  11. * @param string $payType
  12. */
  13. public static function factory($flag)
  14. {
  15. $className = self::_convertUnderline($flag);
  16. $className = "App\\Repositories\\Service\\Payment\\{$className}";
  17. if (class_exists($className)) {
  18. return $className::getInstance();
  19. } else {
  20. return new ErrorPayment();
  21. }
  22. }
  23. /**
  24. * 获取最后一次错误信息
  25. * @return
  26. */
  27. public function getLastError()
  28. {
  29. return $this->_lastError;
  30. }
  31. /**
  32. * @param array $data
  33. * @return string 成功时 返回支付链接、失败时返回空字符串 并给 lastError 赋值
  34. */
  35. abstract public function createPayLink($data = array());
  36. /**
  37. * 查单是否支付
  38. * @param array $data
  39. * @param string $orderSn
  40. * @param float $money
  41. * @param string $partnerId
  42. * @return bool
  43. */
  44. abstract public function queryOrder($data, &$orderSn, &$money, $partnerId);
  45. /**
  46. * @param string $orderSn 我方订单号
  47. * @param string $tradeNo 支付方单号
  48. * @param float $money 支付金额
  49. * @param string $partnerId 商户号
  50. * @return bool
  51. */
  52. abstract public function notify(&$orderSn, &$tradeNo, &$money, $partnerId);
  53. /**
  54. * 根据支付方要求输出指定内容
  55. */
  56. abstract public function stopNotify();
  57. /**
  58. * 发起http get 请求
  59. * @param $url
  60. * @return string
  61. */
  62. protected function httpGet($url, $data,)
  63. {
  64. try {
  65. $result = Http::withHeaders([
  66. 'timeout' => 20, // 超时,单位秒.
  67. 'verify' => false, // 是否校验SSL证书.
  68. 'http_errors' => false, // 如果跟随http异常头部是否中断.
  69. 'debug' => false, // 是否启用调试模式.
  70. 'allow_redirects' => true, // 不跟随301/302跳转.
  71. ])->get($url, $data);
  72. if ($result->successful()) {
  73. return $result->body();
  74. }
  75. } catch (\Exception $e) {
  76. return $e->getMessage();
  77. }
  78. return '';
  79. }
  80. /**
  81. * 发起 http post 请求
  82. * @param $url
  83. * @param $data
  84. * @param $formType 1.post;2.json;
  85. * @return string
  86. */
  87. protected function httpPost($url, $data, $formType = 1)
  88. {
  89. switch ($formType) {
  90. case 1:
  91. $asForm = 'asForm';
  92. break;
  93. case 2:
  94. $asForm = 'asJson';
  95. break;
  96. }
  97. try {
  98. $result = Http::withHeaders([
  99. 'timeout' => 20, // 超时,单位秒.
  100. 'verify' => false, // 是否校验SSL证书.
  101. 'http_errors' => false, // 如果跟随http异常头部是否中断.
  102. 'debug' => false, // 是否启用调试模式.
  103. 'allow_redirects' => true, // 不跟随301/302跳转.
  104. ])->$asForm()->post($url, $data);
  105. if ($result->successful()) {
  106. return $result->body();
  107. }
  108. } catch (\Exception $e) {
  109. logger($e->getMessage());
  110. }
  111. return '';
  112. }
  113. private static function _convertUnderline($str)
  114. {
  115. $string = '';
  116. $arr = explode('_', $str);
  117. foreach ($arr as $value) {
  118. $string .= ucfirst(strtolower($value));
  119. }
  120. return $string . 'Payment';
  121. }
  122. /**
  123. * 签名算法
  124. *
  125. * @param $data
  126. * @return string
  127. */
  128. public function getSign($data, $splicing = true)
  129. {
  130. ksort($data);
  131. $md5str = "";
  132. foreach ($data as $key => $val) {
  133. $md5str = $md5str . $key . "=" . $val . "&";
  134. }
  135. if ($splicing === false) {
  136. $md5str = substr($md5str, 0, -1);
  137. }
  138. return $md5str;
  139. }
  140. /**
  141. * 构建rsa私钥
  142. *
  143. * @param $privateKey
  144. * @return false|OpenSSLAsymmetricKey
  145. */
  146. public function setupPrvKeySign($privateKey)
  147. {
  148. $pem = chunk_split($privateKey, 64, "\n");
  149. $pem = "-----BEGIN RSA PRIVATE KEY-----\n" . $pem . "-----END RSA PRIVATE KEY-----\n";
  150. $key = openssl_pkey_get_private($pem);
  151. return $key;
  152. }
  153. /**
  154. * 构建rsa共钥
  155. *
  156. * @param $publicKey
  157. * @return false|OpenSSLAsymmetricKey
  158. */
  159. public function setupPubKey($publicKey)
  160. {
  161. $pem = chunk_split($publicKey, 64, "\n");
  162. $pem = "-----BEGIN PUBLIC KEY-----\n" . $pem . "-----END PUBLIC KEY-----";
  163. $key = openssl_pkey_get_public($pem);
  164. return $key;
  165. }
  166. }