PaymentInterface.php 5.7 KB

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