chengkun
2025-09-11 364a083e94138f7ed2d8114bf6dbdfda4eaf2683
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
<?php
 
namespace pay;
 
use Alipay\EasySDK\Kernel\Factory;
use Alipay\EasySDK\Kernel\Util\ResponseChecker;
use Alipay\EasySDK\Kernel\Config as AlipayConfig;
use think\facade\Config;
 
 
class Alipay {
    /**
     * 获取缓存配置
     * @access public
     * @param null|string $name    名称
     * @param mixed       $default 默认值
     * @return mixed
     */
    public function getConfig(string $name = null, $default = null)
    {
        if (!is_null($name)) {
            return Config::get('pay.' . $name, $default);
        }
        return Config::get('pay');
    }
 
    /**
     * 获取支付宝配置
     */
    private function getAlipayOptions() {
        $config = $this->getConfig('payment.alipay');
//        var_dump($config);
        $options = new AlipayConfig();
        $options->protocol = $config['protocol'];
        $options->gatewayHost = $config['gatewayHost'];
        $options->signType = $config['signType'];
        $options->appId = $config['appId'];
        // 为避免私钥随源码泄露,推荐从文件中读取私钥字符串而不是写入源码中
        $options->merchantPrivateKey = $config['merchantPrivateKey'];
        $options->alipayCertPath = $config['alipayCertPath'];
        $options->alipayRootCertPath = $config['alipayRootCertPath'];
        $options->merchantCertPath = $config['merchantCertPath'];
        //注:如果采用非证书模式,则无需赋值上面的三个证书路径,改为赋值如下的支付宝公钥字符串即可
        // $options->alipayPublicKey = '<-- 请填写您的支付宝公钥,例如:MIIBIjANBg... -->';
        //可设置异步通知接收服务地址(可选)
        $options->notifyUrl = $config['notifyUrl'];
 
        return $options;
    }
 
    public function pcPay()  {
        $create = Factory::payment()->common()->create("Iphone6 16G",
            microtime(), "88.88", "2088002656718920");
        $result = Factory::payment()->page()
            ->asyncNotify("https://www.test2.com/newCallback")
            ->pay("Iphone6 16G", $create->outTradeNo, "0.10", "https://www.taobao.com");
        $this->assertEquals(true, strpos($result->body, 'alipay-easysdk-php-') > 0);
        $this->assertEquals(true, strpos($result->body, 'sign') > 0);
    }
}