├── .editorconfig
├── .github
├── CONTRIBUTING.md
├── FUNDING.yml
├── ISSUE_TEMPLATE
│ ├── bug_report.md
│ └── feature_request.md
├── SECURITY.md
├── dependabot.yml
└── workflows
│ ├── php-cs-fixer.yml
│ ├── psalm.yml
│ ├── run-tests.yml
│ └── update-changelog.yml
├── .php-cs-fixer.php
├── CHANGELOG.md
├── LICENSE
├── README.md
├── _ide_helper.php
├── composer.json
├── docs
└── usage.png
├── psalm.xml.dist
└── src
└── Pay.php
/.editorconfig:
--------------------------------------------------------------------------------
1 | root = true
2 |
3 | [*]
4 | indent_style = space
5 | indent_size = 4
6 | end_of_line = lf
7 | charset = utf-8
8 | trim_trailing_whitespace = true
9 | insert_final_newline = false
10 |
11 | [*.{vue,js,scss}]
12 | charset = utf-8
13 | indent_style = space
14 | indent_size = 2
15 | end_of_line = lf
16 | insert_final_newline = true
17 | trim_trailing_whitespace = true
18 |
19 | [*.md]
20 | trim_trailing_whitespace = false
--------------------------------------------------------------------------------
/.github/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # Contributing
2 |
3 | Contributions are **welcome** and will be fully **credited**.
4 |
5 | Please read and understand the contribution guide before creating an issue or pull request.
6 |
7 | ## Etiquette
8 |
9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code
10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be
11 | extremely unfair for them to suffer abuse or anger for their hard work.
12 |
13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the
14 | world that developers are civilized and selfless people.
15 |
16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient
17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used.
18 |
19 | ## Viability
20 |
21 | When requesting or submitting new features, first consider whether it might be useful to others. Open
22 | source projects are used by many developers, who may have entirely different needs to your own. Think about
23 | whether or not your feature is likely to be used by other users of the project.
24 |
25 | ## Procedure
26 |
27 | Before filing an issue:
28 |
29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident.
30 | - Check to make sure your feature suggestion isn't already present within the project.
31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress.
32 | - Check the pull requests tab to ensure that the feature isn't already in progress.
33 |
34 | Before submitting a pull request:
35 |
36 | - Check the codebase to ensure that your feature doesn't already exist.
37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix.
38 |
39 | ## Requirements
40 |
41 | If the project maintainer has any additional requirements, you will find them listed here.
42 |
43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer).
44 |
45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests.
46 |
47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.
48 |
49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option.
50 |
51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.
52 |
53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.
54 |
55 | **Happy coding**!
56 |
--------------------------------------------------------------------------------
/.github/FUNDING.yml:
--------------------------------------------------------------------------------
1 | # These are supported funding model platforms
2 |
3 | patreon: guanguans
4 | custom: https://www.guanguans.cn/images/wechat.jpeg
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/bug_report.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Bug report
3 | about: Create a report to help us improve
4 | title: ''
5 | labels: bug
6 | assignees: guanguans
7 |
8 | ---
9 |
10 | **Describe the bug**
11 | A clear and concise description of what the bug is.
12 |
13 | **To Reproduce**
14 | Steps to reproduce the behavior:
15 | 1. Go to '...'
16 | 2. Click on '....'
17 | 3. Scroll down to '....'
18 | 4. See error
19 |
20 | **Expected behavior**
21 | A clear and concise description of what you expected to happen.
22 |
23 | **Screenshots**
24 | If applicable, add screenshots to help explain your problem.
25 |
26 | **Additional context**
27 | Add any other context about the problem here.
--------------------------------------------------------------------------------
/.github/ISSUE_TEMPLATE/feature_request.md:
--------------------------------------------------------------------------------
1 | ---
2 | name: Feature request
3 | about: Suggest an idea for this project
4 | title: ''
5 | labels: good idea
6 | assignees: guanguans
7 |
8 | ---
9 |
10 | **Is your feature request related to a problem? Please describe.**
11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
12 |
13 | **Describe the solution you'd like**
14 | A clear and concise description of what you want to happen.
15 |
16 | **Describe alternatives you've considered**
17 | A clear and concise description of any alternative solutions or features you've considered.
18 |
19 | **Additional context**
20 | Add any other context or screenshots about the feature request here.
--------------------------------------------------------------------------------
/.github/SECURITY.md:
--------------------------------------------------------------------------------
1 | # Security Policy
2 |
3 | If you discover any security related issues, please email ityaozm@gmail.com instead of using the issue tracker.
4 |
--------------------------------------------------------------------------------
/.github/dependabot.yml:
--------------------------------------------------------------------------------
1 | version: 2
2 | updates:
3 | - package-ecosystem: composer
4 | directory: "/"
5 | schedule:
6 | interval: daily
7 | time: "21:00"
8 | open-pull-requests-limit: 10
9 |
--------------------------------------------------------------------------------
/.github/workflows/php-cs-fixer.yml:
--------------------------------------------------------------------------------
1 | name: check & fix styling
2 |
3 | on: [push]
4 |
5 | jobs:
6 | php-cs-fixer:
7 | runs-on: ubuntu-latest
8 |
9 | steps:
10 | - name: Checkout code
11 | uses: actions/checkout@v2
12 | with:
13 | ref: ${{ github.head_ref }}
14 |
15 | - name: Run PHP CS Fixer
16 | uses: docker://oskarstark/php-cs-fixer-ga
17 | with:
18 | args: --config=.php-cs-fixer.php --allow-risky=yes
19 |
20 | - name: Commit changes
21 | uses: stefanzweifel/git-auto-commit-action@v4
22 | with:
23 | commit_message: Fix styling
24 |
--------------------------------------------------------------------------------
/.github/workflows/psalm.yml:
--------------------------------------------------------------------------------
1 | name: psalm
2 |
3 | on:
4 | push:
5 | paths:
6 | - '**.php'
7 | - 'psalm.xml.dist'
8 |
9 | jobs:
10 | psalm:
11 | name: psalm
12 | runs-on: ubuntu-latest
13 | steps:
14 | - uses: actions/checkout@v2
15 |
16 | - name: Setup PHP
17 | uses: shivammathur/setup-php@v2
18 | with:
19 | php-version: '7.4'
20 | extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
21 | coverage: none
22 |
23 | - name: Cache composer dependencies
24 | uses: actions/cache@v2
25 | with:
26 | path: vendor
27 | key: composer-${{ hashFiles('composer.lock') }}
28 |
29 | - name: Run composer install
30 | run: composer install -n --prefer-dist
31 |
32 | - name: Run psalm
33 | run: ./vendor/bin/psalm
34 |
--------------------------------------------------------------------------------
/.github/workflows/run-tests.yml:
--------------------------------------------------------------------------------
1 | name: tests
2 |
3 | on: [push, pull_request]
4 |
5 | jobs:
6 | test:
7 | runs-on: ${{ matrix.os }}
8 | strategy:
9 | fail-fast: true
10 | matrix:
11 | os: [ubuntu-latest]
12 | php: [7.1]
13 | dependency-version: [prefer-stable]
14 |
15 | name: P${{ matrix.php }} - ${{ matrix.dependency-version }} - ${{ matrix.os }}
16 |
17 | steps:
18 | - name: Checkout code
19 | uses: actions/checkout@v2
20 |
21 | - name: Cache dependencies
22 | uses: actions/cache@v2
23 | with:
24 | path: ~/.composer/cache/files
25 | key: dependencies-php-${{ matrix.php }}-composer-${{ hashFiles('composer.json') }}
26 |
27 | - name: Setup PHP
28 | uses: shivammathur/setup-php@v2
29 | with:
30 | php-version: ${{ matrix.php }}
31 | extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, bcmath, soap, intl, gd, exif, iconv, imagick
32 | coverage: xdebug
33 |
34 | - name: Install dependencies
35 | run: composer update --${{ matrix.dependency-version }} --prefer-dist --no-interaction
36 |
37 | - name: Execute tests
38 | run: composer test-coverage
39 |
40 | - name: Upload coverage to Codecov
41 | uses: codecov/codecov-action@v1
42 | with:
43 | token: ${{ secrets.CODECOV_TOKEN }} # not required for public repos
44 | fail_ci_if_error: false # optional (default = false)
45 | verbose: true # optional (default = false)
--------------------------------------------------------------------------------
/.github/workflows/update-changelog.yml:
--------------------------------------------------------------------------------
1 | name: "update changelog"
2 |
3 | on:
4 | release:
5 | types: [ released ]
6 |
7 | jobs:
8 | update:
9 | runs-on: ubuntu-latest
10 |
11 | steps:
12 | - name: Checkout code
13 | uses: actions/checkout@v3
14 | with:
15 | ref: 1.x
16 |
17 | - name: Update Changelog
18 | uses: stefanzweifel/changelog-updater-action@v1
19 | with:
20 | latest-version: ${{ github.event.release.name }}
21 | release-notes: ${{ github.event.release.body }}
22 |
23 | - name: Commit updated CHANGELOG
24 | uses: stefanzweifel/git-auto-commit-action@v4
25 | with:
26 | branch: 1.x
27 | commit_message: Update CHANGELOG
28 | file_pattern: CHANGELOG.md
29 |
--------------------------------------------------------------------------------
/.php-cs-fixer.php:
--------------------------------------------------------------------------------
1 |
7 |
8 | This source file is subject to the MIT license that is bundled.
9 | EOF;
10 |
11 | $finder = Symfony\Component\Finder\Finder::create()
12 | ->in([
13 | __DIR__ . '/src',
14 | __DIR__ . '/tests',
15 | ])
16 | ->name('*.php')
17 | ->notName('*.blade.php')
18 | ->ignoreDotFiles(true)
19 | ->ignoreVCS(true);
20 |
21 | return (new PhpCsFixer\Config())
22 | ->setRules([
23 | // '@PSR12' => true,
24 | '@Symfony' => true,
25 | 'header_comment' => [
26 | 'header' => $header,
27 | 'comment_type' => 'PHPDoc'
28 | ],
29 | 'array_syntax' => ['syntax' => 'short'],
30 | 'ordered_imports' => ['sort_algorithm' => 'alpha'],
31 | 'no_unused_imports' => true,
32 | 'not_operator_with_successor_space' => true,
33 | 'no_useless_else' => true,
34 | 'no_useless_return' => true,
35 | 'single_quote' => true,
36 | 'class_attributes_separation' => true,
37 | 'standardize_not_equals' => true,
38 | // 'trailing_comma_in_multiline' => true,
39 | // 'php_unit_construct' => true,
40 | // 'php_unit_strict' => true,
41 | // 'declare_strict_types' => true,
42 | ])
43 | // ->setRiskyAllowed(true)
44 | ->setFinder($finder);
45 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Changelog
2 |
3 | All notable changes to `guanguans/yii-pay` will be documented in this file
4 |
5 | ## v1.2.3 - 2022-05-25
6 |
7 | ## What's Changed
8 |
9 | - ⬆️ Update phpunit/phpunit requirement from ^7.0|^8.0 to ^7.0|^8.0|^9.0 by @dependabot in https://github.com/guanguans/yii-pay/pull/20
10 |
11 | ## New Contributors
12 |
13 | - @dependabot made their first contribution in https://github.com/guanguans/yii-pay/pull/20
14 |
15 | **Full Changelog**: https://github.com/guanguans/yii-pay/compare/v1.2.2...v1.2.3
16 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2020 琯琯
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
yii-pay
2 |
3 | > 基于 [yansongda/pay](https://github.com/yansongda/pay) 开发的适配于 Yii 的 alipay 和 wechat 的支付扩展包。
4 |
5 | 
6 |
7 | [](https://github.com/guanguans/yii-pay/actions/workflows/run-tests.yml)
8 | [](https://github.com/guanguans/yii-pay/actions/workflows/php-cs-fixer.yml)
9 | [](https://codecov.io/gh/guanguans/yii-pay)
10 | [](https://packagist.org/packages/guanguans/yii-pay)
11 | [](https://packagist.org/packages/guanguans/yii-pay)
12 | [](https://packagist.org/packages/guanguans/yii-pay)
13 |
14 | ## 环境要求
15 |
16 | * Yii >= 2.0
17 |
18 | ## 安装
19 |
20 | ```shell
21 | $ composer require guanguans/yii-pay --prefer-dist -v
22 | ```
23 |
24 | ## 配置
25 |
26 | Yii2 配置文件 `config/main.php` 的 components 中添加:
27 |
28 | ```php
29 | 'components' => [
30 | // ...
31 | 'pay' => [
32 | 'class' => 'Guanguans\YiiPay\Pay',
33 | // 'defaultDriver' => 'wechat', // 默认支付驱动[wechat, alipay] 若配置为 wechat:`Yii::$app->pay->wap();` 等效于 `Yii::$app->pay->wechat->wap();`
34 | 'wechatOptions' => [
35 | 'appid' => 'wxb3fxxxxxxxxxxx', // APP APPID
36 | 'app_id' => 'wxb3fxxxxxxxxxxx', // 公众号 APPID
37 | 'miniapp_id' => 'wxb3fxxxxxxxxxxx', // 小程序 APPID
38 | 'mch_id' => '14577xxxx',
39 | 'key' => 'mF2suE9sU6Mk1Cxxxxxxxxxxx',
40 | 'notify_url' => 'http://xxxxxx.cn/notify.php',
41 | 'cert_client' => './cert/apiclient_cert.pem', // optional,退款等情况时用到
42 | 'cert_key' => './cert/apiclient_key.pem',// optional,退款等情况时用到
43 | 'log' => [ // optional
44 | 'file' => './logs/wechat.log',
45 | 'level' => 'info', // 建议生产环境等级调整为 info,开发环境为 debug
46 | 'type' => 'single', // optional, 可选 daily.
47 | 'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
48 | ],
49 | 'http' => [ // optional
50 | 'timeout' => 5.0,
51 | 'connect_timeout' => 5.0,
52 | // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
53 | ],
54 | // 'mode' => 'dev', // optional, dev/hk;当为 `hk` 时,为香港 gateway。
55 | ],
56 | 'alipayOptions' => [
57 | 'app_id' => '2016082000295641',
58 | 'notify_url' => 'http://xxxxxx.cn/notify.php',
59 | 'return_url' => 'http://xxxxxx.cn/return.php',
60 | 'ali_public_key' => 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAuWJKrQ6SWvS6niI+4vEVZiYfjkCfLQfoFI2nCp9ZLDS42QtiL4Ccyx8scgc3nhVwmVRte8f57TFvGhvJD0upT4O5O/lRxmTjechXAorirVdAODpOu0mFfQV9y/T9o9hHnU+VmO5spoVb3umqpq6D/Pt8p25Yk852/w01VTIczrXC4QlrbOEe3sr1E9auoC7rgYjjCO6lZUIDjX/oBmNXZxhRDrYx4Yf5X7y8FRBFvygIE2FgxV4Yw+SL3QAa2m5MLcbusJpxOml9YVQfP8iSurx41PvvXUMo49JG3BDVernaCYXQCoUJv9fJwbnfZd7J5YByC+5KM4sblJTq7bXZWQIDAQAB',
61 | // 加密方式: **RSA2**
62 | 'private_key' => 'MIIEpAIBAAKCAQEAs6+F2leOgOrvj9jTeDhb5q46GewOjqLBlGSs/bVL4Z3fMr3p+Q1Tux/6uogeVi/eHd84xvQdfpZ87A1SfoWnEGH5z15yorccxSOwWUI+q8gz51IWqjgZxhWKe31BxNZ+prnQpyeMBtE25fXp5nQZ/pftgePyUUvUZRcAUisswntobDQKbwx28VCXw5XB2A+lvYEvxmMv/QexYjwKK4M54j435TuC3UctZbnuynSPpOmCu45ZhEYXd4YMsGMdZE5/077ZU1aU7wx/gk07PiHImEOCDkzqsFo0Buc/knGcdOiUDvm2hn2y1XvwjyFOThsqCsQYi4JmwZdRa8kvOf57nwIDAQABAoIBAQCw5QCqln4VTrTvcW+msB1ReX57nJgsNfDLbV2dG8mLYQemBa9833DqDK6iynTLNq69y88ylose33o2TVtEccGp8Dqluv6yUAED14G6LexS43KtrXPgugAtsXE253ZDGUNwUggnN1i0MW2RcMqHdQ9ORDWvJUCeZj/AEafgPN8AyiLrZeL07jJz/uaRfAuNqkImCVIarKUX3HBCjl9TpuoMjcMhz/MsOmQ0agtCatO1eoH1sqv5Odvxb1i59c8Hvq/mGEXyRuoiDo05SE6IyXYXr84/Nf2xvVNHNQA6kTckj8shSi+HGM4mO1Y4Pbb7XcnxNkT0Inn6oJMSiy56P+CpAoGBAO1O+5FE1ZuVGuLb48cY+0lHCD+nhSBd66B5FrxgPYCkFOQWR7pWyfNDBlmO3SSooQ8TQXA25blrkDxzOAEGX57EPiipXr/hy5e+WNoukpy09rsO1TMsvC+v0FXLvZ+TIAkqfnYBgaT56ku7yZ8aFGMwdCPL7WJYAwUIcZX8wZ3dAoGBAMHWplAqhe4bfkGOEEpfs6VvEQxCqYMYVyR65K0rI1LiDZn6Ij8fdVtwMjGKFSZZTspmsqnbbuCE/VTyDzF4NpAxdm3cBtZACv1Lpu2Om+aTzhK2PI6WTDVTKAJBYegXaahBCqVbSxieR62IWtmOMjggTtAKWZ1P5LQcRwdkaB2rAoGAWnAPT318Kp7YcDx8whOzMGnxqtCc24jvk2iSUZgb2Dqv+3zCOTF6JUsV0Guxu5bISoZ8GdfSFKf5gBAo97sGFeuUBMsHYPkcLehM1FmLZk1Q+ljcx3P1A/ds3kWXLolTXCrlpvNMBSN5NwOKAyhdPK/qkvnUrfX8sJ5XK2H4J8ECgYAGIZ0HIiE0Y+g9eJnpUFelXvsCEUW9YNK4065SD/BBGedmPHRC3OLgbo8X5A9BNEf6vP7fwpIiRfKhcjqqzOuk6fueA/yvYD04v+Da2MzzoS8+hkcqF3T3pta4I4tORRdRfCUzD80zTSZlRc/h286Y2eTETd+By1onnFFe2X01mwKBgQDaxo4PBcLL2OyVT5DoXiIdTCJ8KNZL9+kV1aiBuOWxnRgkDjPngslzNa1bK+klGgJNYDbQqohKNn1HeFX3mYNfCUpuSnD2Yag53Dd/1DLO+NxzwvTu4D6DCUnMMMBVaF42ig31Bs0jI3JQZVqeeFzSET8fkoFopJf3G6UXlrIEAQ==',
63 | // 使用公钥证书模式,请配置下面两个参数,同时修改ali_public_key为以.crt结尾的支付宝公钥证书路径,如(./cert/alipayCertPublicKey_RSA2.crt)
64 | // 'app_cert_public_key' => './cert/appCertPublicKey.crt', //应用公钥证书路径
65 | // 'alipay_root_cert' => './cert/alipayRootCert.crt', //支付宝根证书路径
66 | 'log' => [ // optional
67 | 'file' => './logs/alipay.log',
68 | 'level' => 'info', // 建议生产环境等级调整为 info,开发环境为 debug
69 | 'type' => 'single', // optional, 可选 daily.
70 | 'max_file' => 30, // optional, 当 type 为 daily 时有效,默认 30 天
71 | ],
72 | 'http' => [ // optional
73 | 'timeout' => 5.0,
74 | 'connect_timeout' => 5.0,
75 | // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
76 | ],
77 | // 'mode' => 'dev', // optional,设置此参数,将进入沙箱模式
78 | ],
79 | ],
80 | // ...
81 | ]
82 | ```
83 |
84 | ## 使用
85 |
86 | ### 获取 alipay 实例
87 |
88 | ```php
89 | Yii::$app->pay->getAlipay();
90 | // or
91 | Yii::$app->pay->alipay;
92 | ```
93 |
94 | ### 支付宝使用示例,更多详细文档请参考 [yansongda/pay](https://github.com/yansongda/pay)
95 |
96 | ```php
97 | time(),
109 | 'total_amount' => '1',
110 | 'subject' => 'test subject - 测试',
111 | ];
112 |
113 | $alipay = Yii::$app->pay->getAlipay()->web($order); // 电脑支付
114 | // $alipay = Yii::$app->pay->getAlipay()->wap($order); // 手机网站支付
115 | // $alipay = Yii::$app->pay->getAlipay()->app($order); // APP 支付
116 | // $alipay = Yii::$app->pay->getAlipay()->pos($order); // 刷卡支付
117 | // $alipay = Yii::$app->pay->getAlipay()->scan($order); // 扫码支付
118 | // $alipay = Yii::$app->pay->getAlipay()->transfer($order); // 帐户转账
119 | // $alipay = Yii::$app->pay->getAlipay()->mini($order); // 小程序支付
120 |
121 | return $alipay->send();
122 | }
123 |
124 | public function actionReturn()
125 | {
126 | $data = Yii::$app->pay->getAlipay()->verify();
127 |
128 | // 订单号:$data->out_trade_no
129 | // 支付宝交易号:$data->trade_no
130 | // 订单总金额:$data->total_amount
131 | }
132 |
133 | public function actionNotify()
134 | {
135 | $alipay = Yii::$app->pay->getAlipay();
136 |
137 | try{
138 | $data = $alipay->verify();
139 |
140 | // 请自行对 trade_status 进行判断及其它逻辑进行判断,在支付宝的业务通知中,只有交易通知状态为 TRADE_SUCCESS 或 TRADE_FINISHED 时,支付宝才会认定为买家付款成功。
141 | // 1、商户需要验证该通知数据中的out_trade_no是否为商户系统中创建的订单号;
142 | // 2、判断total_amount是否确实为该订单的实际金额(即商户订单创建时的金额);
143 | // 3、校验通知中的seller_id(或者seller_email) 是否为out_trade_no这笔单据的对应的操作方(有的时候,一个商户可能有多个seller_id/seller_email);
144 | // 4、验证app_id是否为该商户本身。
145 | // 5、其它业务逻辑情况
146 |
147 | Yii::$app->pay->getLog()->debug('Alipay notify', $data->all());
148 | } catch (\Exception $e) {
149 | // $e->getMessage();
150 | }
151 |
152 | return $alipay->success()->send();
153 | }
154 | }
155 | ```
156 |
157 | ### 获取微信实例
158 |
159 | ```php
160 | Yii::$app->pay->getWechat();
161 | // or
162 | Yii::$app->pay->wechat;
163 | ```
164 |
165 | ### 微信使用示例,更多详细文档请参考 [yansongda/pay](https://github.com/yansongda/pay)
166 |
167 | ```php
168 | time(),
180 | 'total_fee' => '1', // **单位:分**
181 | 'body' => 'test body - 测试',
182 | 'openid' => 'onkVf1FjWS5SBIixxxxxxx',
183 | ];
184 |
185 | $pay = Yii::$app->pay->getWechat()->mp($order); // 公众号支付
186 | // $pay = Yii::$app->pay->getWechat()->miniapp($order); // 小程序支付
187 | // $pay = Yii::$app->pay->getWechat()->wap($order); // H5 支付
188 | // $pay = Yii::$app->pay->getWechat()->scan($order); // 扫码支付
189 | // $pay = Yii::$app->pay->getWechat()->pos($order); // 刷卡支付
190 | // $pay = Yii::$app->pay->getWechat()->app($order); // APP 支付
191 | // $pay = Yii::$app->pay->getWechat()->transfer($order); // 企业付款
192 | // $pay = Yii::$app->pay->getWechat()->redpack($order); // 普通红包
193 | // $pay = Yii::$app->pay->getWechat()->groupRedpack($order); // 分裂红包
194 |
195 | // $pay->appId
196 | // $pay->timeStamp
197 | // $pay->nonceStr
198 | // $pay->package
199 | // $pay->signType
200 | }
201 |
202 | public function actionNotify()
203 | {
204 | $pay = Yii::$app->pay->getWechat();
205 |
206 | try{
207 | $data = $pay->verify();
208 |
209 | Yii::$app->pay->getLog()->debug('Alipay notify', $data->all());
210 | } catch (\Exception $e) {
211 | // $e->getMessage();
212 | }
213 |
214 | return $pay->success()->send();
215 | }
216 | }
217 | ```
218 |
219 | ## 测试
220 |
221 | ```shell
222 | $ composer test
223 | ```
224 |
225 | ## 相关链接
226 |
227 | * [https://github.com/yansongda/pay](https://github.com/yansongda/pay),[yansongda](https://github.com/yansongda)
228 |
229 | ## License
230 |
231 | [MIT](LICENSE)
--------------------------------------------------------------------------------
/_ide_helper.php:
--------------------------------------------------------------------------------
1 |
7 | *
8 | * This source file is subject to the MIT license that is bundled.
9 | */
10 |
11 | class Yii
12 | {
13 | /**
14 | * @var Application
15 | */
16 | public static $app;
17 | }
18 |
19 | /**
20 | * @property \Guanguans\YiiPay\Pay $pay
21 | */
22 | class Application extends \yii\web\Application
23 | {
24 | }
25 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "guanguans/yii-pay",
3 | "description": "适配于 Yii 的 alipay 和 wechat 的支付扩展包",
4 | "license": "MIT",
5 | "type": "library",
6 | "keywords": [
7 | "pay",
8 | "alipay",
9 | "wechat"
10 | ],
11 | "authors": [
12 | {
13 | "name": "guanguans",
14 | "email": "ityaozm@gmail.com"
15 | }
16 | ],
17 | "support": {
18 | "issues": "https://github.com/guanguans/yii-pay/issues",
19 | "source": "https://github.com/guanguans/yii-pay"
20 | },
21 | "require": {
22 | "yansongda/pay": "^2.0 || ^3.0",
23 | "yiisoft/yii2": "^2.0"
24 | },
25 | "require-dev": {
26 | "brainmaestro/composer-git-hooks": "^2.8 || ^3.0",
27 | "friendsofphp/php-cs-fixer": "^2.0 || ^3.0",
28 | "mockery/mockery": "^1.3",
29 | "phpunit/phpunit": "^7.0 || ^8.0 || ^9.0 || ^11.0",
30 | "vimeo/psalm": "^3.0 || ^4.0 || ^5.0 || ^6.0"
31 | },
32 | "minimum-stability": "dev",
33 | "autoload": {
34 | "psr-4": {
35 | "Guanguans\\YiiPay\\": "src"
36 | }
37 | },
38 | "autoload-dev": {
39 | "psr-4": {
40 | "Guanguans\\Tests\\": "tests"
41 | }
42 | },
43 | "config": {
44 | "allow-plugins": {
45 | "yiisoft/yii2-composer": true
46 | }
47 | },
48 | "extra": {
49 | "hooks": {
50 | "pre-commit": [
51 | "composer test",
52 | "composer style-lint"
53 | ],
54 | "pre-push": [
55 | "composer test",
56 | "composer style-lint"
57 | ]
58 | }
59 | },
60 | "scripts": {
61 | "post-install-cmd": [
62 | "cghooks add --ignore-lock",
63 | "cghooks update"
64 | ],
65 | "post-update-cmd": [
66 | "cghooks update"
67 | ],
68 | "cghooks": "./vendor/bin/cghooks",
69 | "post-merge": "composer install",
70 | "psalm": "./vendor/bin/psalm",
71 | "style-fix": "php-cs-fixer fix --using-cache=no --config=.php-cs-fixer.php --ansi",
72 | "style-lint": "php-cs-fixer fix --using-cache=no --diff --config=.php-cs-fixer.php --dry-run --ansi",
73 | "test": "./vendor/bin/phpunit --coverage-text --colors=always --verbose",
74 | "test-coverage": "./vendor/bin/phpunit --coverage-html=coverage/ --coverage-clover=clover.xml --color=always --verbose"
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/docs/usage.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/guanguans/yii-pay/b068154a143bd4da6700d8eee25ddf6c2121c0e7/docs/usage.png
--------------------------------------------------------------------------------
/psalm.xml.dist:
--------------------------------------------------------------------------------
1 |
2 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
--------------------------------------------------------------------------------
/src/Pay.php:
--------------------------------------------------------------------------------
1 |
7 | *
8 | * This source file is subject to the MIT license that is bundled.
9 | */
10 |
11 | namespace Guanguans\YiiPay;
12 |
13 | use Yansongda\Pay\Log;
14 | use Yansongda\Pay\Pay as YsdPay;
15 | use yii\base\Component;
16 | use yii\base\InvalidConfigException;
17 |
18 | /**
19 | * @property string $defaultDriver
20 | * @property \Yansongda\Pay\Gateways\Wechat $wechat
21 | * @property \Yansongda\Pay\Gateways\Alipay $alipay
22 | * @property Log $log
23 | *
24 | * @method \Symfony\Component\HttpFoundation\Response app(array $config) APP 支付
25 | * @method \Yansongda\Supports\Collection groupRedpack(array $config) 分裂红包
26 | * @method \Yansongda\Supports\Collection miniapp(array $config) 小程序支付
27 | * @method \Yansongda\Supports\Collection mp(array $config) 公众号支付
28 | * @method \Yansongda\Supports\Collection pos(array $config) 刷卡支付
29 | * @method \Yansongda\Supports\Collection redpack(array $config) 普通红包
30 | * @method \Yansongda\Supports\Collection scan(array $config) 扫码支付
31 | * @method \Yansongda\Supports\Collection transfer(array $config) 企业付款
32 | * @method \Symfony\Component\HttpFoundation\RedirectResponse wap(array $config) H5 支付
33 | * @method \Symfony\Component\HttpFoundation\Response web(array $config) 电脑支付
34 | * @method \Yansongda\Supports\Collection mini(array $config) 小程序支付
35 | *
36 | * @see \Yansongda\Pay\Gateways\Wechat
37 | * @see \Yansongda\Pay\Gateways\Alipay
38 | */
39 | class Pay extends Component
40 | {
41 | /**
42 | * @var string
43 | */
44 | private $defaultDriver;
45 |
46 | /**
47 | * @var array
48 | */
49 | public $wechatOptions = [];
50 |
51 | /**
52 | * @var array
53 | */
54 | public $alipayOptions = [];
55 |
56 | /**
57 | * @var \Yansongda\Pay\Gateways\Wechat
58 | */
59 | private static $_wechat;
60 |
61 | /**
62 | * @var \Yansongda\Pay\Gateways\Alipay
63 | */
64 | private static $_alipay;
65 |
66 | /**
67 | * @var Log
68 | */
69 | private $log;
70 |
71 | /**
72 | * Initializes the object.
73 | * This method is invoked at the end of the constructor after the object is initialized with the given configuration.
74 | */
75 | public function init()
76 | {
77 | parent::init();
78 | $this->log = \Yii::createObject(Log::class);
79 | }
80 |
81 | /**
82 | * @return string
83 | */
84 | public function getDefaultDriver()
85 | {
86 | return $this->defaultDriver;
87 | }
88 |
89 | /**
90 | * @param string $defaultDriver
91 | */
92 | public function setDefaultDriver($defaultDriver)
93 | {
94 | if (! in_array($defaultDriver, ['wechat', 'alipay'])) {
95 | throw new InvalidConfigException("Invalid default driver(wechat/alipay): $defaultDriver");
96 | }
97 |
98 | $this->defaultDriver = $defaultDriver;
99 | }
100 |
101 | /**
102 | * @return \Yansongda\Pay\Gateways\Wechat
103 | */
104 | public function getWechat(array $wechatOptions = [])
105 | {
106 | $wechatOptions and $this->wechatOptions = array_merge($this->wechatOptions, $wechatOptions);
107 | if (! static::$_wechat instanceof \Yansongda\Pay\Gateways\Wechat || $wechatOptions) {
108 | static::$_wechat = YsdPay::wechat($this->wechatOptions);
109 | }
110 |
111 | return static::$_wechat;
112 | }
113 |
114 | /**
115 | * @return \Yansongda\Pay\Gateways\Alipay
116 | */
117 | public function getAlipay(array $alipayOptions = [])
118 | {
119 | $alipayOptions and $this->alipayOptions = array_merge($this->alipayOptions, $alipayOptions);
120 | if (! static::$_alipay instanceof \Yansongda\Pay\Gateways\Alipay || $alipayOptions) {
121 | static::$_alipay = YsdPay::alipay($this->alipayOptions);
122 | }
123 |
124 | return static::$_alipay;
125 | }
126 |
127 | /**
128 | * @return object|Log
129 | *
130 | * @throws InvalidConfigException
131 | */
132 | public function getLog()
133 | {
134 | return $this->log;
135 | }
136 |
137 | /**
138 | * @param string $method
139 | * @param array $arguments
140 | *
141 | * @throws InvalidConfigException
142 | */
143 | public function __call($method, $arguments)
144 | {
145 | if (is_null($this->defaultDriver)) {
146 | throw new InvalidConfigException('The default driver is not set.');
147 | }
148 |
149 | return call_user_func_array([$this->{$this->defaultDriver}, $method], $arguments);
150 | }
151 | }
152 |
--------------------------------------------------------------------------------