TestController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <?php
  2. namespace App\Http\Controllers\V1;
  3. use App\Models\AdminMerchant;
  4. use Illuminate\Http\Request;
  5. use Illuminate\Support\Facades\Log;
  6. use Laravel\Lumen\Routing\Controller as BaseController;
  7. use Alipay\EasySDK\Kernel\Factory;
  8. use Alipay\EasySDK\Kernel\Util\ResponseChecker;
  9. use Alipay\EasySDK\Kernel\Config;
  10. use App\Library\Alipay\Alipay;
  11. use App\Library\Alipay\MessageSendData;//支付宝模板消息数据类
  12. class TestController extends BaseController
  13. {
  14. public $setting;
  15. protected static $MERCHANT_ID = 0;
  16. protected static $MERCHANT = [];
  17. function __construct()
  18. {
  19. // $this->cinfig_pay();//支付配置初始化
  20. }
  21. public function cinfig_pay(){
  22. self::$MERCHANT_ID = 1;
  23. self::$MERCHANT = AdminMerchant::byIdNoStatus(1);
  24. if (!self::$MERCHANT) {
  25. Log::error('[支付]:参数错误');
  26. }
  27. $merchant = self::$MERCHANT;
  28. $options = new Config();
  29. $options->protocol = config('alipay.alipay.default.protocol');
  30. $options->gatewayHost = config('alipay.alipay.default.gatewayHost');
  31. $options->signType = config('alipay.alipay.default.signType');
  32. $options->appId = $merchant['alipaymini_appId'];
  33. // 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
  34. $options->merchantPrivateKey = config('alipay.alipay.default.merchantPrivateKey');
  35. $options->alipayCertPath = base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_alipayCertPath'];
  36. $options->alipayRootCertPath = base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_alipayRootCertPath'];
  37. $options->merchantCertPath = base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_merchantCertPath'];
  38. //注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可
  39. // $options->alipayPublicKey = '<-- 请填写您的支付宝公钥,例如:MIIBIjANBg... -->';
  40. //可设置异步通知接收服务地址(可选)
  41. $options->notifyUrl = config('alipay.alipay.default.notifyUrl');
  42. $this->setting = Factory::setOptions($options);
  43. }
  44. //支付宝资金预授权 老版本支付宝sdk
  45. public function zijinyushouquan(){
  46. self::$MERCHANT_ID = 3;
  47. self::$MERCHANT = AdminMerchant::byIdNoStatus(3);
  48. if (!self::$MERCHANT) {
  49. Log::error('[支付]:参数错误');
  50. }
  51. $merchant = self::$MERCHANT;
  52. $alipayobj = new Alipay();
  53. // $result = $alipayobj->zijidongjie_cret(); // 证书公钥
  54. // $result = $alipayobj->zijidongjie(); // 普通公钥
  55. // $result = $alipayobj->SdkAliPay(); //成功执行
  56. $data = [
  57. //公共参数
  58. 'appId' => $merchant['alipaymini_appId'],
  59. 'rsaPrivateKey' => $merchant['alipaymini_merchantPrivateKey'],
  60. // 'appCertPath' => base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_merchantCertPath'],
  61. // 'alipayCertPath' => base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_alipayCertPath'],
  62. // 'rootCertPath' => base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_alipayRootCertPath'],
  63. 'appCertPath' => base_path() . '/database/zhifubao/' . $merchant['alipaymini_merchantCertPath'],
  64. 'alipayCertPath' => base_path() . '/database/zhifubao/' . $merchant['alipaymini_alipayCertPath'],
  65. 'rootCertPath' => base_path() . '/database/zhifubao/' . $merchant['alipaymini_alipayRootCertPath']
  66. ];
  67. $message = MessageSendData::PayMessageData([
  68. 'to_user_id'=>'2088822410577596',
  69. 'user_template_id'=> 'bafa857bc7614494b9a100bb177ca945',
  70. // 'page'=>'pages/index/index',
  71. // 'keyword1'=> 'keyword1',
  72. 'keyword2'=> 'keyword2',
  73. 'keyword3'=> 'keyword3',
  74. 'keyword4'=> 'keyword4',
  75. ]);
  76. $result = $alipayobj->AlipaySendMessage($data,$message);//发送模板消息
  77. }
  78. public function login(Request $request)
  79. {
  80. try {
  81. $result = $this->setting->base()->oauth()->getToken('ee4b3c871f7c4f30a82251908458VB64');
  82. $responseChecker = new ResponseChecker();
  83. //3. 处理响应或异常
  84. if ($responseChecker->success($result)) {
  85. echo "调用成功" . PHP_EOL;
  86. } else {
  87. echo "调用失败,原因:" . $result->msg . "," . $result->subMsg . PHP_EOL;
  88. }
  89. } catch (Exception $e) {
  90. echo "调用失败," . $e->getMessage() . PHP_EOL;;
  91. }
  92. }
  93. public function alipayment(Request $request)
  94. {
  95. try {
  96. //2. 发起API调用(以支付能力下的统一收单交易创建接口为例)
  97. $result = $this->setting->payment()->common()->create("iPhone6 16G", rand(10000,99999), "0.01",'2088822410577596');
  98. //3. 处理响应或异常
  99. if (!empty($result->code) && $result->code == 10000) {
  100. echo "调用成功" . PHP_EOL;
  101. print_r('<pre>');
  102. print_r($result);
  103. } else {
  104. echo "调用失败,原因:" . $result->msg . "," . $result->subMsg . PHP_EOL;
  105. }
  106. } catch (Exception $e) {
  107. echo "调用失败," . $e->getMessage() . PHP_EOL;;
  108. }
  109. }
  110. //沙箱环境支付宝支付回调
  111. public function alipay_notify()
  112. {
  113. file_put_contents('alipay_notify.txt', date('Y-m-d H:i:s', time()) , FILE_APPEND);
  114. }
  115. }