Payment.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace common\components;
  3. use common\components\payment\BalancePay;
  4. use common\components\payment\SandPayFast;
  5. use common\components\payment\ScorePay;
  6. use Yii;
  7. use yii\base\Component;
  8. use common\components\payment\AliPay;
  9. use common\components\payment\HuijuBill;
  10. use common\components\payment\UnionPay;
  11. use common\components\payment\WechatPay;
  12. use common\components\payment\JoinPay;
  13. use common\components\payment\JoinPayFastPay;
  14. use common\helpers\ArrayHelper;
  15. use common\logic\center\Center;
  16. /**
  17. * 支付组件
  18. *
  19. * Class Pay
  20. * @package common\components
  21. * @property \common\components\payment\WechatPay $wechat
  22. * @property \common\components\payment\BalancePay $balance
  23. * @property \common\components\payment\ScorePay $score
  24. * @property \common\components\payment\AliPay $alipay
  25. * @property \common\components\payment\UnionPay $union
  26. * @property \common\components\payment\JoinPay $joinpay
  27. * @property \common\components\payment\JoinPayFastPay $joinpay_fastpay
  28. */
  29. class Payment extends Component
  30. {
  31. /**
  32. * 公用配置
  33. *
  34. * @var
  35. */
  36. protected $platformConfig;
  37. public function init()
  38. {
  39. $config = Yii::$app->services->setting->platform->get('pay', true);
  40. foreach ($config as $key => $value) {
  41. $config[$key] = json_decode($value, true);
  42. }
  43. // 默认读后台配置可切换为根据商户来获取配置
  44. $this->platformConfig = $config ?? [];
  45. parent::init();
  46. }
  47. /**
  48. * 支付宝支付
  49. *
  50. * @param array $config
  51. * @return AliPay
  52. * @throws
  53. */
  54. public function alipay(array $config = [])
  55. {
  56. $config = $config ?: $this->platformConfig['alipay'];
  57. return new AliPay(
  58. [
  59. 'app_id' => $config['alipay_app_id'],
  60. 'notify_url' => $config['notify_url'],
  61. 'return_url' => $config['return_url'],
  62. 'ali_public_key' => $config['alipay_public_key'],
  63. // 加密方式: ** RSA2 **
  64. 'private_key' => $config['alipay_private_key'],
  65. 'sandbox' => false,
  66. 'is_use_cert' => !empty($config['is_use_cert']) ? $config['is_use_cert'] : false,
  67. 'alipay_public_cert'=> $config['alipay_public_cert'] ?? '',
  68. 'app_cert' => $config['app_cert'] ?? '',
  69. 'alipay_root_cert' => $config['alipay_root_cert'] ?? '',
  70. ]
  71. );
  72. }
  73. /**
  74. * 微信支付
  75. *
  76. * @param array $config
  77. * @return WechatPay
  78. */
  79. public function wechat(array $config = [])
  80. {
  81. $config = $config ?: $this->platformConfig['wechat'];
  82. return new WechatPay([
  83. 'app_id' => $config['app_id'], // 公众号 APPID
  84. 'open_app_id' => $config['wechat_open_app_id'], // 微信开放平台 APPID
  85. 'mini_program_app_id' => $config['mini_program_app_id'], // 微信小程序 APPID
  86. 'mch_id' => $config['wechat_mch_id'],
  87. 'api_key' => $config['wechat_pay_secret'],
  88. 'cert_client' => dirname(Yii::$app->basePath).'/'.$config['wechat_cert_pem_file'], // optional,退款等情况时用到
  89. 'cert_key' => dirname(Yii::$app->basePath).'/'.$config['wechat_key_pem_file'],// optional,退款等情况时用到
  90. ]);
  91. }
  92. /**
  93. * 银联支付
  94. *
  95. * @param array $config
  96. * @return UnionPay
  97. * @throws \
  98. */
  99. public function union(array $config = [])
  100. {
  101. $config = $config ?: $this->platformConfig['union'];
  102. return new UnionPay([
  103. 'mch_id' => $config['union_mchid'],
  104. 'notify_url' => '',
  105. 'return_url' => '',
  106. 'cert_id' => $config['union_cert_id'],
  107. 'private_key' => $config['union_private_key'],
  108. ]);
  109. }
  110. /**
  111. * 汇聚聚合支付
  112. * @param array $config
  113. * @return JoinPay
  114. */
  115. public function joinpay(array $config = [])
  116. {
  117. $config = $config ?: $this->platformConfig['joinpay'];
  118. return new JoinPay(
  119. [
  120. 'merchant_no' => $config['merchant_no'] ?? '',
  121. 'merchant_secret' => $config['merchant_secret'] ?? '',
  122. // 'trade_merchant_no' => $config['trade_merchant_no'] ?? '',
  123. // 'wechat_app_id' => $config['wechat_app_id'] ?? '',
  124. 'wechat_gzh_trade_merchant_no' => $config['wechat_gzh_trade_merchant_no'] ?? '',
  125. 'wechat_gzh_appid' => $config['wechat_gzh_appid'] ?? '',
  126. 'wechat_xcx_trade_merchant_no' => $config['wechat_xcx_trade_merchant_no'] ?? '',
  127. 'wechat_xcx_appid' => $config['wechat_xcx_appid'] ?? '' ,
  128. 'alipay_trade_merchant_no' => $config['alipay_trade_merchant_no'] ?? '',
  129. 'wechat_app3_trade_merchant_no' => $config['app3_trade_merchant_no'] ?? '',
  130. 'unionpay_trade_merchant_no' => $config['unionpay_trade_merchant_no'] ?? '',
  131. 'notify_url' => '',
  132. 'return_url' => '',
  133. ]
  134. );
  135. }
  136. /**
  137. * 汇聚支付- 快捷银行卡支付
  138. * @param array $config
  139. * @return JoinPayFastPay
  140. */
  141. public function joinpayFastPay(array $config = [])
  142. {
  143. $config = $config ?: $this->platformConfig['joinpay_fast'];
  144. return new JoinPayFastPay(
  145. [
  146. 'joinpay_merchant_no' => $config['merchant_no'] ?? '',
  147. 'joinpay_fast_pay_public_key' => $config['fast_pay_public_key'] ?? '',
  148. 'joinpay_fast_pay_private_key' => $config['fast_pay_private_key'] ?? '',
  149. 'notify_url' => '',
  150. 'return_url' => '',
  151. ]
  152. );
  153. }
  154. public function sandpayFastPay(array $config)
  155. {
  156. $config = $config ?: $this->platformConfig['sandpay_fast'];
  157. return new SandPayFast($config);
  158. }
  159. /**
  160. * 汇聚分账支付
  161. * @param array $config
  162. * @return HuijuBill
  163. */
  164. public function huijuBill(array $config = [])
  165. {
  166. $config = $config ?: $this->platformConfig['wechat'];
  167. return new HuijuBill([
  168. 'merchant_no' => $config['merchant_no']
  169. ]);
  170. }
  171. public function balance(){
  172. return new BalancePay();
  173. }
  174. public function score(){
  175. return new ScorePay();
  176. }
  177. /**
  178. * @param $name
  179. * @return mixed
  180. * @throws \Exception
  181. */
  182. public function __get($name)
  183. {
  184. try {
  185. return parent::__get($name);
  186. } catch (\Exception $e) {
  187. if ($this->$name()) {
  188. return $this->$name([]);
  189. } else {
  190. throw $e->getPrevious();
  191. }
  192. }
  193. }
  194. }