AlipayConfig.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. /**
  3. *
  4. * example目录下为简单的支付样例,仅能用于搭建快速体验微信支付使用
  5. * 样例的作用仅限于指导如何使用sdk,在安全上面仅做了简单处理, 复制使用样例代码时请慎重
  6. * 请勿直接直接使用样例对外提供服务
  7. *
  8. **/
  9. namespace app\common\services\alipay\f2fpay\model;
  10. use app\common\exceptions\AppException;
  11. use app\common\helpers\Url;
  12. use Yunshop\StoreCashier\store\common\service\RefreshToken;
  13. use Yunshop\StoreCashier\store\models\StoreAlipaySetting;
  14. /**
  15. *
  16. * 该类需要业务自己继承, 该类只是作为deamon使用
  17. * 实际部署时,请务必保管自己的商户密钥,证书等
  18. *
  19. */
  20. class AlipayConfig
  21. {
  22. public $config;
  23. public $set;
  24. public function __construct()
  25. {
  26. if (request()->is_shop_pos) {
  27. $setting = \Setting::get('shop.pay');
  28. $this->set = $set = [
  29. 'app_id' => decrypt($setting['alipay_app_id']),
  30. 'alipay_public_key' => decrypt($setting['rsa_public_key']),
  31. 'merchant_private_key' => decrypt($setting['rsa_private_key']),
  32. ];
  33. } else {
  34. $this->set = $set = \Setting::get('shop.alipay_set');
  35. }
  36. $this->config = array (
  37. //签名方式,默认为RSA2(RSA2048)
  38. 'sign_type' => "RSA2",
  39. //支付宝公钥
  40. 'alipay_public_key' => $set['alipay_public_key'],
  41. //商户私钥
  42. 'merchant_private_key' => $set['merchant_private_key'],
  43. //编码格式
  44. 'charset' => "UTF-8",
  45. //支付宝网关
  46. 'gatewayUrl' => "https://openapi.alipay.com/gateway.do",
  47. //应用ID
  48. 'app_id' => $set['app_id'],
  49. //应用ID
  50. 'pid' => $set['pid'] ? : '',
  51. //应用ID
  52. 'name' => $set['name'] ? : '',
  53. //异步通知地址,只有扫码支付预下单可用
  54. 'notify_url' => "http://www.baidu.com",
  55. //最大查询重试次数
  56. 'MaxQueryRetry' => "12",
  57. //查询间隔
  58. 'QueryDuration' => "5"
  59. );
  60. }
  61. public function getConfig()
  62. {
  63. return $this->config;
  64. }
  65. public function getRoyalty()
  66. {
  67. $result = 0;
  68. if ($this->set['royalty']) {
  69. $sub_set = StoreAlipaySetting::where('store_id', request()->store_id)->first();
  70. if ($sub_set->royalty && !$sub_set->no_authorized_royalty) {
  71. $result = 1;
  72. }
  73. }
  74. return $result;
  75. }
  76. /**
  77. * @return mixed
  78. * @throws \Exception
  79. */
  80. public function getAuthToken()
  81. {
  82. $app_auth_token = '';
  83. if (!$this->set['app_type']) {
  84. $storeAlipaySetting = StoreAlipaySetting::uniacid()->where('store_id', request()->store_id)->first();
  85. if (!$storeAlipaySetting) {
  86. throw new AppException('门店未授权支付宝');
  87. }
  88. if ($storeAlipaySetting->expires_in < time()) {
  89. $storeAlipaySetting = RefreshToken::refreshToken();
  90. }
  91. $app_auth_token = $storeAlipaySetting->app_auth_token;
  92. }
  93. return $app_auth_token;
  94. }
  95. }