AliPay.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace app\common\library;
  3. use Alipay\EasySDK\Kernel\Factory;
  4. use Alipay\EasySDK\Kernel\Util\ResponseChecker;
  5. use app\common\model\Config;
  6. use think\Controller;
  7. class AliPay extends Controller
  8. {
  9. /*
  10. 配置参数
  11. */
  12. public function __construct()
  13. {
  14. // $this->appId = "2021003130678750";
  15. // $this->rsaPrivateKey = 'MIIEogIBAAKCAQEAuruxWS/zWEXuot2pNUyPGxrVXuLo+CLjp8s0Hrw+mHw0Z2rrJFhLm5Zh61jbXok/VWTo+BUeDmpV6yZhSHpFQAwjzXOj1LrsDTahc3Q1gRx/eEX7zl1jiW6exPFI2E61szsT8K63LVLGUnqXfAeonW3bGj5YCJ85pcb+D6XLsk2aArEM7ni0g2G/cbbBzFH20TPfiGVTP7QaXWFKJQiiRnWf5n52pAzXcSR8KHa+Jujrul1PC/XUro+0vtPKSTvECVkAV6FC1K3i7j2DHjD3jCz1afG/wu5jmW01iMhAWQDqYNZWZCANusyMwBlE1eimuc7eZZGQ3reSuXuNGnpaVwIDAQABAoIBAH4LJWnLORnzkmHy9z1X4Rztnur+hTzA6CQhwJuieyoYZJMOE4yj4N0zxALFJsYIJGzywWJ7dp9KXZZtgKsYhVf3Gc+baBG23pEIuDAjV94/qzc3HgIs/XwN5DORnpcLghBqJ0qF1HVOcznHMCtzS/h/564uOOQQ/iuNR36ubfF60f4yhwg5P+RE9UvsRYK6tLAd7oB8vRryeXGIJu1tlQI7QaKW9Gx5uOejgfXPFDC478PQssvZIuTJyvkEbY3p7GSFjgKUpQmXc+IVD5HWaLMpmpgseuORU467c5ZYgsIW9xtAHNl30tX1h6aSjMhLwW5O8cUzceFSSb+Pqo1MR8ECgYEA7S/qr+lj5akWYoS+lZtjgjrJaiLbg1Q7uiFKSJZyy61GMYaPJ2zE8VyGMZxvZXFhPgAgPzQ45/sbMvBYWoz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nWf5n52pAzXcSR8KHa+Jujrul1PC/XUro+0vtPKSTvECVkAV6FC1K3i7j2DHjD3jCz1afG/wu5jmW01iMhAWQDqYNZWZCANusyMwBlE1eimuc7eZZGQ3reSuXuNGnpaVwIDAQAB';
  17. }
  18. /**
  19. * @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
  20. * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
  21. * @throws \GuzzleHttp\Exception\GuzzleException
  22. * 支付宝支付
  23. */
  24. public static function ali_pay($subject,$totalAmount,$outTradeNo,$notifyUrl){
  25. Factory::setOptions(self::getOptions($notifyUrl));
  26. try {
  27. //2. 发起API调用(以支付能力下的统一收单交易创建接口为例)
  28. //$result = Factory::payment()->common()->create("iPhone6 16G", "20200326235526001", "88.88", "2088002656718920");
  29. $result = Factory::payment()->App()->pay($subject,$outTradeNo , $totalAmount);
  30. $responseChecker = new ResponseChecker();
  31. //3. 处理响应或异常
  32. if ($responseChecker->success($result)) {
  33. return $result->body;
  34. } else {
  35. echo "调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL;
  36. }
  37. } catch (Exception $e) {
  38. return false;
  39. }
  40. }
  41. public static function ali_pay_pc($subject='商品',$totalAmount,$outTradeNo,$notifyUrl,$returnUrl){
  42. Factory::setOptions(self::getOptions($notifyUrl));
  43. try {
  44. $result = Factory::payment()->Page()->pay($subject,$outTradeNo,$totalAmount,$returnUrl);
  45. $responseChecker = new ResponseChecker();
  46. //3. 处理响应或异常
  47. if ($responseChecker->success($result)) {
  48. return $result->body;
  49. } else {
  50. echo "调用失败,原因:". $result->msg.",".$result->subMsg.PHP_EOL;
  51. }
  52. } catch (Exception $e) {
  53. return false;
  54. }
  55. }
  56. public static function getOptions($notifyUrl){
  57. $options = new \Alipay\EasySDK\Kernel\Config();
  58. $options->protocol = 'https';
  59. $options->gatewayHost = 'openapi.alipay.com';
  60. $options->signType = 'RSA2';
  61. $options->appId = '2021003130678750';
  62. // 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
  63. $options->merchantPrivateKey = 'MIIEogIBAAKCAQEAuruxWS/zWEXuot2pNUyPGxrVXuLo+CLjp8s0Hrw+mHw0Z2rrJFhLm5Zh61jbXok/VWTo+BUeDmpV6yZhSHpFQAwjzXOj1LrsDTahc3Q1gRx/eEX7zl1jiW6exPFI2E61szsT8K63LVLGUnqXfAeonW3bGj5YCJ85pcb+D6XLsk2aArEM7ni0g2G/cbbBzFH20TPfiGVTP7QaXWFKJQiiRnWf5n52pAzXcSR8KHa+Jujrul1PC/XUro+0vtPKSTvECVkAV6FC1K3i7j2DHjD3jCz1afG/wu5jmW01iMhAWQDqYNZWZCANusyMwBlE1eimuc7eZZGQ3reSuXuNGnpaVwIDAQABAoIBAH4LJWnLORnzkmHy9z1X4Rztnur+hTzA6CQhwJuieyoYZJMOE4yj4N0zxALFJsYIJGzywWJ7dp9KXZZtgKsYhVf3Gc+baBG23pEIuDAjV94/qzc3HgIs/XwN5DORnpcLghBqJ0qF1HVOcznHMCtzS/h/564uOOQQ/iuNR36ubfF60f4yhwg5P+RE9UvsRYK6tLAd7oB8vRryeXGIJu1tlQI7QaKW9Gx5uOejgfXPFDC478PQssvZIuTJyvkEbY3p7GSFjgKUpQmXc+IVD5HWaLMpmpgseuORU467c5ZYgsIW9xtAHNl30tX1h6aSjMhLwW5O8cUzceFSSb+Pqo1MR8ECgYEA7S/qr+lj5akWYoS+lZtjgjrJaiLbg1Q7uiFKSJZyy61GMYaPJ2zE8VyGMZxvZXFhPgAgPzQ45/sbMvBYWozJ8UNbcbJEO3dxoPEmYQBI7j2AIgO+yg3cMPIB9D6z+94lROlCVo+PjU8kTu0hI9/7WV+GFbEvDChptaGKaMet9CECgYEAyYtO3Dm/Su3TkBPmsX6zrKFHwBNZjQYWTRpTMy1fRSAl/kl6UDhriHBXDi/HGWCQelQlrcdWhmMXuygts7hN3G9k2LOYfJoVPS6sfgPgZPboVEh0Ea/uVEmp3l+H8wHEMj2dOScxe6LMrEk1UwfgCjeZv+bYqOZP7eBxL0RR/3cCgYAVTzjGlTFkcMySDVsSFePaz21JG9ic8/RpxJPn8ZxesDKeQHicEimfhbCIxIU9l/Ikn95Fz2tNbEIFUZKekAeUpXiRvnJi5VhQHVpVXLEwtlXMuIlrndMqkpOaXgfNmSfACTTskflTuMiKTsOrdXQfk3V4fXK8pOTabKkCwO3gAQKBgCoLjeATi/ytsL0V6cKNlB4nZziC0OPCqVRccY7zoDRjiImL+qBRpN1J0l97SCXbJ0jd1R5zv3E5qvG4aBBeGqAp/PfYP69+1X92BTYkyONHPyCWHOCzuxyKYY2qkEn5zFxOzkXmD0Sjqd8nRqCTvAFlqAWIuxpBMduGPxaFQjXxAoGAD2SBY2eQ7OWwil91X4TgdKhWXMpCgqQChvqB/rFPK6L9264mM1DIwWc3w5Keu8c5tElRLnK2dJtof/Tu5vKf29DZA2egc2OZSXI9qQlyH7orubNLK2tXoH1/phvy+45K2rczmU6LnJQvcBzWjf32lLIdqZx6CI8Q/iY2gHgq4EI=';
  64. /*$options->alipayCertPath = '<-- 请填写您的支付宝公钥证书文件路径,例如:/foo/alipayCertPublicKey_RSA2.crt -->';
  65. $options->alipayRootCertPath = '<-- 请填写您的支付宝根证书文件路径,例如:/foo/alipayRootCert.crt" -->';
  66. $options->merchantCertPath = '<-- 请填写您的应用公钥证书文件路径,例如:/foo/appCertPublicKey_2019051064521003.crt -->';*/
  67. //注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可
  68. $options->alipayPublicKey ='MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuruxWS/zWEXuot2pNUyPGxrVXuLo+CLjp8s0Hrw+mHw0Z2rrJFhLm5Zh61jbXok/VWTo+BUeDmpV6yZhSHpFQAwjzXOj1LrsDTahc3Q1gRx/eEX7zl1jiW6exPFI2E61szsT8K63LVLGUnqXfAeonW3bGj5YCJ85pcb+D6XLsk2aArEM7ni0g2G/cbbBzFH20TPfiGVTP7QaXWFKJQiiRnWf5n52pAzXcSR8KHa+Jujrul1PC/XUro+0vtPKSTvECVkAV6FC1K3i7j2DHjD3jCz1afG/wu5jmW01iMhAWQDqYNZWZCANusyMwBlE1eimuc7eZZGQ3reSuXuNGnpaVwIDAQAB';
  69. //可设置异步通知接收服务地址(可选)
  70. $options->notifyUrl = $notifyUrl;
  71. //可设置AES密钥,调用AES加解密相关接口时需要(可选)
  72. $options->encryptKey = "";
  73. return $options;
  74. }
  75. }