123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168 |
- <?php
- namespace App\Http\Controllers\V1;
- use App\Models\AdminMerchant;
- use Illuminate\Http\Request;
- use Illuminate\Support\Facades\Log;
- use Laravel\Lumen\Routing\Controller as BaseController;
- use Alipay\EasySDK\Kernel\Factory;
- use Alipay\EasySDK\Kernel\Util\ResponseChecker;
- use Alipay\EasySDK\Kernel\Config;
- use App\Library\Alipay\Alipay;
- use App\Library\Alipay\MessageSendData;//支付宝模板消息数据类
- class TestController extends BaseController
- {
- public $setting;
- protected static $MERCHANT_ID = 0;
- protected static $MERCHANT = [];
- function __construct()
- {
- // $this->cinfig_pay();//支付配置初始化
- }
- public function cinfig_pay(){
- self::$MERCHANT_ID = 1;
- self::$MERCHANT = AdminMerchant::byIdNoStatus(1);
- if (!self::$MERCHANT) {
- Log::error('[支付]:参数错误');
- }
- $merchant = self::$MERCHANT;
- $options = new Config();
- $options->protocol = config('alipay.alipay.default.protocol');
- $options->gatewayHost = config('alipay.alipay.default.gatewayHost');
- $options->signType = config('alipay.alipay.default.signType');
- $options->appId = $merchant['alipaymini_appId'];
- // 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
- $options->merchantPrivateKey = config('alipay.alipay.default.merchantPrivateKey');
- $options->alipayCertPath = base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_alipayCertPath'];
- $options->alipayRootCertPath = base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_alipayRootCertPath'];
- $options->merchantCertPath = base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_merchantCertPath'];
- //注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可
- // $options->alipayPublicKey = '<-- 请填写您的支付宝公钥,例如:MIIBIjANBg... -->';
- //可设置异步通知接收服务地址(可选)
- $options->notifyUrl = config('alipay.alipay.default.notifyUrl');
- $this->setting = Factory::setOptions($options);
- }
- //支付宝资金预授权 老版本支付宝sdk
- public function zijinyushouquan(){
- self::$MERCHANT_ID = 3;
- self::$MERCHANT = AdminMerchant::byIdNoStatus(3);
- if (!self::$MERCHANT) {
- Log::error('[支付]:参数错误');
- }
- $merchant = self::$MERCHANT;
- $alipayobj = new Alipay();
- // $result = $alipayobj->zijidongjie_cret(); // 证书公钥
- // $result = $alipayobj->zijidongjie(); // 普通公钥
- // $result = $alipayobj->SdkAliPay(); //成功执行
- $data = [
- //公共参数
- 'appId' => $merchant['alipaymini_appId'],
- 'rsaPrivateKey' => $merchant['alipaymini_merchantPrivateKey'],
- // 'appCertPath' => base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_merchantCertPath'],
- // 'alipayCertPath' => base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_alipayCertPath'],
- // 'rootCertPath' => base_path() . '/storage/app/public/merchant/' . $merchant['alipaymini_alipayRootCertPath'],
- 'appCertPath' => base_path() . '/database/zhifubao/' . $merchant['alipaymini_merchantCertPath'],
- 'alipayCertPath' => base_path() . '/database/zhifubao/' . $merchant['alipaymini_alipayCertPath'],
- 'rootCertPath' => base_path() . '/database/zhifubao/' . $merchant['alipaymini_alipayRootCertPath']
- ];
- $message = MessageSendData::PayMessageData([
- 'to_user_id'=>'2088822410577596',
- 'user_template_id'=> 'bafa857bc7614494b9a100bb177ca945',
- // 'page'=>'pages/index/index',
- // 'keyword1'=> 'keyword1',
- 'keyword2'=> 'keyword2',
- 'keyword3'=> 'keyword3',
- 'keyword4'=> 'keyword4',
- ]);
- $result = $alipayobj->AlipaySendMessage($data,$message);//发送模板消息
- }
- public function login(Request $request)
- {
- try {
- $result = $this->setting->base()->oauth()->getToken('ee4b3c871f7c4f30a82251908458VB64');
- $responseChecker = new ResponseChecker();
- //3. 处理响应或异常
- if ($responseChecker->success($result)) {
- echo "调用成功" . PHP_EOL;
- } else {
- echo "调用失败,原因:" . $result->msg . "," . $result->subMsg . PHP_EOL;
- }
- } catch (Exception $e) {
- echo "调用失败," . $e->getMessage() . PHP_EOL;;
- }
- }
- public function alipayment(Request $request)
- {
- try {
- //2. 发起API调用(以支付能力下的统一收单交易创建接口为例)
- $result = $this->setting->payment()->common()->create("iPhone6 16G", rand(10000,99999), "0.01",'2088822410577596');
- //3. 处理响应或异常
- if (!empty($result->code) && $result->code == 10000) {
- echo "调用成功" . PHP_EOL;
- print_r('<pre>');
- print_r($result);
- } else {
- echo "调用失败,原因:" . $result->msg . "," . $result->subMsg . PHP_EOL;
- }
- } catch (Exception $e) {
- echo "调用失败," . $e->getMessage() . PHP_EOL;;
- }
- }
- //沙箱环境支付宝支付回调
- public function alipay_notify()
- {
- file_put_contents('alipay_notify.txt', date('Y-m-d H:i:s', time()) , FILE_APPEND);
- }
- }
|