| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213 |
- <?php
- namespace common\components;
- use common\components\payment\BalancePay;
- use common\components\payment\SandPayFast;
- use common\components\payment\ScorePay;
- use Yii;
- use yii\base\Component;
- use common\components\payment\AliPay;
- use common\components\payment\HuijuBill;
- use common\components\payment\UnionPay;
- use common\components\payment\WechatPay;
- use common\components\payment\JoinPay;
- use common\components\payment\JoinPayFastPay;
- use common\helpers\ArrayHelper;
- use common\logic\center\Center;
- /**
- * 支付组件
- *
- * Class Pay
- * @package common\components
- * @property \common\components\payment\WechatPay $wechat
- * @property \common\components\payment\BalancePay $balance
- * @property \common\components\payment\ScorePay $score
- * @property \common\components\payment\AliPay $alipay
- * @property \common\components\payment\UnionPay $union
- * @property \common\components\payment\JoinPay $joinpay
- * @property \common\components\payment\JoinPayFastPay $joinpay_fastpay
- */
- class Payment extends Component
- {
- /**
- * 公用配置
- *
- * @var
- */
- protected $platformConfig;
- public function init()
- {
- $config = Yii::$app->services->setting->platform->get('pay', true);
- foreach ($config as $key => $value) {
- $config[$key] = json_decode($value, true);
- }
- // 默认读后台配置可切换为根据商户来获取配置
- $this->platformConfig = $config ?? [];
- parent::init();
- }
- /**
- * 支付宝支付
- *
- * @param array $config
- * @return AliPay
- * @throws
- */
- public function alipay(array $config = [])
- {
- $config = $config ?: $this->platformConfig['alipay'];
- return new AliPay(
- [
- 'app_id' => $config['alipay_app_id'],
- 'notify_url' => $config['notify_url'],
- 'return_url' => $config['return_url'],
- 'ali_public_key' => $config['alipay_public_key'],
- // 加密方式: ** RSA2 **
- 'private_key' => $config['alipay_private_key'],
- 'sandbox' => false,
- 'is_use_cert' => !empty($config['is_use_cert']) ? $config['is_use_cert'] : false,
- 'alipay_public_cert'=> $config['alipay_public_cert'] ?? '',
- 'app_cert' => $config['app_cert'] ?? '',
- 'alipay_root_cert' => $config['alipay_root_cert'] ?? '',
- ]
- );
- }
- /**
- * 微信支付
- *
- * @param array $config
- * @return WechatPay
- */
- public function wechat(array $config = [])
- {
- $config = $config ?: $this->platformConfig['wechat'];
- return new WechatPay([
- 'app_id' => $config['app_id'], // 公众号 APPID
- 'open_app_id' => $config['wechat_open_app_id'], // 微信开放平台 APPID
- 'mini_program_app_id' => $config['mini_program_app_id'], // 微信小程序 APPID
- 'mch_id' => $config['wechat_mch_id'],
- 'api_key' => $config['wechat_pay_secret'],
- 'cert_client' => dirname(Yii::$app->basePath).'/'.$config['wechat_cert_pem_file'], // optional,退款等情况时用到
- 'cert_key' => dirname(Yii::$app->basePath).'/'.$config['wechat_key_pem_file'],// optional,退款等情况时用到
- ]);
- }
- /**
- * 银联支付
- *
- * @param array $config
- * @return UnionPay
- * @throws \
- */
- public function union(array $config = [])
- {
- $config = $config ?: $this->platformConfig['union'];
- return new UnionPay([
- 'mch_id' => $config['union_mchid'],
- 'notify_url' => '',
- 'return_url' => '',
- 'cert_id' => $config['union_cert_id'],
- 'private_key' => $config['union_private_key'],
- ]);
- }
- /**
- * 汇聚聚合支付
- * @param array $config
- * @return JoinPay
- */
- public function joinpay(array $config = [])
- {
- $config = $config ?: $this->platformConfig['joinpay'];
- return new JoinPay(
- [
- 'merchant_no' => $config['merchant_no'] ?? '',
- 'merchant_secret' => $config['merchant_secret'] ?? '',
- // 'trade_merchant_no' => $config['trade_merchant_no'] ?? '',
- // 'wechat_app_id' => $config['wechat_app_id'] ?? '',
- 'wechat_gzh_trade_merchant_no' => $config['wechat_gzh_trade_merchant_no'] ?? '',
- 'wechat_gzh_appid' => $config['wechat_gzh_appid'] ?? '',
- 'wechat_xcx_trade_merchant_no' => $config['wechat_xcx_trade_merchant_no'] ?? '',
- 'wechat_xcx_appid' => $config['wechat_xcx_appid'] ?? '' ,
- 'alipay_trade_merchant_no' => $config['alipay_trade_merchant_no'] ?? '',
- 'wechat_app3_trade_merchant_no' => $config['app3_trade_merchant_no'] ?? '',
- 'unionpay_trade_merchant_no' => $config['unionpay_trade_merchant_no'] ?? '',
- 'notify_url' => '',
- 'return_url' => '',
- ]
- );
- }
- /**
- * 汇聚支付- 快捷银行卡支付
- * @param array $config
- * @return JoinPayFastPay
- */
- public function joinpayFastPay(array $config = [])
- {
- $config = $config ?: $this->platformConfig['joinpay_fast'];
- return new JoinPayFastPay(
- [
- 'joinpay_merchant_no' => $config['merchant_no'] ?? '',
- 'joinpay_fast_pay_public_key' => $config['fast_pay_public_key'] ?? '',
- 'joinpay_fast_pay_private_key' => $config['fast_pay_private_key'] ?? '',
- 'notify_url' => '',
- 'return_url' => '',
- ]
- );
- }
- public function sandpayFastPay(array $config)
- {
- $config = $config ?: $this->platformConfig['sandpay_fast'];
- return new SandPayFast($config);
- }
- /**
- * 汇聚分账支付
- * @param array $config
- * @return HuijuBill
- */
- public function huijuBill(array $config = [])
- {
- $config = $config ?: $this->platformConfig['wechat'];
- return new HuijuBill([
- 'merchant_no' => $config['merchant_no']
- ]);
- }
- public function balance(){
- return new BalancePay();
- }
- public function score(){
- return new ScorePay();
- }
- /**
- * @param $name
- * @return mixed
- * @throws \Exception
- */
- public function __get($name)
- {
- try {
- return parent::__get($name);
- } catch (\Exception $e) {
- if ($this->$name()) {
- return $this->$name([]);
- } else {
- throw $e->getPrevious();
- }
- }
- }
- }
|