Compare commits

..

No commits in common. "6.x" and "4.x" have entirely different histories.
6.x ... 4.x

131 changed files with 7065 additions and 14128 deletions

3
.github/FUNDING.yml vendored
View File

@ -1 +1,2 @@
custom: https://btcpayserver.org/donate/
# These are supported funding model platforms
custom: https://foundation.btcpayserver.org

38
.github/ISSUE_TEMPLATE/bug-report.md vendored Normal file
View File

@ -0,0 +1,38 @@
---
name: Bug report
about: File a bug report
title: ''
labels: ''
assignees: ''
---
**Describe the bug**
A clear and concise description of what the bug is.
**To Reproduce**
Steps to reproduce the behavior:
1. Go to '...'
2. Click on '....'
3. Scroll down to '....'
4. See error
**Expected behavior**
A clear and concise description of what you expected to happen.
**Screenshots**
If applicable, add screenshots to help explain your problem.
**Your BTCPay Environment (please complete the following information):**
- BTCPay Server Version [available in the right bottom corner of footer]
- Deployment Method: [e.g. Docker, Manual, Third-Party-hoist]
- Browser [e.g. chrome, safari]
**Logs (if applicable)**
Basic logs can be found in Server Settings > Logs. More logs https://docs.btcpayserver.org/Troubleshooting/#2-looking-through-the-logs
**Setup Parameters**
If you're reporting a deployment issue run `. btcpay-setup.sh -i` and paste your the parameters by obscuring private information.
**Additional context**
Add any other context about the problem here.

View File

@ -1,79 +0,0 @@
name: 🐛 Bug Report
description: File a bug report
title: "[Bug]: "
labels: ["bug"]
body:
- type: markdown
attributes:
value: |
### ❗️ Read this before submitting your bug report:
- **Provide as much detail as possible** - error logs, screenshots, your exact configuration. If the issue cannot be reproduced, it cannot be fixed.
- **Avoid reporting "error 500" or "white page" errors** - this is a universal error message that does not provide enough information to qualify the issue. Enable debug mode in the Performance section of PrestaShop or manually in `/config/defines.inc.php` and try again. You should get a proper error message.
- Thank you for taking the time to fill out this bug report!
- type: textarea
id: version
attributes:
label: What is your BTCPay Server version?
description: You can see the version in the footer's bottom right corner
placeholder: I'm running BTCPay v1.X.X.X
validations:
required: true
- type: textarea
id: deployment
attributes:
label: How did you deploy BTCPay Server?
description: Docker, manual, third-party host? Read more on deployment methods [here](https://docs.btcpayserver.org/Deployment/)
placeholder: I'm running BTCPay Server on a...
validations:
required: true
- type: textarea
id: module-version
attributes:
label: What is version of the PrestaShop plugin are you using?
description: You can see the version in the header of the configuration page or in the module list
placeholder: I'm running BTCPay v1.X.X.X
validations:
required: true
- type: textarea
id: what-happened
attributes:
label: What happened?
description: A clear and concise description of what the bug is.
placeholder: Tell us what you see!
validations:
required: true
- type: textarea
id: reproduce
attributes:
label: How did you encounter this bug?
description: Step by step describe how did you encounter the bug?
placeholder: 1. I clicked X 2. Then I clicked Y 3. See error
validations:
required: true
- type: textarea
id: logoutput
attributes:
label: Relevant log output
description: Please copy and paste any relevant log output. This will be automatically formatted into code, so no need for backticks. PrestaShop logs can be found in Advanced Parameters > Logs.
render: shell
- type: textarea
id: browser
attributes:
label: What browser do you use (if useful)?
description: Provide your browser and it's version. If you replicated issues on multiple browsers, let us know which ones.
placeholder: For example Safari 15.00, Chrome 10.0, Tor, Edge, etc
validations:
required: false
- type: textarea
id: additonal
attributes:
label: Additional information
description: Feel free to provide additional information. Screenshots are always helpful.
- type: checkboxes
id: terms
attributes:
label: Are you sure this is a bug report?
description: By submitting this report, you agree that this is not a support or a feature request. For general questions please read our [documentation](https://docs.btcpayserver.org). You can ask questions [on our community chat](https://chat.btcpayserver.org)
options:
- label: I confirm this is a bug report
required: true

View File

@ -1,17 +1,5 @@
blank_issues_enabled: true
blank_issues_enabled: false
contact_links:
- name: 💡 Request a feature
url: https://github.com/btcpayserver/prestashop-plugin/discussions/categories/ideas-feature-requests
about: Submit a feature request or vote on ideas posted by others. Features with most upvotes become roadmap candidates
- name: 🧑‍💻 Ask a technical question
url: https://github.com/btcpayserver/prestashop-plugin/discussions/new?category=technical-support
about: If you're experiencing a technical problem post it to our community support forum
- name: 📝 Official Documentation
url: https://docs.btcpayserver.org
about: Check our documentation for answers to common questions
- name: 💬 Community Support Chat
- name: Community Support Chat
url: https://chat.btcpayserver.org/
about: Ask general questions and get community support in real-time
- name: 🙏 PrestaShop Support
url: https://www.prestashop-project.org/support/
about: Read up on how you can get support for your PrestaShop store
about: Ask general questions and get community support in real-time.

View File

@ -0,0 +1,23 @@
---
name: Feature request
about: Suggest a new feature or enhancement
title: ''
labels: ''
assignees: ''
---
**Is your feature request related to a problem? Please describe.**
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]
**Describe the solution you'd like**
A clear and concise description of what you want to happen.
**Sketch/Image/Wireframe/Mockup**
If applicable provide examples, wireframes, sketches or images to better explain your idea.
**Describe alternatives you've considered**
A clear and concise description of any alternative solutions or features you've considered.
**Additional context**
Add any other context or screenshots about the feature request here.

19
.github/PULL_REQUEST_TEMPLATE.md vendored Normal file
View File

@ -0,0 +1,19 @@
<!-----------------------------------------------------------------------------
Thank you for contributing to the BTCPay plugin!
Please take the time to edit the "Answers" rows below with the necessary information.
Check out our contribution guidelines to find out how to complete it:
https://devdocs.prestashop.com/1.7/contribute/contribution-guidelines/#pull-requests
------------------------------------------------------------------------------>
| Questions | Answers
| ------------- | -------------------------------------------------------
| Description? | Please be specific when describing the PR. <br> Every detail helps: versions, browser/server configuration, specific module/theme, etc. Feel free to add more information below this table.
| Type? | bug fix / improvement / new feature / refacto / critical
| BC breaks? | yes / no
| Deprecations? | yes / no
| Fixed ticket? | Fixes btcpayserver/prestashop-plugin#{issue number here}.
| How to test? | Please indicate how to best verify that this PR is correct.
<!-- Click the form's "Preview" button to make sure the table is functional in GitHub. Thank you! -->

View File

@ -1,40 +0,0 @@
version: 2
updates:
# Ensure root composer is up-to-date
- package-ecosystem: composer
directory: /
schedule:
interval: daily
cooldown:
default-days: 7
open-pull-requests-limit: 5
reviewers:
- BitcoinMitchell
labels:
- dependencies
# Ensure module composer is up-to-date
- package-ecosystem: composer
directory: /modules/btcpay
schedule:
interval: daily
cooldown:
default-days: 7
open-pull-requests-limit: 5
reviewers:
- BitcoinMitchell
labels:
- dependencies
# Ensure workflows are up-to-date
- package-ecosystem: github-actions
directory: "/.github/workflows"
schedule:
interval: daily
cooldown:
default-days: 7
open-pull-requests-limit: 5
reviewers:
- BitcoinMitchell
labels:
- dependencies

View File

@ -1,35 +0,0 @@
# Description
Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.
- Fixes # (issue)
- Depends on # (issue or pr)
# Type of change
Please delete options that are not relevant.
- [ ] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
- [ ] Refactor (non-breaking change which improves the codebase)
# How Has This Been Tested?
Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration
- [ ] Test A
- [ ] Test B
**Test Configuration**:
* BTCPay Server version:
* PrestaShop version:
* PHP version:
# Checklist:
- [ ] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [ ] I have checked my code and corrected any misspellings

View File

@ -4,33 +4,27 @@ on:
push:
tags: [ 'v*' ] # Push events to matching v*, i.e. v1.0, v20.15.10
permissions: {}
jobs:
linting:
name: "Release a new ZIP"
runs-on: "ubuntu-latest"
steps:
- name: "Checkout"
uses: "actions/checkout@c2d88d3ecc89a9ef08eebf45d9637801dcee7eb5" # v6.0.0
with:
persist-credentials: false
uses: "actions/checkout@v2"
- name: "Setup PHP, with composer and extensions"
uses: "shivammathur/setup-php@44454db4f0199b8b9685a5d763dc37cbf79108e1" # v2.36.0
uses: "shivammathur/setup-php@v2" # https://github.com/shivammathur/setup-php
with:
php-version: "8.0"
php-version: "7.3"
extensions: "mbstring, xml, ctype, iconv, intl, gd"
tools: "composer:v2"
- name: "Get composer cache directory"
id: "composer-cache"
run: 'echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT'
id: "composercache"
run: "echo '::set-output name=dir::$(composer config cache-files-dir)'"
- name: "Cache composer dependencies"
uses: "actions/cache@9255dc7a253b0ccc959486e2bca901246202afeb" # v5.0.1
uses: "actions/cache@v2"
with:
path: "${{ steps.composer-cache.outputs.dir }}"
path: "${{ steps.composercache.outputs.dir }}"
key: "${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}"
restore-keys: "${{ runner.os }}-composer-"
lookup-only: true
- name: "Validate composer"
run: "composer validate"
- name: "Install Composer dependencies"
@ -40,14 +34,24 @@ jobs:
- name: "Make zip"
run: "make"
# Make a release from the tag and upload the zip
# Make a release from the tag
- name: "Create Release"
id: "create_release"
uses: "softprops/action-gh-release@a06a81a03ee405af7f2048a818ed3f03bbf83c7b" # v2.5.0
uses: "actions/create-release@v1"
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" # This token is provided by Actions, you do not need to create your own token
with:
tag_name: "${{ github.ref }}"
release_name: "Release ${{ github.ref }}"
# Upload the zip to the release
- name: "Upload Release Asset"
id: "upload-release-asset"
uses: "actions/upload-release-asset@v1"
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
with:
name: "Release ${{ github.ref_name }}"
files: "./build/btcpay.zip"
fail_on_unmatched_files: true
generate_release_notes: true
upload_url: "${{ steps.create_release.outputs.upload_url }}" # This pulls from the CREATE RELEASE step above, referencing it's ID to get its outputs object, which include a `upload_url`. See this blog post for more info: https://jasonet.co/posts/new-features-of-github-actions/#passing-data-to-future-steps
asset_path: "./build/btcpay.zip"
asset_name: "btcpay.zip"
asset_content_type: "application/zip"

View File

@ -1,26 +0,0 @@
name: GitHub Actions Security Analysis with zizmor 🌈
on:
push:
branches: ["main"]
pull_request:
branches: ["**"]
permissions: {}
jobs:
zizmor:
name: Run zizmor 🌈
runs-on: ubuntu-latest
permissions:
security-events: write
contents: read # only needed for private repos
actions: read # only needed for private repos
steps:
- name: Checkout repository
uses: actions/checkout@c2d88d3ecc89a9ef08eebf45d9637801dcee7eb5 # v6.0.0
with:
persist-credentials: false
- name: Run zizmor 🌈
uses: zizmorcore/zizmor-action@e639db99335bc9038abc0e066dfcd72e23d26fb4 # v0.3.0

View File

@ -1,43 +1,32 @@
name: "Validate"
on:
push:
paths: [ 'modules/**' ]
branches: [ '6.x' ]
tags: [ 'v*' ]
pull_request:
paths: [ 'modules/**' ]
permissions: {}
on: [ "push", "pull_request" ]
jobs:
linting:
name: "Linting (PHP ${{ matrix.php-versions }})"
runs-on: "ubuntu-latest"
strategy:
fail-fast: false
matrix:
php-versions: [ '8.0', '8.1' ]
php-versions: [ '7.3', '7.4', '8.0' ]
steps:
- name: "Checkout"
uses: "actions/checkout@c2d88d3ecc89a9ef08eebf45d9637801dcee7eb5" # v6.0.0
with:
persist-credentials: false
uses: "actions/checkout@v2"
- name: "Setup PHP, with composer and extensions"
uses: "shivammathur/setup-php@44454db4f0199b8b9685a5d763dc37cbf79108e1" # v2.36.0
uses: "shivammathur/setup-php@v2" # https://github.com/shivammathur/setup-php
with:
php-version: "${{ matrix.php-versions }}"
extensions: "mbstring, xml, ctype, iconv, intl, gd"
tools: "composer:v2"
- name: "Get composer cache directory"
id: "composer-cache"
run: 'echo "dir=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT'
id: "composercache"
run: "echo '::set-output name=dir::$(composer config cache-files-dir)'"
- name: "Cache composer dependencies"
uses: "actions/cache@9255dc7a253b0ccc959486e2bca901246202afeb" # v5.0.1
uses: "actions/cache@v2"
with:
path: "${{ steps.composer-cache.outputs.dir }}"
path: "${{ steps.composercache.outputs.dir }}"
key: "${{ runner.os }}-composer-${{ hashFiles('**/composer.lock') }}"
restore-keys: "${{ runner.os }}-composer-"
lookup-only: true
- name: "Validate composer"
run: "composer validate"
- name: "Install Composer dependencies"

5
.gitignore vendored
View File

@ -8,18 +8,13 @@ btcpay.zip
# Ignore php-cs-fixer cache
.php_cs.cache
.php-cs-fixer.cache
# Ignore phpcs cache
.phpcs-cache
# Ignore developer specific docker-compose
docker-compose.override.yml
# Ignore files in the module that are added via `make`
/modules/btcpay/LICENSE
/modules/btcpay/README.md
/modules/btcpay/docs/README.md
# We dont care about the lib/vendor folder
/modules/btcpay/lib/

View File

@ -16,7 +16,6 @@ return (new PhpCsFixer\Config())
'concat_space' => false,
'fopen_flags' => false,
'native_function_invocation' => false,
'native_constant_invocation' => false,
'phpdoc_summary' => false,
'protected_to_private' => false,
'psr_autoloading' => false,

View File

@ -1 +0,0 @@
8.0

View File

@ -1,8 +0,0 @@
[![Twitter Follow](https://img.shields.io/twitter/follow/btcpayserver?color=brightgreen&label=Follow%20%40BTCPayServer&style=flat-square)](https://twitter.com/btcpayserver)
[![BTCPay Server Community](https://img.shields.io/badge/chat-mattermost-brightgreen?style=flat-square)](https://chat.btcpayserver.org/btcpayserver)
BTCPay is built and maintained entirely by volunteer contributors around the internet. We welcome and appreciate new contributions.
Contributors looking to help out, before opening a pull request, please [create an issue](https://github.com/btcpayserver/prestashop-plugin/issues/new/choose)
or join [our community chat](https://chat.btcpayserver.org) to get early feedback, discuss best ways to tackle the problem and to ensure there is no work duplication.

View File

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2017-2025 btcpayserver
Copyright (c) 2017-2021 btcpayserver
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View File

@ -3,49 +3,22 @@ MODULE_FOLDER := "./modules"
BUILD_FOLDER := "./build"
ZIP_NAME := "${MODULE}.zip"
ZIP_DEBUG_NAME := "${MODULE}_debug.zip"
MODULE_OUT := "${BUILD_FOLDER}/${ZIP_NAME}"
.PHONY: all build install update upgrade clean lint lint-fix
.PHONY: all deps build clean lint lint-fix
all: build
build: ## Build the bastard binary file
# Installing all dependencies
@cd "$(MODULE_FOLDER)/$(MODULE)" \
&& composer install --no-dev
# Dump autoloader
@cd "$(MODULE_FOLDER)/$(MODULE)" \
&& composer dump-autoload -o --no-dev
# Removing the old ZIP if present
@rm -f $(MODULE_OUT)
# Make the build folder
@mkdir -p $(BUILD_FOLDER)
# Copy the license to the module
@cp ./LICENSE "$(MODULE_FOLDER)/$(MODULE)"
# Copy the README to the module root and docs
@cp ./README.md "$(MODULE_FOLDER)/$(MODULE)"
@cp ./README.md "$(MODULE_FOLDER)/$(MODULE)/docs"
# Zip the module
@cd $(MODULE_FOLDER) \
&& zip -r $(ZIP_NAME) $(MODULE) \
&& mv $(ZIP_NAME) "../$(BUILD_FOLDER)"
debug: ## Build the bastard binary file as debug file
deps: ## Download and make all dependencies
# Installing all dependencies
@cd "$(MODULE_FOLDER)/$(MODULE)" \
&& composer install
# Dump autoloader
@cd "$(MODULE_FOLDER)/$(MODULE)" \
&& composer dump-autoload -o
&& composer dump-autoload -o --no-dev
build: deps ## Build the bastard binary file
# Removing the old ZIP if present
@rm -f $(MODULE_OUT)
@ -60,40 +33,8 @@ debug: ## Build the bastard binary file as debug file
# Zip the module
@cd $(MODULE_FOLDER) \
&& zip -r $(ZIP_DEBUG_NAME) $(MODULE) \
&& mv $(ZIP_DEBUG_NAME) "../$(BUILD_FOLDER)"
bump: ## Bump all package versions
# Bump all root dependencies
@composer install
# Bump all module dependencies
@cd "$(MODULE_FOLDER)/$(MODULE)" \
&& composer install
install: ## Install everything for development
# Installing all root dependencies
@composer install
# Installing all module dependencies
@cd "$(MODULE_FOLDER)/$(MODULE)" \
&& composer install
update: ## Update all dependencies (including development)
# Upgrading all root dependencies
@composer update
# Upgrading all module dependencies
@cd "$(MODULE_FOLDER)/$(MODULE)" \
&& composer update
upgrade: ## Upgrade all dependencies (including development)
# Upgrading all root dependencies
@composer upgrade
# Upgrading all module dependencies
@cd "$(MODULE_FOLDER)/$(MODULE)" \
&& composer upgrade
&& zip -r $(ZIP_NAME) $(MODULE) \
&& mv $(ZIP_NAME) "../$(BUILD_FOLDER)"
clean: ## Remove previous builds
# Removing the ZIP
@ -110,17 +51,14 @@ lint: ## Lints the module
@./vendor/bin/php-cs-fixer fix --diff --dry-run -v
# Run PHPCS
@./vendor/bin/phpcs --cache -p
# Run PHP Parallel Lint
@./vendor/bin/parallel-lint --exclude ./modules/btcpay/vendor ./modules/btcpay
@./vendor/bin/phpcs
lint-fix: ## Resolves linter issues
# Run PHP CS Fixer
@./vendor/bin/php-cs-fixer fix -v
# Run PHPCBF
@./vendor/bin/phpcbf --cache -p
@./vendor/bin/phpcbf -p
help: ## Display this help screen
@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

105
README.md
View File

@ -1,94 +1,71 @@
# BTCPay Server - Prestashop Plugin
# Prestashop Plugin
![BTCPay Server](https://raw.githubusercontent.com/btcpayserver/btcpayserver/master/BTCPayServer/wwwroot/img/btc_pay_BG_twitter.png)
[![Maintained](https://img.shields.io/maintenance/yes/2021?style=flat-square)](https://github.com/btcpayserver/prestashop-plugin/pulse)
[![GitHub Workflow Status](https://img.shields.io/github/workflow/status/btcpayserver/prestashop-plugin/Validate/master?style=flat-square)](https://github.com/btcpayserver/prestashop-plugin/actions)
[![GitHub License](https://img.shields.io/github/license/btcpayserver/prestashop-plugin?color=brightgreen&style=flat-square)](https://github.com/btcpayserver/prestashop-plugin/blob/master/LICENSE)
[![GitHub contributors](https://img.shields.io/github/contributors-anon/btcpayserver/prestashop-plugin?style=flat-square)](https://github.com/btcpayserver/prestashop-plugin/graphs/contributors)
<h3 align="center">
Accept Bitcoin payments within PrestaShop
</h3>
[![PrestaShop module version](https://img.shields.io/badge/module%20version-4.0.2-brightgreen?style=flat-square)](https://github.com/btcpayserver/prestashop-plugin/releases)
[![GitHub release (latest SemVer)](https://img.shields.io/github/v/release/btcpayserver/prestashop-plugin?sort=semver&style=flat-square)](https://github.com/btcpayserver/prestashop-plugin/releases)
[![GitHub all releases](https://img.shields.io/github/downloads/btcpayserver/prestashop-plugin/total?style=flat-square)](https://github.com/btcpayserver/prestashop-plugin/releases)
<div align="center">
<p>
<a href="https://github.com/btcpayserver/prestashop-plugin/pulse"><img src="https://img.shields.io/maintenance/yes/2025?style=flat-square" alt="Maintained"/></a>
<a href="https://github.com/btcpayserver/prestashop-plugin/actions"><img src="https://img.shields.io/github/actions/workflow/status/btcpayserver/prestashop-plugin/validate.yml?style=flat-square" alt="GitHub Workflow Status"/></a>
<a href="https://github.com/btcpayserver/prestashop-plugin/blob/6.x/LICENSE"><img src="https://img.shields.io/github/license/btcpayserver/prestashop-plugin?color=brightgreen&amp;style=flat-square" alt="GitHub License"/></a>
<a href="https://github.com/btcpayserver/prestashop-plugin#contributing"><img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square" alt="PRs are welcome"/></a>
<a href="https://github.com/btcpayserver/prestashop-plugin/graphs/contributors"><img src="https://img.shields.io/github/contributors-anon/btcpayserver/prestashop-plugin?style=flat-square" alt="GitHub contributors"/></a>
</p>
<p>
<a href="https://github.com/btcpayserver/prestashop-plugin/releases"><img src="https://img.shields.io/github/v/release/btcpayserver/prestashop-plugin?sort=semver&amp;style=flat-square" alt="GitHub release (latest SemVer)"/></a>
<a href="https://github.com/btcpayserver/prestashop-plugin/releases"><img src="https://img.shields.io/github/downloads/btcpayserver/prestashop-plugin/total?style=flat-square" alt="GitHub all releases"/></a>
</p>
</div>
<div align="center">
<sub>"This is lies, my trust in you is broken, I will make you obsolete" 💚
</a>
</div>
<br/>
This is a Bitcoin payment plugin for PrestaShop using BTCPay server. [BTCPay Server](https://btcpayserver.org) is a free and open source server for merchants wanting to accept Bitcoin for their business.
<p align="center">
<a href="https://github.com/btcpayserver/prestashop-plugin/issues/new/choose">Report a bug</a>
·
<a href="https://github.com/btcpayserver/prestashop-plugin/discussions/new/choose">Request a feature</a>
·
<a href="https://docs.btcpayserver.org/FAQ/">FAQ</a>
</p>
**Warning**: This version should be considered as beta. Use it at your own risk.
---
## Tested successfully
* Prestashop 1.7.7.x
* BTCPay server v1.0.6.4
## 🔧 Requirements
**Note**: The legacy version can be found at [prestashop-plugin-legacy](https://github.com/btcpayserver/prestashop-plugin-legacy) and will not recieve any further support at this time.
Please ensure that you meet the following requirements before installing this plugin.
That said, this module has `v4` as it's module version and includes migration scripts to try and migrate your old module to this new version. The module name has been kept as is, so upgrading should be easy.
- You are using PHP 8.0 or higher
- Your PrestaShop is version 8.0/9.0 or higher.
- Your BTCPay Server is version 1.7.0 or higher
- The PDO, curl, gd, intl, json, and mbstring PHP extensions are available
- You have a BTCPay Server, either [self-hosted](https://docs.btcpayserver.org/Deployment/) or [hosted by a third party](https://docs.btcpayserver.org/Deployment/ThirdPartyHosting/)
- [You've a registered account on the instance](https://docs.btcpayserver.org/RegisterAccount)
- [You've a BTCPay store on the instance](https://docs.btcpayserver.org/CreateStore)
- [You've a wallet connected to your store](https://docs.btcpayserver.org/WalletSetup)
## Internals
### Tested successfully
- Prestashop v8.0, v8.0.1, v8.0.4, v8.1.0, v8.1.3, v8.1.4, v8.2.1, v9.0.0
- BTCPay server v1.7.0, v1.7.3.0, v1.12.5, v1.13.0, v1.13.5, v2.1.5, v2.2.1
- BTCPay server v2 is supported as of module version v6.3.0.
This plugin support creating orders before or after payment. That said the `before`-mode still requires a callback from the BTCPay server itself so that all relevant data is available during order creation.
### Multistore
## Future improvements
There is a lot of improvements that can still be made:
* Create a docker container for testing
* Rewrite to use the new [Greenfield Invoice API](https://docs.btcpayserver.org/API/Greenfield/v1/).
As of right now the module is **not** compatible with Prestashop's multistore feature. Any help to resolve this is very much welcomed. Please see the relevant [discussion](https://github.com/btcpayserver/prestashop-plugin/discussions/130).
## Documentation
## 📗 Documentation
Please check out our [official website](https://btcpayserver.org/), our [complete documentation](https://docs.btcpayserver.org/) and [FAQ](https://docs.btcpayserver.org/FAQ/) for more details.
Please check out our [official website](https://btcpayserver.org/), [complete documentation](https://docs.btcpayserver.org/) and [FAQ](https://docs.btcpayserver.org/FAQ/) for more details.
If you have trouble using BTCPay, consider joining [communities listed on official website](https://btcpayserver.org/#communityCTA) to get help from BTCPay community members. Only file [Github issue](https://github.com/btcpayserver/prestashop-plugin/issues) for technical issues you can't resolve through other channels or feature requests you've validated with other members of community.
If you have trouble using BTCPay Server, consider joining [communities listed on the official website](https://btcpayserver.org/#communityCTA) to get help from other contributors. Only create a [GitHub issue](https://github.com/btcpayserver/prestashop-plugin/issues/new/choose) for technical issues you can't resolve through other channels or feature requests you've validated with other members of the community.
Main community chat is located on [Mattermost](https://chat.btcpayserver.org/).
## 🚀 Quick Start Guide
### Quick Start Guide
To get up and running with our plugin quickly, see the [PrestaShop Guide on our documentation website](https://docs.btcpayserver.org/PrestaShop/).
## 🧑‍💻 Versioning
## Versioning
We use [SemVer](http://semver.org/) for versioning. For the versions available, see the [releases within this repository](https://github.com/btcpayserver/prestashop-plugin/releases).
We use [SemVer](http://semver.org/) for versioning. For the versions available, see the
[tags on this repository](https://github.com/BitcoinMitchell/docker-prestashop/tags).
## 🤝 Contributing
## Contributing
BTCPay Server is built and maintained entirely by volunteer contributors around the internet. We welcome and appreciate new contributions.
[![Twitter Follow](https://img.shields.io/twitter/follow/btcpayserver?color=brightgreen&label=Follow%20%40BTCPayServer&style=flat-square)](https://twitter.com/btcpayserver)
[![BTCPay Server Community](https://img.shields.io/badge/chat-mattermost-brightgreen?style=flat-square)](https://chat.btcpayserver.org/btcpayserver)
Contributors looking to help out, before opening a pull request, please join [our community chat](https://chat.btcpayserver.org/) or [start a GitHub discussion](https://github.com/btcpayserver/btcpayserver/discussions) to get early feedback, discuss the best ways to tackle the problem, and ensure there is no work duplication.
BTCPay is built and maintained entirely by volunteer contributors around the internet. We welcome and appreciate new contributions.
### 🔧 Development
Contributors looking to help out, before opening a pull request, please [create an issue](https://github.com/btcpayserver/prestashop-plugin/issues/new/choose)
or join [our community chat](https://chat.btcpayserver.org) to get early feedback, discuss best ways to tackle the problem and to ensure there is no work duplication.
We recommend using [PrestaShop Kickstarter](https://github.com/PrestaShopCorp/docker-compose-kickstarter) (which supports [ngrok](https://ngrok.com/)) and a dockerized [BTCPay Server instance](https://github.com/btcpayserver/btcpayserver-docker/blob/master/Production/docker-compose.btc.yml) (or [an online testnet instance](https://testnet.demo.btcpayserver.org/)) for testing locally.
## PrestaShop Support
## 🏪 PrestaShop Support
PrestaShop support can be found through it's official channels.
PrestaShop support can be found through its official channels.
* [Homepage](https://www.prestashop.com)
* [Documentation](https://doc.prestashop.com)
* [Support Forums](https://www.prestashop.com/forums)
* [Documentation](https://docs.prestashop-project.org/v.8-documentation) - contains answers to commonly asked questions and provides instructions for almost everything. Including setup, management, etc.
* [Support Forums](https://www.prestashop.com/forums) - the official Prestashop forum is a good place to find further answers and ask Prestashop related questions.
## 📝 License
## License
BTCPay Server software, logo and designs are provided under [MIT License](LICENSE).

View File

@ -1,74 +1,7 @@
# Reporting a potential Vulnerability.
<!-- Short intro. -->
We take the security of our project seriously, and we encourage responsible disclosure of any vulnerabilities that may be found. To facilitate this process, we have established the following vulnerability reporting process.
Security issues and bugs should be reported privately, via email. To report a security issue, please send an email to **security@btcpayserver.org** (not for support).
We appreciate your efforts to disclose your findings responsibly.
You will receive a reply indicating the next steps in handling your report. If for some reason you do not receive a reply within 24 hours, please follow up via email to ensure the original message was received.
##### 1. Reporting Channel
If you believe you have discovered a vulnerability in our project, please email us at `security@btcpayserver.org`. Alternatively, you may report the vulnerability to us through [huntr.dev](https://huntr.dev/repos/btcpayserver/btcpayserver/).
After the initial reply to your report, you will be informed of the progress towards a fix and full announcement. You may be asked to provide additional information or guidance.
Please allow for up to 2 business days for an acknowledgement of receipt. If you receive no response within 2 business days, please follow up via email to ensure the original message was received.
Upon review of your report, you may be asked to provide additional information or guidance.
<!--TODO: If available, add link to PGP key used to read security report emails.-->
##### 2. In-Scope
<!-- What's in scope? Any repo in our org for example. -->
We welcome reports of vulnerabilities in repositories owned by the [BTCPay Server GitHub Organization](https://github.com/btcpayserver). This includes any issues related to the confidentiality, integrity, or availability of systems or data in these systems.
##### 3. Out of Scope
<!-- What's out of scope? Thinking here about custom deployments, plugins that are not created by BTCPay (this includes kukks plugins that should be reported to him directly). -->
1. Any BTCPay Server deployment that has been customized in any way. To facilitate reproducibility, please verify that the BTCPay Server instance is based on the un-altered source-code or [Docker deployment](https://docs.btcpayserver.org/Docker/).
2. Any BTCPay Server plugin that is not authored by `btcpayserver` as stated by the author tag in-app.
##### 4. Preferred Reporting Template
<!-- Template example to guide reporter into including specific info that we'd appreciate be included in the report. -->
We encourage the use of a reporting template that includes a detailed description of the vulnerability, any evidence or proof of concept, and steps to reproduce the vulnerability.
Please find an example of an email template [at the end of this document](#7-reporting-template-example).
##### 5. Timeline for Remediation
<!-- Tentative 90 business day timeline for resolution. This is a typical industry standard, but have included wording to include the fact that we're a team of volunteers, and that we cannot guarantee it. -->
While we will work to remediate the reported vulnerability within 90 business days from the acknowledgment of the report, being a team of volunteers, we cannot guarantee this timeline to be accurate at all time.
We will provide regular updates to the reporter until the vulnerability is resolved.
##### 6. Timeline to Public Disclosure
<!-- No tentative timeline, given it can differ based on multiple criteria, but we have to take into account the fact that a public disclosure of a full year is too much. Security by obscurity is rarely beneficial, especially for the uninformed end-users. -->
We will work with the reporter to define a suitable timeline to public disclosure once the vulnerability is remediated.
<!--
##### 7. More information
For more information on our complete vulnerability response process, please see our [documentation]()
-->
##### 7. Reporting Template Example
<!-- Simple template for users to take as example for vulnerability reporting. Contains the basic minimum information that we need to assess promptly a report. -->
Feel free to use the below template to report a vulnerability.
```
Subject: Vulnerability Report - BTCPay Server
Dear BTCPay Server team,
I am writing to report a security vulnerability that I have identified in BTCPay Server. I believe this vulnerability poses a significant threat to the security of the project and its users.
Here are the details of the vulnerability:
* Vulnerability description: [Provide a clear and concise description of the vulnerability]
* Impact: [Describe the potential impact of the vulnerability, ie. any potential consequences for the project, its users, or any third parties]
* Affected version(s): [Specify which version(s) of the project are affected by the vulnerability]
* Steps to reproduce & Proof of Concept: [Provide a step-by-step guide to reproduce the vulnerability, including any screenshots and code snippets you feel would help]
* Severity: [Provide your assessment of the severity of the vulnerability, using a scale such as Warning/Low/Medium/High/Critical]
* Mitigation or Fix: [Provide your recommendation for a solution or mitigation strategy for the vulnerability]
If needed, I [agree/do not agree] to be invited into a Github private fork for the purpose of helping resolve this vulnerability. [Please include a link to your github profile]
Please let me know if you need any further information or if you would like to discuss this vulnerability in more detail.
Thank you for your attention to this matter.
Sincerely,
[Your Name/Handle]
```
We appreciate your efforts to responsibly disclose your findings, and will make every effort to acknowledge your contributions.

View File

@ -1,40 +0,0 @@
# Upgrading from 4.0 to 5.0
Prestashop modules come with an auto-upgrade feature which will be used to upgrade your currently running plugin. This documents serves as a reference for what has been changed.
## Requirements
The module requirements have been changed to:
- You are using PHP 7.3.0 or higher
- Your PrestaShop is version 1.7.7.0 or higher.
- Your BTCPay Server is version 1.3.0 or higher
## Database
We now fetch all invoice and payment data directly from BTCPay Server so the following columns will be removed from `bitcoin_payment` during the upgrade:
- `bitcoin_price`
- `bitcoin_paid`
- `bitcoin_address`
- `bitcoin_refund_address`
- `rate`
## Different PHP client
We switched from [`btcpayserver-php-client`] to the newer and actively supported [`btcpayserver-greenfield-php`]. This means that the old `IPN` has been replaced with an `webhook` based approach.
For you this means that **_you'll have to re-connect_** to your BTCPay Server instance.
## Invoice
On the front-end side of things we now show the customer how much they have transferred and via what payment.
### Order states
The order states have received a new translation so that they are more correct:
- `Awaiting Bitcoin payment` has been changed to `Awaiting crypto payment`.
- `Waiting for Bitcoin confirmations` has been changed to `Waiting for confirmations`.
- `Bitcoin transaction failed` has been changed to `Crypto transaction failed`.
- `Paid with Bitcoin` has been changed to `Paid with crypto`.
[`btcpayserver-php-client`]: https://github.com/btcpayserver/btcpayserver-php-client
[`btcpayserver-greenfield-php`]: https://github.com/btcpayserver/btcpayserver-greenfield-php

View File

@ -1,10 +0,0 @@
# Upgrading from 5.0 to 6.0
Prestashop modules come with an auto-upgrade feature which will be used to upgrade your currently running plugin. This documents serves as a reference for what has been changed.
## Requirements
The module requirements have been changed to:
- You are using PHP 8.0 or higher
- Your PrestaShop is version 8.0 or higher.
- Your BTCPay Server is version 1.7.0 or higher

View File

@ -1,28 +1,20 @@
{
"name": "btcpay/prestashop-plugin",
"description": "BTCPay Server payment plugin for PrestaShop",
"license": "MIT",
"type": "prestashop-module",
"description": "BTCPay payment plugin for PrestaShop",
"keywords": [
"BTCPay",
"BTCPay Server",
"bitcoin",
"crypto",
"ecommerce",
"module",
"payment",
"prestashop",
"plugin",
"prestashop"
"payment",
"ecommerce",
"bitcoin",
"BTCPay"
],
"homepage": "https://github.com/btcpayserver/prestashop-plugin",
"support": {
"issues": "https://github.com/btcpayserver/prestashop-plugin/issues",
"source": "https://github.com/btcpayserver/prestashop-plugin"
},
"license": "MIT",
"require": {
"php": ">=8.0",
"php": ">=7.3.0",
"ext-PDO": "*",
"ext-bcmath": "*",
"ext-curl": "*",
"ext-dom": "*",
"ext-fileinfo": "*",
@ -32,43 +24,25 @@
"ext-json": "*",
"ext-mbstring": "*",
"ext-zip": "*",
"composer/installers": "^1.12.0"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^1.0.0",
"ergebnis/composer-normalize": "^2.43.0",
"ezyang/htmlpurifier": "dev-master as 4.17.0",
"friendsofphp/php-cs-fixer": "^3.4.0",
"lcobucci/jwt": "dev-3.4.6-patch as 3.4.6",
"php-parallel-lint/php-console-highlighter": "^1.0",
"php-parallel-lint/php-parallel-lint": "^1.4.0",
"phpoffice/phpspreadsheet": "^1.29.1",
"prestashop/autoindex": "^2.1",
"prestashop/php-dev-tools": "^5",
"prestashop/prestashop": "^8.1.7",
"roave/security-advisories": "dev-latest",
"slevomat/coding-standard": "^8.15.0",
"squizlabs/php_codesniffer": "^3.10.2"
"composer/installers": "^v1.9.0"
},
"replace": {
"prestashop/blockreassurance": "^5.1.4",
"prestashop/blockwishlist": "^3",
"prestashop/classic": "^2.0.0-beta",
"prestashop/contactform": "^4.3.0",
"prestashop/blockwishlist": "^2.0",
"prestashop/contactform": "^v4.3.0",
"prestashop/dashactivity": "^2",
"prestashop/dashgoals": "^2",
"prestashop/dashproducts": "^2",
"prestashop/dashtrends": "^2",
"prestashop/gamification": "^2.4.0",
"prestashop/gamification": "^v2.4.0",
"prestashop/graphnvd3": "^2",
"prestashop/gridhtml": "^2",
"prestashop/gsitemap": "^4",
"prestashop/pagesnotfound": "^2",
"prestashop/productcomments": "^7.0",
"prestashop/productcomments": "^v4.2.2",
"prestashop/ps_banner": "^2",
"prestashop/ps_bestsellers": "^1.0",
"prestashop/ps_brandlist": "^1.0",
"prestashop/ps_cashondelivery": "^2.0",
"prestashop/ps_cashondelivery": "^1.0",
"prestashop/ps_categoryproducts": "^1.0",
"prestashop/ps_categorytree": "^2",
"prestashop/ps_checkpayment": "^2",
@ -79,28 +53,24 @@
"prestashop/ps_customersignin": "^2",
"prestashop/ps_customtext": "^4",
"prestashop/ps_dataprivacy": "^2.0",
"prestashop/ps_distributionapiclient": "^1.0.1",
"prestashop/ps_emailalerts": "^3.0",
"prestashop/ps_emailsubscription": "^2.7.0",
"prestashop/ps_facetedsearch": "^3.7.1",
"prestashop/ps_emailsubscription": "^v2.7.0",
"prestashop/ps_facetedsearch": "^v3.7.1",
"prestashop/ps_faviconnotificationbo": "^2",
"prestashop/ps_featuredproducts": "^2",
"prestashop/ps_googleanalytics": "^5.0",
"prestashop/ps_imageslider": "^3",
"prestashop/ps_languageselector": "^2",
"prestashop/ps_linklist": "^6",
"prestashop/ps_linklist": "^5",
"prestashop/ps_mainmenu": "^2",
"prestashop/ps_newproducts": "^1.0",
"prestashop/ps_searchbar": "^2",
"prestashop/ps_sharebuttons": "^2",
"prestashop/ps_shoppingcart": "^3",
"prestashop/ps_shoppingcart": "^2",
"prestashop/ps_socialfollow": "^2",
"prestashop/ps_specials": "^1.0",
"prestashop/ps_supplierlist": "^1.0",
"prestashop/ps_themecusto": "^1",
"prestashop/ps_viewedproduct": "^1.2",
"prestashop/ps_wirepayment": "^2",
"prestashop/psgdpr": "^1.0",
"prestashop/statsbestcategories": "^2",
"prestashop/statsbestcustomers": "^2",
"prestashop/statsbestmanufacturers": "^2",
@ -118,25 +88,28 @@
"prestashop/statsregistrations": "^2",
"prestashop/statssales": "^2",
"prestashop/statssearch": "^2",
"prestashop/statsstock": "^2",
"symfony/polyfill-mbstring": "*",
"symfony/polyfill-php54": "*",
"symfony/polyfill-php55": "^1.10",
"symfony/polyfill-php56": "*",
"symfony/polyfill-php70": "*",
"symfony/polyfill-php71": "*",
"symfony/polyfill-php72": "*",
"symfony/polyfill-php73": "*",
"symfony/polyfill-php74": "*"
"prestashop/statsstock": "^2"
},
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.0",
"ergebnis/composer-normalize": "^2.8",
"friendsofphp/php-cs-fixer": "^v2.16.4",
"johnkary/phpunit-speedtrap": "^v3.2.0",
"phpspec/prophecy-phpunit": "^2.0",
"prestashop/prestashop": "dev-develop",
"roave/security-advisories": "dev-latest",
"slevomat/coding-standard": "^6.3",
"squizlabs/php_codesniffer": "^3.5",
"symfony/phpunit-bridge": "^v3.4.43"
},
"config": {
"platform": {
"php": "7.3"
},
"preferred-install": "dist",
"prepend-autoloader": false,
"sort-packages": true
},
"repositories": [
{
"type": "vcs",
"url": "https://github.com/PrestaShop/jwt"
}
],
"minimum-stability": "dev",
"prefer-stable": true,
"autoload": {
"psr-4": {
"BTCPay\\": "modules/btcpay/src"
@ -145,19 +118,7 @@
"modules/btcpay/btcpay.php"
]
},
"config": {
"allow-plugins": {
"composer/installers": true,
"dealerdirect/phpcodesniffer-composer-installer": true,
"ergebnis/composer-normalize": true
},
"platform": {
"php": "8.0"
},
"preferred-install": "dist",
"prepend-autoloader": false,
"sort-packages": true
},
"minimum-stability": "stable",
"scripts": {
"post-install-cmd": [
"@install-codestandards"
@ -165,5 +126,9 @@
"install-codestandards": [
"Dealerdirect\\Composer\\Plugin\\Installers\\PHPCodeSniffer\\Plugin::run"
]
},
"support": {
"issues": "https://github.com/btcpayserver/prestashop-plugin/issues",
"source": "https://github.com/btcpayserver/prestashop-plugin"
}
}

7249
composer.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
<?php
const _DB_PREFIX_ = 'ps_';
define('_DB_PREFIX_', 'PS_');
abstract class AbstractAssetManager extends AbstractAssetManagerCore
{
@ -55,7 +55,6 @@ abstract class ObjectModel extends ObjectModelCore
}
abstract class PaymentModule extends PaymentModuleCore
{
protected $is_eu_compatible;
}
abstract class ProductListingFrontController extends ProductListingFrontControllerCore
{
@ -93,9 +92,27 @@ class AddressFormat extends AddressFormatCore
class AdminAccessController extends AdminAccessControllerCore
{
}
class AdminAddonsCatalogController extends AdminAddonsCatalogControllerCore
{
}
class AdminAddressesController extends AdminAddressesControllerCore
{
}
class AdminAdminPreferencesController extends AdminAdminPreferencesControllerCore
{
}
class AdminAttachmentsController extends AdminAttachmentsControllerCore
{
}
class AdminAttributeGeneratorController extends AdminAttributeGeneratorControllerCore
{
}
class AdminAttributesGroupsController extends AdminAttributesGroupsControllerCore
{
}
class AdminBackupController extends AdminBackupControllerCore
{
}
class AdminCarriersController extends AdminCarriersControllerCore
{
}
@ -108,24 +125,60 @@ class AdminCartRulesController extends AdminCartRulesControllerCore
class AdminCartsController extends AdminCartsControllerCore
{
}
class AdminCategoriesController extends AdminCategoriesControllerCore
{
}
class AdminCmsCategoriesController extends AdminCmsCategoriesControllerCore
{
}
class AdminCmsContentController extends AdminCmsContentControllerCore
{
}
class AdminCmsController extends AdminCmsControllerCore
{
}
class AdminContactsController extends AdminContactsControllerCore
{
}
class AdminController extends AdminControllerCore
{
}
class AdminCountriesController extends AdminCountriesControllerCore
{
}
class AdminCurrenciesController extends AdminCurrenciesControllerCore
{
}
class AdminCustomerPreferencesController extends AdminCustomerPreferencesControllerCore
{
}
class AdminCustomersController extends AdminCustomersControllerCore
{
}
class AdminCustomerThreadsController extends AdminCustomerThreadsControllerCore
{
}
class AdminDashboardController extends AdminDashboardControllerCore
{
}
class AdminDeliverySlipController extends AdminDeliverySlipControllerCore
{
}
class AdminEmailsController extends AdminEmailsControllerCore
{
}
class AdminEmployeesController extends AdminEmployeesControllerCore
{
}
class AdminFeaturesController extends AdminFeaturesControllerCore
{
}
class AdminGendersController extends AdminGendersControllerCore
{
}
class AdminGeolocationController extends AdminGeolocationControllerCore
{
}
class AdminGroupsController extends AdminGroupsControllerCore
{
}
@ -135,12 +188,36 @@ class AdminImagesController extends AdminImagesControllerCore
class AdminImportController extends AdminImportControllerCore
{
}
class AdminInformationController extends AdminInformationControllerCore
{
}
class AdminInvoicesController extends AdminInvoicesControllerCore
{
}
class AdminLanguagesController extends AdminLanguagesControllerCore
{
}
class AdminLegacyLayoutController extends AdminLegacyLayoutControllerCore
{
}
class AdminLocalizationController extends AdminLocalizationControllerCore
{
}
class AdminLoginController extends AdminLoginControllerCore
{
}
class AdminLogsController extends AdminLogsControllerCore
{
}
class AdminMaintenanceController extends AdminMaintenanceControllerCore
{
}
class AdminManufacturersController extends AdminManufacturersControllerCore
{
}
class AdminMetaController extends AdminMetaControllerCore
{
}
class AdminModulesController extends AdminModulesControllerCore
{
}
@ -150,15 +227,51 @@ class AdminModulesPositionsController extends AdminModulesPositionsControllerCor
class AdminNotFoundController extends AdminNotFoundControllerCore
{
}
class AdminOrderMessageController extends AdminOrderMessageControllerCore
{
}
class AdminOrderPreferencesController extends AdminOrderPreferencesControllerCore
{
}
class AdminOrdersController extends AdminOrdersControllerCore
{
}
class AdminOutstandingController extends AdminOutstandingControllerCore
{
}
class AdminPatternsController extends AdminPatternsControllerCore
{
}
class AdminPaymentController extends AdminPaymentControllerCore
{
}
class AdminPaymentPreferencesController extends AdminPaymentPreferencesControllerCore
{
}
class AdminPdfController extends AdminPdfControllerCore
{
}
class AdminPerformanceController extends AdminPerformanceControllerCore
{
}
class AdminPPreferencesController extends AdminPPreferencesControllerCore
{
}
class AdminPreferencesController extends AdminPreferencesControllerCore
{
}
class AdminProductsController extends AdminProductsControllerCore
{
}
class AdminProfilesController extends AdminProfilesControllerCore
{
}
class AdminQuickAccessesController extends AdminQuickAccessesControllerCore
{
}
class AdminReferrersController extends AdminReferrersControllerCore
{
}
class AdminRequestSqlController extends AdminRequestSqlControllerCore
{
}
@ -171,6 +284,12 @@ class AdminSearchConfController extends AdminSearchConfControllerCore
class AdminSearchController extends AdminSearchControllerCore
{
}
class AdminSearchEnginesController extends AdminSearchEnginesControllerCore
{
}
class AdminShippingController extends AdminShippingControllerCore
{
}
class AdminShopController extends AdminShopControllerCore
{
}
@ -180,6 +299,9 @@ class AdminShopGroupController extends AdminShopGroupControllerCore
class AdminShopUrlController extends AdminShopUrlControllerCore
{
}
class AdminSlipController extends AdminSlipControllerCore
{
}
class AdminSpecificPriceRuleController extends AdminSpecificPriceRuleControllerCore
{
}
@ -192,24 +314,63 @@ class AdminStatsController extends AdminStatsControllerCore
class AdminStatusesController extends AdminStatusesControllerCore
{
}
class AdminStockConfigurationController extends AdminStockConfigurationControllerCore
{
}
class AdminStockCoverController extends AdminStockCoverControllerCore
{
}
class AdminStockInstantStateController extends AdminStockInstantStateControllerCore
{
}
class AdminStockManagementController extends AdminStockManagementControllerCore
{
}
class AdminStockMvtController extends AdminStockMvtControllerCore
{
}
class AdminStoresController extends AdminStoresControllerCore
{
}
class AdminSuppliersController extends AdminSuppliersControllerCore
{
}
class AdminSupplyOrdersController extends AdminSupplyOrdersControllerCore
{
}
class AdminTabsController extends AdminTabsControllerCore
{
}
class AdminTagsController extends AdminTagsControllerCore
{
}
class AdminTaxesController extends AdminTaxesControllerCore
{
}
class AdminTaxRulesGroupController extends AdminTaxRulesGroupControllerCore
{
}
class AdminThemesCatalogController extends AdminThemesCatalogControllerCore
{
}
class AdminThemesController extends AdminThemesControllerCore
{
}
class AdminTrackingController extends AdminTrackingControllerCore
{
}
class AdminTranslationsController extends AdminTranslationsControllerCore
{
}
class AdminWarehousesController extends AdminWarehousesControllerCore
{
}
class AdminWebserviceController extends AdminWebserviceControllerCore
{
}
class AdminZonesController extends AdminZonesControllerCore
{
}
class Alias extends AliasCore
{
}
@ -219,6 +380,9 @@ class Attachment extends AttachmentCore
class AttachmentController extends AttachmentControllerCore
{
}
class Attribute extends AttributeCore
{
}
class AttributeGroup extends AttributeGroupCore
{
}
@ -579,6 +743,9 @@ class OrderDetail extends OrderDetailCore
class OrderDetailController extends OrderDetailControllerCore
{
}
class OrderDiscount extends OrderDiscountCore
{
}
class OrderFollowController extends OrderFollowControllerCore
{
}
@ -648,6 +815,9 @@ class PhpEncryption extends PhpEncryptionCore
class PhpEncryptionEngine extends PhpEncryptionEngineCore
{
}
class PhpEncryptionLegacyEngine extends PhpEncryptionLegacyEngineCore
{
}
class PrestaShopBackup extends PrestaShopBackupCore
{
}
@ -702,6 +872,9 @@ class RangePrice extends RangePriceCore
class RangeWeight extends RangeWeightCore
{
}
class Referrer extends ReferrerCore
{
}
class RequestSql extends RequestSqlCore
{
}
@ -891,11 +1064,15 @@ class WebserviceSpecificManagementImages extends WebserviceSpecificManagementIma
class WebserviceSpecificManagementSearch extends WebserviceSpecificManagementSearchCore
{
}
class Windows extends WindowsCore
{
}
class Zone extends ZoneCore
{
}
/* Class aliases */
class Autoload extends PrestaShopAutoload
{
}

View File

@ -1,76 +0,0 @@
#!/bin/bash
# Helper to connect to the running bitcoind (by default running regtest).
# Function to show usage instructions
show_usage() {
cat <<EOF
Usage: bitcoin-cli-helper COMMAND [ARGS]
Commands:
getnewaddress Generate a new address
generatetoaddress BLOCKS ADDRESS Generate new blocks and send the reward to the specified address
generate NUM_BLOCKS Generate a specified number of blocks
settxfee FEE Set the transaction fee for this network
sendtoaddress ADDRESS AMOUNT Send funds to an address
EOF
}
# Check if a valid command is provided
validate_command() {
local valid_commands=("getnewaddress" "generatetoaddress" "generate" "settxfee" "sendtoaddress")
local command=$1
for valid_cmd in "${valid_commands[@]}"; do
if [ "$valid_cmd" = "$command" ]; then
return 0
fi
done
return 1
}
# Validate the number of arguments
if [ "$#" -lt 1 ]; then
echo "Error: Missing command."
show_usage
exit 1
fi
# Check if the provided command is valid
if ! validate_command "$1"; then
echo "Error: Invalid command."
show_usage
exit 1
fi
# Execute the command with docker
if [ "$1" = "generate" ]; then
if [ "$#" -lt 2 ]; then
echo "Error: Missing number of blocks to generate."
show_usage
exit 1
fi
# Execute it right away, as we must pass `-`
docker exec prestashop_bitcoind bitcoin-cli -datadir="/data" -generate "$2"
exit 0
elif [ "$1" = "generatetoaddress" ]; then
if [ "$#" -lt 3 ]; then
echo "Error: Missing blocks to mine and/or address."
show_usage
exit 1
fi
elif [ "$1" = "settxfee" ]; then
if [ "$#" -lt 2 ]; then
echo "Error: Missing transaction fee value."
show_usage
exit 1
fi
elif [ "$1" = "sendtoaddress" ]; then
if [ "$#" -lt 3 ]; then
echo "Error: Missing address and/or amount to send."
show_usage
exit 1
fi
fi
# Execute the command with docker
docker exec prestashop_bitcoind bitcoin-cli -datadir="/data" "$@"

View File

@ -1,14 +0,0 @@
# Apache 2.2
<IfModule !mod_authz_core.c>
<Files *.php>
order allow,deny
deny from all
</Files>
</IfModule>
# Apache 2.4
<IfModule mod_authz_core.c>
<Files *.php>
Require all denied
</Files>
</IfModule>

View File

@ -1,19 +1,13 @@
<?php
use BTCPay\Constants;
use BTCPay\Github\Versioning;
use BTCPay\Installer\Config;
use BTCPay\Installer\Hooks;
use BTCPay\Installer\OrderStates;
use BTCPay\Installer\Tables;
use BTCPay\Installer\Webhook;
use BTCPay\LegacyOrderBitcoinRepository;
use BTCPay\Repository\BitcoinPaymentRepository;
use BTCPay\Repository\TableRepository;
use BTCPay\Server\Client;
use BTCPayServer\Exception\BTCPayException;
use BTCPayServer\Exception\RequestException;
use PrestaShop\PrestaShop\Adapter\Configuration;
use PrestaShop\PrestaShop\Adapter\Presenter\Order\OrderPresenter;
use PrestaShop\PrestaShop\Adapter\SymfonyContainer;
use PrestaShop\PrestaShop\Core\Payment\PaymentOption;
if (!defined('_PS_VERSION_')) {
@ -26,54 +20,41 @@ if (!file_exists(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
/** @noinspection AutoloadingIssuesInspection */
class BTCPay extends PaymentModule
{
/**
* @var BitcoinPaymentRepository
*/
private $repository;
public $tabs = [
[
'name' => 'BTCPay Server',
'name' => 'BTCPay',
'visible' => true,
'class_name' => 'AdminConfigureBTCPay',
'parent_class_name' => 'AdminParentPayment',
],
];
/**
* @var Configuration
*/
private $configuration;
/**
* @var Versioning
*/
private $versioning;
public function __construct()
{
$this->name = 'btcpay';
$this->tab = 'payments_gateways';
$this->version = '6.3.1';
$this->author = 'BTCPay Server';
$this->ps_versions_compliancy = ['min' => Constants::MINIMUM_PS_VERSION, 'max' => _PS_VERSION_];
$this->controllers = ['payment', 'validation', 'webhook'];
$this->version = '4.0.2';
$this->author = 'BTCPayServer';
$this->ps_versions_compliancy = ['min' => '1.7.7', 'max' => _PS_VERSION_];
$this->controllers = ['payment', 'validation', 'ipn'];
$this->is_eu_compatible = true;
$this->bootstrap = true;
$this->module_key = 'aec3ab8084e8e626bdcbda75d2ab39ee';
$this->currencies = true;
$this->currencies_mode = 'radio';
parent::__construct();
$this->displayName = $this->trans('BTCPay Server', [], 'Modules.Btcpay.Admin');
$this->description = $this->trans('Accept crypto payments via BTCPay Server.', [], 'Modules.Btcpay.Front');
$this->confirmUninstall = $this->trans('Are you sure that you want to uninstall this module? Past purchases and order states will be kept, but your configuration will be removed.', [], 'Modules.Btcpay.Front');
$this->configuration = new Configuration();
$this->versioning = new Versioning();
// Process any and all alerts/warnings
$this->displayModuleWarnings();
$this->displayName = $this->trans('BTCPay', [], 'Modules.Btcpay.Admin');
$this->description = $this->trans('Accepts Bitcoin payments via BTCPay.', [], 'Modules.Btcpay.Front');
$this->confirmUninstall = $this->trans('Are you sure you want to delete your details?', [], 'Modules.Btcpay.Front');
}
public function install(): bool
@ -82,26 +63,12 @@ class BTCPay extends PaymentModule
return false;
}
if (version_compare(\PHP_VERSION, Constants::MINIMUM_PHP_VERSION, '<')) {
$this->addModuleErrors([
[
'key' => sprintf('PHP version is too low. Expected %s or higher, received %s.', Constants::MINIMUM_PHP_VERSION, \PHP_VERSION),
'parameters' => [],
'domain' => 'Admin.Modules.Notification',
],
]);
if (null === ($repository = $this->getRepository())) {
return false;
}
if (version_compare(_PS_VERSION_, Constants::MINIMUM_PS_VERSION, '<')) {
$this->addModuleErrors([
[
'key' => sprintf('PrestaShop version is too low. Expected %s or higher, received %s.', Constants::MINIMUM_PS_VERSION, _PS_VERSION_),
'parameters' => [],
'domain' => 'Admin.Modules.Notification',
],
]);
if (!empty($errors = (new Config())->install())) {
$this->addModuleErrors($errors);
return false;
}
@ -112,14 +79,6 @@ class BTCPay extends PaymentModule
return false;
}
if (!empty($errors = (new Config())->install())) {
$this->addModuleErrors($errors);
return false;
}
// Create required tables
$repository = new TableRepository($this->get('doctrine.dbal.default_connection'));
if (!empty($errors = (new Tables($repository))->install())) {
$this->addModuleErrors($errors);
$this->uninstall();
@ -137,13 +96,10 @@ class BTCPay extends PaymentModule
return true;
}
/**
* When uninstalling, only remove the configuration and webhook. This way, order states and payment links are not lost.
*/
public function uninstall(): bool
{
if (!empty($errors = (new Webhook())->uninstall())) {
$this->addModuleErrors($errors);
if (null === ($repository = $this->getRepository())) {
return false;
}
if (!empty($errors = (new Config())->uninstall())) {
@ -152,27 +108,31 @@ class BTCPay extends PaymentModule
return false;
}
return parent::uninstall();
}
/**
* When resetting, only deal with the configuration. This way, order states and payment links are not lost.
*/
public function reset(): bool
{
$config = new Config();
if (!empty($errors = $config->uninstall())) {
if (!empty($errors = (new Tables($repository))->uninstall())) {
$this->addModuleErrors($errors);
return false;
}
if (!empty($errors = $config->install())) {
if (!empty($errors = (new OrderStates($this->name))->uninstall())) {
$this->addModuleErrors($errors);
return false;
}
return parent::uninstall();
}
public function reset(): bool
{
if (!$this->uninstall()) {
return false;
}
if (!$this->install()) {
return false;
}
return true;
}
@ -186,12 +146,7 @@ class BTCPay extends PaymentModule
Tools::redirectAdmin($this->context->link->getAdminLink('AdminConfigureBTCPay'));
}
/**
* Hooks on prestashop invoice
*
* @throws PrestaShopDatabaseException
* @throws JsonException
*/
// Hooks on prestashop invoice
public function hookDisplayAdminOrderMainBottom($params): ?string
{
// If the module is not active, abort
@ -205,77 +160,43 @@ class BTCPay extends PaymentModule
}
// Get BTCPay URL or abort
if (empty($serverUrl = $this->configuration->get(Constants::CONFIGURATION_BTCPAY_HOST))) {
if (empty($serverUrl = Configuration::get('BTCPAY_URL'))) {
return null;
}
// Ensure the client is ready for use
if (null === ($client = Client::createFromConfiguration($this->configuration)) || false === $client->isValid()) {
return null;
}
// Get legacy repository
$repository = new LegacyOrderBitcoinRepository();
// Get the order
if (null === ($bitcoinPayment = BitcoinPaymentRepository::getOneByOrderID($params['id_order']))) {
if (null === ($orderBitcoin = $repository->getOneByOrderID($params['id_order']))) {
return null;
}
// Check if it has an invoice ID
if (null === ($invoiceId = $bitcoinPayment->getInvoiceId()) || empty($invoiceId)) {
if (null === ($invoiceId = $orderBitcoin->getInvoiceId()) || empty($invoiceId)) {
return null;
}
// Get the cart
if (false === ($cart = Cart::getCartByOrderId($bitcoinPayment->getOrderId()))) {
if (false === ($cart = Cart::getCartByOrderId($orderBitcoin->getOrderId()))) {
return null;
}
// Get the store ID
$storeID = $this->configuration->get(Constants::CONFIGURATION_BTCPAY_STORE_ID);
// Get the currency from our cart
$currency = Currency::getCurrencyInstance((int) $cart->id_currency);
// Prepare smarty
$this->context->smarty->assign([
'server_url' => $serverUrl,
'storeCurrency' => Currency::getCurrencyInstance($cart->id_currency)->getSymbol(),
'server_url' => $serverUrl,
'currency_sign' => $currency->getSymbol(),
'payment_details' => $orderBitcoin->toArray(),
]);
try {
// Get the invoice and its payments
$invoice = $client->invoice()->getInvoice($storeID, $invoiceId);
// Filter out methods without payments
$paymentMethods = array_filter($client->invoice()->getPaymentMethods($storeID, $invoiceId), static function ($method) {
return !empty($method->getPayments());
});
// Has any payment been received
$paymentReceived = array_reduce($paymentMethods, static function ($carry, $method) {
return empty($method->getPayments()) ? $carry : true;
}, false);
// Add more information to smarty
$this->context->smarty->assign([
'invoice' => $invoice,
'paymentMethods' => $paymentMethods,
'paymentReceived' => $paymentReceived,
]);
return $this->display(__FILE__, 'views/templates/admin/invoice_block.tpl');
} catch (RequestException $exception) {
// Log the exception
PrestaShopLogger::addLog(\sprintf('[WARNING] Tried to load BTCPay Server invoice in hookDisplayAdminOrderMainBottom: %s', $exception->getMessage()), \PrestaShopLogger::LOG_SEVERITY_LEVEL_WARNING, $exception->getCode(), 'Order', $bitcoinPayment->getOrderId());
// Show that the invoice was not found
return $this->display(__FILE__, 'views/templates/admin/invoice_missing_block.tpl');
}
return $this->display(__FILE__, 'views/templates/admin/invoice_block.tpl');
}
/**
* Hooks on prestashop order details in frontend
*
* @throws JsonException
* @throws PrestaShopDatabaseException
*/
public function hookDisplayOrderDetail(array $params): ?string
// Hooks on prestashop order details in frontend
public function hookDisplayOrderDetail($params): ?string
{
// If the module is not active, abort
if (!$this->active) {
@ -287,70 +208,65 @@ class BTCPay extends PaymentModule
return null;
}
// Get BTCPay URL or abort
if (empty($serverUrl = Configuration::get('BTCPAY_URL'))) {
return null;
}
// Check if we actually have an order
$order = $params['order'];
if (!$order instanceof Order) {
return null;
}
// If created by another module, return
if ($order->module !== $this->name) {
return false;
}
// Check if we actually have an cart
// Check if we actually have an order
$cart = $params['cart'];
if (!$cart instanceof Cart) {
return null;
}
// Get BTCPay URL or abort
if (empty($serverUrl = $this->configuration->get(Constants::CONFIGURATION_BTCPAY_HOST))) {
return null;
}
// Ensure the client is ready for use
if (null === ($client = Client::createFromConfiguration($this->configuration)) || false === $client->isValid()) {
return null;
}
// Get legacy repository
$repository = new LegacyOrderBitcoinRepository();
// Get the order
if (null === ($bitcoinPayment = BitcoinPaymentRepository::getOneByOrderID($order->id))) {
if (null === ($orderBitcoin = $repository->getOneByOrderID($order->id))) {
return null;
}
// Check if it has an invoice ID
if (null === ($invoiceId = $bitcoinPayment->getInvoiceId()) || empty($invoiceId)) {
if (null === ($invoiceId = $orderBitcoin->getInvoiceId()) || empty($invoiceId)) {
return null;
}
// Get the store ID
$storeID = $this->configuration->get(Constants::CONFIGURATION_BTCPAY_STORE_ID);
// Get the currency from our cart
$currency = Currency::getCurrencyInstance((int) $cart->id_currency);
try {
// Prepare smarty
$this->context->smarty->assign([
'serverURL' => $serverUrl,
'storeCurrency' => Currency::getCurrencyInstance($cart->id_currency)->getSymbol(),
'invoice' => $client->invoice()->getInvoice($storeID, $invoiceId),
'paymentMethods' => $client->invoice()->getPaymentMethods($storeID, $invoiceId),
]);
// Prepare smarty
$this->context->smarty->assign(
[
'server_url' => $serverUrl,
'currency_sign' => $currency->sign,
'payment_details' => $orderBitcoin->toArray(),
]
);
return $this->display(__FILE__, 'views/templates/hooks/order_detail.tpl');
} catch (RequestException $exception) {
// Log the exception
PrestaShopLogger::addLog(\sprintf('[WARNING] Tried to load BTCPay Server invoice in hookDisplayOrderDetail: %s', $exception->getMessage()), \PrestaShopLogger::LOG_SEVERITY_LEVEL_WARNING, $exception->getCode(), 'Order', $order->id);
// If the invoice is gone just return null
return null;
}
return $this->display(__FILE__, 'views/templates/hooks/order_detail.tpl');
}
/**
* Hooks on prestashop payment returns
*
* @throws Exception
*/
public function hookDisplayPaymentEU(): array
{
if (!$this->active) {
return [];
}
return [
'cta_text' => $this->name,
'logo' => Media::getMediaPath(_PS_MODULE_DIR_ . $this->name . '/views/images/bitcoin.png'),
'action' => $this->context->link->getModuleLink($this->name, 'payment', [], true),
];
}
// Hooks on prestashop payment returns
public function hookPaymentReturn($params): ?string
{
// If the module is not active, abort
@ -369,75 +285,38 @@ class BTCPay extends PaymentModule
return null;
}
// Get the order
if (null === ($bitcoinPayment = BitcoinPaymentRepository::getOneByOrderID($order->id))) {
return null;
}
// Prepare smarty to present order details
$this->context->smarty->assign([
'presenter' => (new OrderPresenter())->present($order),
'bitcoinPayment' => $bitcoinPayment,
'order_state' => $order->getCurrentState(),
'os_waiting' => $this->configuration->getInt(Constants::CONFIGURATION_ORDER_STATE_WAITING),
'os_confirming' => $this->configuration->getInt(Constants::CONFIGURATION_ORDER_STATE_CONFIRMING),
'os_failed' => $this->configuration->getInt(Constants::CONFIGURATION_ORDER_STATE_FAILED),
'os_paid' => $this->configuration->getInt(Constants::CONFIGURATION_ORDER_STATE_PAID),
]);
$this->context->smarty->assign(
[
'presenter' => (new OrderPresenter())->present($order),
'order_state' => $order->getCurrentState(),
'os_waiting' => (string) Configuration::get('BTCPAY_OS_WAITING'),
'os_confirming' => (string) Configuration::get('BTCPAY_OS_CONFIRMING'),
'os_failed' => (string) Configuration::get('BTCPAY_OS_FAILED'),
'os_paid' => (string) Configuration::get('BTCPAY_OS_PAID'),
]
);
return $this->display(__FILE__, 'views/templates/hooks/payment_return.tpl');
}
/**
* Hooks on prestashop payment options
*
* @throws SmartyException
* @throws JsonException
*/
// Hooks on prestashop payment options
public function hookPaymentOptions(): array
{
// If the module is not active, abort
if (!$this->active) {
return [];
}
// Ensure the client is ready for use
if (null === ($client = Client::createFromConfiguration($this->configuration)) || false === $client->isValid()) {
return [];
}
$paymentOption = new PaymentOption();
$paymentOption->setLogo(Media::getMediaPath(_PS_MODULE_DIR_ . $this->name . '/views/images/bitcoin.png'));
$paymentOption->setModuleName($this->name);
$paymentOption->setCallToActionText($this->trans('Pay with Bitcoin', [], 'Modules.Btcpay.Front'));
$paymentOption->setAction($this->context->link->getModuleLink($this->name, 'payment', [], true));
// Get the store ID
$storeID = $this->configuration->get(Constants::CONFIGURATION_BTCPAY_STORE_ID);
try {
// If the server is not fully synced, do not show the option
if (!$client->server()->getInfo()->isFullySynced()) {
return [];
}
return [
(new PaymentOption())
->setModuleName($this->name)
->setLogo(Media::getMediaPath(_PS_MODULE_DIR_ . $this->name . '/views/images/payment.png'))
->setAction($this->context->link->getModuleLink($this->name, 'payment', [], true))
->setCallToActionText($this->trans('Pay with BTCPay Server', [], 'Modules.Btcpay.Front'))
->setAdditionalInformation($this->context->smarty->fetch('module:btcpay/views/templates/hooks/payment_option.tpl')),
];
} catch (BTCPayException $exception) {
// Log the exception
PrestaShopLogger::addLog(\sprintf('[WARNING] Could not load payments options from server: %s', $exception->getMessage()), \PrestaShopLogger::LOG_SEVERITY_LEVEL_WARNING, $exception->getCode());
return [];
}
return [$paymentOption];
}
/**
* Hooks on prestashop cart changes
*
* @throws JsonException
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
*/
// Hooks on prestashop cart changes
public function hookActionCartSave(array $params): void
{
// If the module is not active, abort
@ -461,18 +340,16 @@ class BTCPay extends PaymentModule
}
// Get the order
if (null === ($bitcoinPayment = BitcoinPaymentRepository::getOneByCartID($cart->id))) {
if (null === ($orderBitcoin = (new LegacyOrderBitcoinRepository())->getOneByCartID($cart->id))) {
return;
}
PrestaShopLogger::addLog('[INFO] Order has changed for cart. Cancelling....', \PrestaShopLogger::LOG_SEVERITY_LEVEL_WARNING, null, 'Cart', $cart->id);
PrestaShopLogger::addLog('[WARNING] Order has changed for cart: ' . $cart->id . '. Cancelling....', 2);
// Try to remove the order
if (false === $bitcoinPayment->delete()) {
$error = \sprintf('[ERROR] Expected to remove the order %s, but failed to do so', $bitcoinPayment->getOrderId());
PrestaShopLogger::addLog($error, \PrestaShopLogger::LOG_SEVERITY_LEVEL_ERROR, null, 'BitcoinPayment', $bitcoinPayment->getId());
throw new \PrestaShopDatabaseException($error);
if (false === $orderBitcoin->delete()) {
PrestaShopLogger::addLog('[ERROR] Expected to remove the order, but failed to do so', 3);
throw new \PrestaShopDatabaseException('Expected to remove the order, but failed to do so');
}
}
@ -483,47 +360,21 @@ class BTCPay extends PaymentModule
}
}
private function displayModuleWarnings(): void
private function getRepository(): ?BitcoinPaymentRepository
{
// Try and create the client
$client = Client::createFromConfiguration($this->configuration);
try {
// Show warning if API key is missing or if we're not yet fully synced
if (null === $client || empty($this->configuration->get(Constants::CONFIGURATION_BTCPAY_API_KEY))) {
$this->warning = $this->trans('Your BTCPay Server store has not yet been linked, payment option is unavailable.', [], 'Modules.Btcpay.Admin');
} elseif (!$client->server()->getInfo()->isFullySynced()) {
$this->warning = $this->trans('One (or more) coins are not yet synced, payment option will be unavailable until the sync has finished.', [], 'Modules.Btcpay.Admin');
if (null === $this->repository) {
try {
$this->repository = $this->get('prestashop.module.btcpay.repository');
} catch (Throwable $e) {
if (null !== ($container = SymfonyContainer::getInstance())) {
$this->repository = new BitcoinPaymentRepository(
$container->get('doctrine.dbal.default_connection'),
$container->getParameter('database_prefix')
);
}
}
} catch (BTCPayException $exception) {
// Log the exception
PrestaShopLogger::addLog(\sprintf('[WARNING] BTCPay Server configuration is no longer valid, resetting host and API key. Error: %s', $exception->getMessage()), \PrestaShopLogger::LOG_SEVERITY_LEVEL_WARNING, $exception->getCode());
// Show a warning
$this->warning = $this->trans('Your BTCPay Server configuration is no longer valid, please setup module again.', [], 'Modules.Btcpay.Admin');
// Reset configuration
$this->configuration->set(Constants::CONFIGURATION_BTCPAY_HOST, Constants::CONFIGURATION_DEFAULT_HOST);
$this->configuration->set(Constants::CONFIGURATION_BTCPAY_API_KEY, null);
// Bail
return;
} catch (\Throwable $exception) {
// Log the exception
PrestaShopLogger::addLog(\sprintf('[WARNING] Could not parse server information: %s', $exception->getMessage()), \PrestaShopLogger::LOG_SEVERITY_LEVEL_WARNING, $exception->getCode());
// Bail
return;
}
// API key/sync warnings are more important than a new version, if a warning is set, return now
if (!empty($this->warning)) {
return;
}
// Otherwise, add a warning on version upgrade
if (null !== ($latest = $this->versioning->latest()) && $latest->newer($this->version)) {
$this->warning = $this->trans(\sprintf('There is a new version available for this plugin: %s.', $latest->getTagName()), [], 'Modules.Btcpay.Admin');
}
return $this->repository;
}
}

View File

@ -1,8 +1,7 @@
{
"name": "btcpay/prestashop",
"description": "BTCPay payment plugin for PrestaShop",
"license": "MIT",
"type": "prestashop-module",
"description": "BTCPay payment plugin for PrestaShop",
"keywords": [
"prestashop",
"plugin",
@ -12,41 +11,30 @@
"BTCPay"
],
"homepage": "https://github.com/btcpayserver/prestashop-plugin",
"support": {
"issues": "https://github.com/btcpayserver/prestashop-plugin/issues",
"source": "https://github.com/btcpayserver/prestashop-plugin"
},
"license": "MIT",
"require": {
"php": ">=8.0",
"php": ">=7.3.0",
"ext-PDO": "*",
"ext-bcmath": "*",
"ext-curl": "*",
"ext-gd": "*",
"ext-intl": "*",
"ext-json": "*",
"ext-mbstring": "*",
"btcpayserver/btcpayserver-greenfield-php": "^2.8.1",
"composer/semver": "^3.4.2",
"stechstudio/backoff": "^1.4"
"btcpayserver/btcpayserver-php-client": "^v1.0.1",
"stechstudio/backoff": "^1.2"
},
"require-dev": {
"ergebnis/composer-normalize": "^2.43",
"roave/security-advisories": "dev-latest",
"symfony/debug-bundle": "~5.4.40",
"symfony/var-dumper": "~5.4.43"
"ergebnis/composer-normalize": "^2.15",
"roave/security-advisories": "dev-latest"
},
"replace": {
"symfony/polyfill-mbstring": "*",
"symfony/polyfill-php54": "*",
"symfony/polyfill-php55": "^1.10",
"symfony/polyfill-php56": "*",
"symfony/polyfill-php70": "*",
"symfony/polyfill-php71": "*",
"symfony/polyfill-php72": "*",
"symfony/polyfill-php73": "*",
"symfony/polyfill-php74": "*"
"config": {
"platform": {
"php": "7.3"
},
"preferred-install": "dist",
"prepend-autoloader": false,
"sort-packages": true
},
"minimum-stability": "stable",
"autoload": {
"psr-4": {
"BTCPay\\": "src/"
@ -56,16 +44,9 @@
],
"exclude-from-classmap": []
},
"config": {
"allow-plugins": {
"ergebnis/composer-normalize": true,
"php-http/discovery": true
},
"platform": {
"php": "8.0"
},
"preferred-install": "dist",
"prepend-autoloader": false,
"sort-packages": true
"minimum-stability": "stable",
"support": {
"issues": "https://github.com/btcpayserver/prestashop-plugin/issues",
"source": "https://github.com/btcpayserver/prestashop-plugin"
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,12 +0,0 @@
<?xml version="1.0" encoding="UTF-8" ?>
<module>
<name>btcpay</name>
<displayName><![CDATA[BTCPay Server]]></displayName>
<version><![CDATA[6.1.0]]></version>
<description><![CDATA[Accept crypto payments via BTCPay Server.]]></description>
<author><![CDATA[BTCPay Server]]></author>
<tab><![CDATA[payments_gateways]]></tab>
<confirmUninstall><![CDATA[Are you sure that you want to uninstall this module? Past purchases and order states will be kept, but your configuration will be removed.]]></confirmUninstall>
<is_configurable>1</is_configurable>
<need_instance>1</need_instance>
</module>

View File

@ -1,11 +0,0 @@
<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

View File

@ -2,31 +2,13 @@ admin_btcpay_configure:
path: /btcpay/configure
methods: [GET]
defaults:
_controller: 'BTCPay\Controller\Admin\Improve\Payment\ConfigureController::viewAction'
_controller: 'BTCPay\Controller\Admin\Improve\Payment\ConfigureController::editAction'
_legacy_controller: AdminConfigureBTCPay
_legacy_link: AdminConfigureBTCPay
_disable_module_prefix: true
admin_btcpay_configure_server_process:
admin_btcpay_configure_process:
path: /btcpay/configure
methods: [GET, POST]
methods: [POST]
defaults:
_controller: 'BTCPay\Controller\Admin\Improve\Payment\ConfigureController::editServerSettingsAction'
_controller: 'BTCPay\Controller\Admin\Improve\Payment\ConfigureController::editProcessAction'
_legacy_controller: AdminConfigureBTCPay
_disable_module_prefix: true
admin_btcpay_configure_general_process:
path: /btcpay/configure/general
methods: [GET, POST]
defaults:
_controller: 'BTCPay\Controller\Admin\Improve\Payment\ConfigureController::editGeneralSettingsAction'
_legacy_controller: AdminConfigureBTCPay
_disable_module_prefix: true
admin_btcpay_validate:
path: /btcpay/api-key/validate
methods: [GET, POST]
defaults:
_controller: 'BTCPay\Controller\Admin\Improve\Payment\ConfigureController::validateAPIKeyAction'
_legacy_controller: AdminConfigureBTCPay
_disable_module_prefix: true

View File

@ -2,63 +2,31 @@ services:
_defaults:
public: true
# Module
prestashop.module.btcpay:
class: BTCPay
# Repositories
prestashop.module.btcpay.repository.install:
class: BTCPay\Repository\TableRepository
prestashop.module.btcpay.repository:
class: BTCPay\Repository\BitcoinPaymentRepository
arguments:
- '@doctrine.dbal.default_connection'
- '%database_prefix%'
$connection: '@doctrine.dbal.default_connection'
$prefix: '%database_prefix%'
# Controllers
BTCPay\Controller\Admin\Improve\Payment\ConfigureController:
class: BTCPay\Controller\Admin\Improve\Payment\ConfigureController
arguments:
- '@prestashop.module.btcpay'
- '@validator'
- '@prestashop.module.btcpay.form_handler.server'
- '@prestashop.module.btcpay.form_handler.general'
# BTCPay form data provider
prestashop.module.btcpay.form.configure.provider:
class: BTCPay\Form\ConfigureFormDataProvider
# Form data provider
prestashop.module.btcpay.form.server.provider:
class: BTCPay\Form\ServerFormDataProvider
prestashop.module.btcpay.form.general.provider:
class: BTCPay\Form\GeneralFormDataProvider
# Form type
prestashop.module.btcpay.form.type.server:
class: BTCPay\Form\Type\ServerType
# Form types
prestashop.module.btcpay.form.type.configure:
class: BTCPay\Form\Type\ConfigureType
parent: 'form.type.translatable.aware'
public: true
tags: [{ name: form.type }]
tags:
- { name: form.type }
prestashop.module.btcpay.form.type.general:
class: BTCPay\Form\Type\GeneralType
parent: 'form.type.translatable.aware'
public: true
tags: [{ name: form.type }]
# Form handler
prestashop.module.btcpay.form_handler.server:
class: PrestaShop\PrestaShop\Core\Form\Handler
# BTCPay form handler
prestashop.module.btcpay.form_handler:
class: 'PrestaShop\PrestaShop\Core\Form\FormHandler'
arguments:
$formFactory: '@Symfony\Component\Form\FormFactoryInterface'
$formBuilder: '@=service("form.factory").createBuilder()'
$hookDispatcher: '@prestashop.core.hook.dispatcher'
$formDataProvider: '@prestashop.module.btcpay.form.server.provider'
$form: 'BTCPay\Form\Type\ServerType'
$hookName: 'ServerType'
$formName: 'server-form'
# Form handler
prestashop.module.btcpay.form_handler.general:
class: PrestaShop\PrestaShop\Core\Form\Handler
arguments:
$formFactory: '@Symfony\Component\Form\FormFactoryInterface'
$hookDispatcher: '@prestashop.core.hook.dispatcher'
$formDataProvider: '@prestashop.module.btcpay.form.general.provider'
$form: 'BTCPay\Form\Type\GeneralType'
$hookName: 'GeneralType'
$formName: 'general-form'
$formDataProvider: '@prestashop.module.btcpay.form.configure.provider'
$formTypes:
'btcpay': 'BTCPay\Form\Type\ConfigureType'
$hookName: 'AdminConfigureBTCPay'

View File

@ -1,23 +1,22 @@
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
class AdminConfigureBTCPayController extends ModuleAdminController
{
/**
* BTCpay module instance. It's assigned automatically by PrestaShop infrastructure.
*
* @var BTCPay
*/
public $module;
/**
* Configure the administration controller and define some sane defaults.
*
* {@inheritdoc}
*/
public function __construct()
{
$this->bootstrap = true;
$this->display = 'view';
parent::__construct();
if (!$this->module->active) {
@ -26,7 +25,7 @@ class AdminConfigureBTCPayController extends ModuleAdminController
}
/**
* Redirect to the actual configuration screen of this module.
* Redirect to the configuration screen of this module.
*
* {@inheritdoc}
*/

View File

@ -1,11 +0,0 @@
<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

View File

@ -1,11 +0,0 @@
<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

View File

@ -0,0 +1,55 @@
<?php
use BTCPay\LegacyOrderBitcoinRepository;
use BTCPay\Server\IPN;
use Symfony\Component\HttpFoundation\Request;
/** @noinspection AutoloadingIssuesInspection */
class BTCPayIpnModuleFrontController extends \ModuleFrontController
{
/**
* Enable SSL only.
*
* @var bool
*/
public $ssl = true;
/**
* @var BTCPay
*/
public $module;
/**
* @var IPN
*/
private $ipn;
public function __construct()
{
parent::__construct();
$this->ipn = new IPN(new LegacyOrderBitcoinRepository());
}
/**
* We don't want to show anything, so just don't render anything.
*
* {@inheritdoc}
*/
public function display(): bool
{
return true;
}
/**
* {@inheritdoc}
*/
public function postProcess(): void
{
try {
$this->ipn->process($this->module, Request::createFromGlobals());
} catch (\Exception $e) {
throw new LogicException('Could not process', 1, $e);
}
}
}

View File

@ -1,13 +1,9 @@
<?php
use BTCPay\Constants;
use BTCPay\LegacyOrderBitcoinRepository;
use BTCPay\Server\Factory;
use PrestaShop\PrestaShop\Adapter\Configuration;
if (!defined('_PS_VERSION_')) {
exit;
}
/** @noinspection AutoloadingIssuesInspection */
class BTCPayPaymentModuleFrontController extends ModuleFrontController
{
/**
@ -17,54 +13,32 @@ class BTCPayPaymentModuleFrontController extends ModuleFrontController
*/
public $ssl = true;
/**
* @var BTCPay
*/
public $module;
/**
* @var Factory
*/
private $factory;
/**
* @var Configuration
*/
private $configuration;
public function __construct()
{
parent::__construct();
$this->factory = new Factory($this->module, $this->context);
$this->configuration = new Configuration();
$this->factory = new Factory(new LegacyOrderBitcoinRepository(), new \Link(), $this->module->name);
}
/**
* {@inheritdoc}
*/
public function initContent(): void
{
parent::initContent();
if (null === $this->configuration->get(Constants::CONFIGURATION_BTCPAY_STORE_ID)) {
$this->warning[] = $this->context->getTranslator()->trans('Payment method has not yet been setup properly. Please try again or contact us.', [], 'Modules.Btcpay.Front');
$this->redirectWithNotifications($this->context->link->getPageLink('cart', $this->ssl));
if (null !== ($redirect = $this->factory->createPaymentRequest($this->context->customer, $this->context->cart))) {
Tools::redirectLink($redirect);
return;
}
try {
if (null !== ($redirect = $this->factory->createPaymentRequest($this->context->customer, $this->context->cart))) {
Tools::redirect($redirect);
return;
}
$this->warning[] = $this->context->getTranslator()->trans('We could not create a payment request via BTCPay Server. Please try again or contact us.', [], 'Modules.Btcpay.Front');
$this->redirectWithNotifications($this->context->link->getPageLink('cart', $this->ssl));
} catch (\Throwable $throwable) {
PrestaShopLogger::addLog(\sprintf('[ERROR] An error occurred during payment creation: %s', $throwable), PrestaShopLogger::LOG_SEVERITY_LEVEL_ERROR, $throwable->getCode());
$this->warning[] = $this->context->getTranslator()->trans('We are having issues with our BTCPay Server backend. Please try again or contact us.', [], 'Modules.Btcpay.Front');
$this->redirectWithNotifications($this->context->link->getPageLink('cart', $this->ssl));
}
$this->warning[] = $this->context->getTranslator()->trans('We are having issues with our BTCPayServer backend. Please try again or contact us.', [], 'Modules.Btcpay.Front');
$this->redirectWithNotifications($this->context->link->getPageLink('cart', $this->ssl));
}
}

View File

@ -1,15 +1,8 @@
<?php
use BTCPay\Constants;
use BTCPay\Invoice\Processor;
use BTCPay\Repository\BitcoinPaymentRepository;
use BTCPay\Server\Client;
use PrestaShop\PrestaShop\Adapter\Configuration;
if (!defined('_PS_VERSION_')) {
exit;
}
use BTCPay\LegacyOrderBitcoinRepository;
/** @noinspection AutoloadingIssuesInspection */
class BTCPayValidationModuleFrontController extends ModuleFrontController
{
/**
@ -20,26 +13,19 @@ class BTCPayValidationModuleFrontController extends ModuleFrontController
public $ssl = true;
/**
* @var \BTCPay
* @var LegacyOrderBitcoinRepository
*/
public $module;
/**
* @var Configuration
*/
private $configuration;
private $repository;
public function __construct()
{
parent::__construct();
$this->configuration = new Configuration();
$this->repository = new LegacyOrderBitcoinRepository();
}
/**
* @throws JsonException
* @throws PrestaShopDatabaseException
* @throws PrestaShopException
* {@inheritdoc}
*/
public function postProcess(): void
{
@ -53,21 +39,20 @@ class BTCPayValidationModuleFrontController extends ModuleFrontController
// Get the translator so we can translate our errors
if (null === ($translator = $this->getTranslator())) {
throw new RuntimeException('Expected the translator to be available');
throw new \RuntimeException('Expected the translator to be available');
}
// Check if our payment option is still valid
$paymentAvailable = false;
$authorized = false;
foreach (Module::getPaymentModules() as $module) {
if ($module['name'] === $this->module->name) {
$paymentAvailable = true;
$authorized = true;
break;
}
}
// If it's no longer valid, redirect the customer.
if (!$paymentAvailable) {
if (!$authorized) {
$this->warning[] = $translator->trans('This payment method is not available.', [], 'Modules.Btcpay.Front');
$this->redirectWithNotifications($this->context->link->getPageLink('cart', $this->ssl));
@ -76,45 +61,27 @@ class BTCPayValidationModuleFrontController extends ModuleFrontController
// Get the passed invoice reference, which we can then use to get the actual order
$invoiceReference = Tools::getValue('invoice_reference', 0);
if (null === ($bitcoinPayment = BitcoinPaymentRepository::getOneByInvoiceReference($invoiceReference))) {
$this->warning[] = $translator->trans('The passed invoice reference is not valid.', [], 'Modules.Btcpay.Front');
if (null === ($orderBitcoin = $this->repository->getOneByInvoiceReference($invoiceReference))) {
$this->warning[] = $translator->trans('There is no order that we can validate.', [], 'Modules.Btcpay.Front');
$this->redirectWithNotifications($this->context->link->getPageLink('cart', $this->ssl));
return;
}
// Grab the current order mode
$orderMode = $this->configuration->get(Constants::CONFIGURATION_ORDER_MODE);
// Get the order and validate it
$order = Order::getByCartId($bitcoinPayment->getCartId());
$order = Order::getByCartId($orderBitcoin->getCartId());
if (null === $order || 0 === $order->id || (int) $order->id_customer !== (int) $this->context->customer->id) {
// The order must exist when using the `before` order mode
if (Constants::ORDER_MODE_BEFORE === $orderMode) {
$this->warning[] = $translator->trans('There is no order that we can process.', [], 'Modules.Btcpay.Front');
$this->redirectWithNotifications($this->context->link->getPageLink('cart', $this->ssl));
$this->warning[] = $translator->trans('There is no order that we can process.', [], 'Modules.Btcpay.Front');
$this->redirectWithNotifications($this->context->link->getPageLink('cart', $this->ssl));
return;
}
// Ensure the client is ready for use
if (null === ($client = Client::createFromConfiguration($this->configuration)) || false === $client->isValid()) {
throw new RuntimeException('Expected the client to be available');
}
// User was quicker than the callback, so we deal with the actual invoice now
$processor = new Processor($this->module, $this->context, $this->configuration, $client);
$processor->paymentReceivedCreateAfter($bitcoinPayment);
// Grab the (now existing) order
$order = Order::getByCartId($bitcoinPayment->getCartId());
return;
}
// Get the customer so we can do a fancy redirect
$customer = new Customer((int) $cart->id_customer);
// If it's a guest, sent them to guest tracking
if (Cart::isGuestCartByCartId($bitcoinPayment->getCartId())) {
if (Cart::isGuestCartByCartId($orderBitcoin->getCartId())) {
Tools::redirect($this->context->link->getPageLink('guest-tracking', $this->ssl, null, ['order_reference' => $order->reference, 'email' => $customer->email]));
return;

View File

@ -1,94 +0,0 @@
<?php
use BTCPay\Constants;
use BTCPay\Server\Client;
use BTCPay\Server\WebhookHandler;
use PrestaShop\PrestaShop\Adapter\Configuration;
use Symfony\Component\HttpFoundation\Request;
if (!defined('_PS_VERSION_')) {
exit;
}
class BTCPayWebhookModuleFrontController extends \ModuleFrontController
{
/**
* Enable SSL only.
*
* @var bool
*/
public $ssl = true;
/**
* @var BTCPay
*/
public $module;
/**
* @var Configuration
*/
private $configuration;
/**
* @var Client|null
*/
private $client;
/**
* @var WebhookHandler
*/
private $handler;
public function __construct()
{
parent::__construct();
$this->configuration = new Configuration();
$this->client = Client::createFromConfiguration($this->configuration);
$this->handler = new WebhookHandler($this->module, $this->context, $this->client);
}
/**
* We don't want to show anything, so just don't render anything.
*
* {@inheritdoc}
*/
public function display(): bool
{
return true;
}
/**
* {@inheritdoc}
*
* @throws Exception
*/
public function postProcess(): void
{
$request = Request::createFromGlobals();
// If the module is inactive, or we don't receive a signature, or we don't have the webhook secret, just return
if (!$this->module->active
|| null === ($signature = $request->headers->get(Constants::BTCPAY_HEADER_SIG))
|| false === ($secret = $this->configuration->get(Constants::CONFIGURATION_BTCPAY_WEBHOOK_SECRET))) {
return;
}
// Ensure the client is ready for use, if not, just return
if (null === $this->client || false === $this->client->isValid()) {
return;
}
// Ensure our webhook is actually valid
if (false === $this->client->webhook()->isIncomingWebhookRequestValid($request->getContent(), $signature, $secret)) {
$error = 'Invalid BTCPay Server payment notification message received - signature did not match.';
\PrestaShopLogger::addLog($error, \PrestaShopLogger::LOG_SEVERITY_LEVEL_ERROR);
throw new \Exception($error);
}
$this->handler->process($request);
echo 'OK';
}
}

View File

@ -1,11 +0,0 @@
<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

View File

@ -1,11 +0,0 @@
<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

File diff suppressed because it is too large Load Diff

View File

@ -2,70 +2,19 @@
namespace BTCPay;
use BTCPayServer\Client\InvoiceCheckoutOptions;
if (!\defined('_PS_VERSION_')) {
exit;
}
use BTCPayServer\Invoice;
class Constants
{
// Version information
public const MINIMUM_BTCPAY_VERSION = '1.7.0';
public const MINIMUM_PS_VERSION = '8.0.0';
public const MINIMUM_PHP_VERSION = '8.0.0';
/**
* All possible transaction speeds as defined by BTCPay server
*/
public const TRANSACTION_SPEEDS = [Invoice::TRANSACTION_SPEED_LOW, Invoice::TRANSACTION_SPEED_MEDIUM, Invoice::TRANSACTION_SPEED_HIGH];
// Cache configuration
public const LASTEST_VERSION_CACHE_KEY = 'BTCPAY_LATEST_VERSION';
public const LASTEST_VERSION_CACHE_EXPIRATION = 60 * 60 * 24 * 7; // 7 days
// GitHub API endpoint for releases
public const GITHUB_API_LATEST_ENDPOINT = 'https://api.github.com/repos/btcpayserver/prestashop-plugin/releases/latest';
// BTCPay Server webhook header
public const BTCPAY_HEADER_SIG = 'Btcpay-Sig';
// BTCPay required permissions
public const BTCPAY_PERMISSIONS = [
'btcpay.store.canmodifystoresettings',
'btcpay.store.webhooks.canmodifywebhooks',
'btcpay.store.canviewstoresettings',
'btcpay.store.cancreateinvoice',
'btcpay.store.canviewinvoices',
'btcpay.store.canmodifyinvoices',
];
// BTCPay Server configuration
public const CONFIGURATION_BTCPAY_HOST = 'BTCPAY_URL';
public const CONFIGURATION_BTCPAY_API_KEY = 'BTCPAY_API_KEY';
public const CONFIGURATION_BTCPAY_STORE_ID = 'BTCPAY_STORE_ID';
public const CONFIGURATION_BTCPAY_WEBHOOK_ID = 'BTCPAY_WEBHOOK_ID';
public const CONFIGURATION_BTCPAY_WEBHOOK_SECRET = 'BTCPAY_WEBHOOK_SECRET';
// Default values
public const CONFIGURATION_DEFAULT_HOST = 'https://testnet.demo.btcpayserver.org';
// Order (creation) related configuration
public const CONFIGURATION_ORDER_MODE = 'BTCPAY_ORDERMODE';
// Order states
public const CONFIGURATION_ORDER_STATE_WAITING = 'BTCPAY_OS_WAITING';
public const CONFIGURATION_ORDER_STATE_CONFIRMING = 'BTCPAY_OS_CONFIRMING';
public const CONFIGURATION_ORDER_STATE_FAILED = 'BTCPAY_OS_FAILED';
public const CONFIGURATION_ORDER_STATE_PAID = 'BTCPAY_OS_PAID';
// Do we want to protect order states from getting changed by webhook
public const CONFIGURATION_PROTECT_ORDERS = 'BTCPAY_PROTECT_ORDERS';
// Do we want to share personal details with BTCPay Server
public const CONFIGURATION_SHARE_METADATA = 'BTCPAY_SHARE_METADATA';
// All possible transaction speeds as defined by BTCPay server
public const CONFIGURATION_SPEED_MODE = 'BTCPAY_TXSPEED';
public const TRANSACTION_SPEEDS = [InvoiceCheckoutOptions::SPEED_HIGH, InvoiceCheckoutOptions::SPEED_MEDIUM, InvoiceCheckoutOptions::SPEED_LOW];
// All possible options for order creation (before or after payment)
/**
* All possible options for order creation (before or after payment)
*/
public const ORDER_MODES = [self::ORDER_MODE_BEFORE, self::ORDER_MODE_AFTER];
public const ORDER_MODE_BEFORE = 'before_payment';
public const ORDER_MODE_AFTER = 'after_payment';
public const ORDER_MODE_BEFORE = 'beforepayment';
public const ORDER_MODE_AFTER = 'afterpayment';
}

View File

@ -2,447 +2,63 @@
namespace BTCPay\Controller\Admin\Improve\Payment;
use BTCPay;
use BTCPay\Constants;
use BTCPay\Form\Data\General;
use BTCPay\Form\Data\Server;
use BTCPay\Github\Versioning;
use BTCPay\Server\Client;
use BTCPay\Server\Data\ValidateApiKey;
use BTCPayServer\Client\ApiKey;
use Exception;
use PrestaShop\PrestaShop\Core\Domain\Configuration\ShopConfigurationInterface;
use PrestaShop\PrestaShop\Core\Form\FormHandlerInterface;
use PrestaShopBundle\Controller\Admin\FrameworkBundleAdminController;
use PrestaShopBundle\Security\Annotation\AdminSecurity;
use PrestaShopBundle\Security\Annotation\ModuleActivated;
use PrestaShopLogger;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Validator\ConstraintViolationInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Throwable;
if (!\defined('_PS_VERSION_')) {
exit;
}
/**
* @ModuleActivated(moduleName="btcpay", redirectRoute="admin_module_manage")
*/
class ConfigureController extends FrameworkBundleAdminController
{
/**
* @var BTCPay
*/
private $module;
/**
* @var ValidatorInterface
*/
private $validator;
/**
* @var FormHandlerInterface
*/
private $serverFormHandler;
/**
* @var FormHandlerInterface
*/
private $generalFormHandler;
/**
* @var Versioning
*/
private $versioning;
public function __construct(BTCPay $module, ValidatorInterface $validator, FormHandlerInterface $serverFormHandler, FormHandlerInterface $generalFormHandler)
{
// Fallback in case 8.0 is used // TODO: Remove once we make 9.0 the minimum
if (\version_compare(\_PS_VERSION_, '8.1.0', '<')) {
parent::__construct();
}
$this->module = $module;
$this->validator = $validator;
$this->serverFormHandler = $serverFormHandler;
$this->generalFormHandler = $generalFormHandler;
$this->versioning = new Versioning();
}
/**
* @AdminSecurity("is_granted('read', request.get('_legacy_controller'))", message="Access denied.")
*
* @throws Exception
* @throws \Exception
*/
public function viewAction(Request $request): Response
{
// Build the client
$client = Client::createFromConfiguration($this->getConfiguration());
// Create the authorization URL (without redirect)
$authorizeUrl = ApiKey::getAuthorizeUrl($this->getConfiguration()->get(Constants::CONFIGURATION_BTCPAY_HOST), Constants::BTCPAY_PERMISSIONS, $this->module->name, true, true, null, $this->module->name);
// Ensure we always have a webhook
if (null !== $client && $client->isValid()) {
$client->webhook()->ensureWebhook($this->getConfiguration()->get(Constants::CONFIGURATION_BTCPAY_STORE_ID));
}
return $this->getResponse($request, $this->serverFormHandler->getForm(), $this->generalFormHandler->getForm(), $authorizeUrl, $client);
}
/**
* @AdminSecurity("is_granted('update', request.get('_legacy_controller'))", message="Access denied.")
*
* @return RedirectResponse|Response
*
* @throws Exception
*/
public function editServerSettingsAction(Request $request): Response
{
// Get configuration container
$shopConfiguration = $this->getConfiguration();
// Get current configuration, before processing everything
$currentConfiguration = Server::create($shopConfiguration);
$serverForm = $this->serverFormHandler->getForm();
$serverForm->handleRequest($request);
// Just show the boring configuration field on no submit/invalid form
if (!$serverForm->isSubmitted() || !$serverForm->isValid()) {
// Try and create the client
$client = Client::createFromConfiguration($this->getConfiguration());
// Create the authorization URL (without redirect)
$authorizeUrl = ApiKey::getAuthorizeUrl($this->getConfiguration()->get(Constants::CONFIGURATION_BTCPAY_HOST), Constants::BTCPAY_PERMISSIONS, $this->module->name, true, true, null, $this->module->name);
return $this->getResponse($request, $serverForm, $this->generalFormHandler->getForm(), $authorizeUrl, $client);
}
/** @var Server $submittedConfiguration */
$submittedConfiguration = $serverForm->getData();
// If there are errors in the form, error out here
if (0 !== \count($saveErrors = $this->serverFormHandler->save($submittedConfiguration->toArray()))) {
$this->flashErrors($saveErrors);
return $this->redirectToRoute('admin_btcpay_configure');
}
// If the configuration is the same, just stop
if ($submittedConfiguration->equals($currentConfiguration)) {
$this->addFlash('success', 'BTCPay Server Plugin: Settings have not changed.');
return $this->redirectToRoute('admin_btcpay_configure');
}
// If we are just removing the API key, do that and return
if (null === $submittedConfiguration->getApiKey() && !empty($currentConfiguration->getApiKey())) {
// Remove the current webhook to prevent issues in the future
if (false === (new Client($shopConfiguration->get(Constants::CONFIGURATION_BTCPAY_HOST), $currentConfiguration->getApiKey()))->webhook()->removeCurrent()) {
$this->addFlash('error', 'BTCPay Server Plugin: Could not remove webhook from the server. Please double check it is actually gone.');
}
$shopConfiguration->set(Constants::CONFIGURATION_BTCPAY_API_KEY, null);
$shopConfiguration->set(Constants::CONFIGURATION_BTCPAY_STORE_ID, null);
$shopConfiguration->set(Constants::CONFIGURATION_BTCPAY_WEBHOOK_ID, null);
$shopConfiguration->set(Constants::CONFIGURATION_BTCPAY_WEBHOOK_SECRET, null);
$this->addFlash('success', 'BTCPay Server plugin: API key has been removed');
return $this->redirectToRoute('admin_btcpay_configure');
}
// Before processing anything further, make sure any webhook that exists is removed
(new Client($currentConfiguration->getHost(), $currentConfiguration->getApiKey()))->webhook()->removeCurrent();
// If an API key is set, use that
if (null !== $submittedConfiguration->getApiKey()) {
return $this->processApiKey($submittedConfiguration);
}
// If nothing has been set, redirect to the host
return $this->processRedirect($request, $submittedConfiguration);
}
/**
* @AdminSecurity("is_granted('update', request.get('_legacy_controller'))", message="Access denied.")
*
* @return RedirectResponse|Response
*
* @throws Exception
*/
public function editGeneralSettingsAction(Request $request): Response
{
// Get current configuration, before processing everything
$currentConfiguration = General::create($this->getConfiguration());
$generalForm = $this->generalFormHandler->getForm();
$generalForm->handleRequest($request);
// Just show the boring configuration field on no submit/invalid form
if (!$generalForm->isSubmitted() || !$generalForm->isValid()) {
// Try and create the client
$client = Client::createFromConfiguration($this->getConfiguration());
// Create the authorization URL (without redirect)
$authorizeUrl = ApiKey::getAuthorizeUrl($this->getConfiguration()->get(Constants::CONFIGURATION_BTCPAY_HOST), Constants::BTCPAY_PERMISSIONS, $this->module->name, true, true, null, $this->module->name);
return $this->getResponse($request, $this->serverFormHandler->getForm(), $generalForm, $authorizeUrl, $client);
}
/** @var General $general */
$general = $generalForm->getData();
// If there are errors in the form, error out here
if (0 !== \count($saveErrors = $this->generalFormHandler->save($general->toArray()))) {
$this->flashErrors($saveErrors);
return $this->redirectToRoute('admin_btcpay_configure');
}
// If the configuration is the same, just stop
if ($general->equals($currentConfiguration)) {
$this->addFlash('success', 'BTCPay Server Plugin: Settings have not changed.');
return $this->redirectToRoute('admin_btcpay_configure');
}
// Return home
$this->addFlash('success', 'BTCPay Server Plugin: Settings have been saved.');
return $this->redirectToRoute('admin_btcpay_configure');
}
/**
* @return RedirectResponse|Response
*
* @throws Exception
*/
public function validateAPIKeyAction(Request $request): Response
{
// If we received an empty post we probably hit the PrestaShop security check
if (empty($request->request->all())) {
$this->addFlash('error', 'Did not receive data from BTCPay Server. If you received an <strong>Invalid Token</strong> page, make sure to properly setup PrestaShop and BTCPay Server (publicly accessible and HTTPS enabled). Please try again once done or use the API key option.');
// Make sure to reset the API key
$this->getConfiguration()->set(Constants::CONFIGURATION_BTCPAY_API_KEY, null);
return $this->redirectToRoute('admin_btcpay_configure');
}
// Validate incoming request and return any errors we encounter
$validateRequest = new ValidateApiKey($request->request);
if (0 !== \count($errors = $this->validator->validate($validateRequest))) {
/** @var ConstraintViolationInterface $error */
foreach ($errors as $error) {
$this->addFlash('error', $error->getMessage());
}
// Make sure to reset the API key
$this->getConfiguration()->set(Constants::CONFIGURATION_BTCPAY_API_KEY, null);
return $this->redirectToRoute('admin_btcpay_configure');
}
// Build the client
$client = new Client($this->getConfiguration()->get(Constants::CONFIGURATION_BTCPAY_HOST), $validateRequest->getApiKey());
// Get the store ID
$storeId = $validateRequest->getStoreID();
try {
// Ensure we have a valid BTCPay Server version
if (null !== ($info = $client->server()->getInfo()) && \version_compare($info->getVersion(), Constants::MINIMUM_BTCPAY_VERSION, '<')) {
$this->addFlash('error', \sprintf('BTCPay server version is too low. Expected %s or higher, received %s.', Constants::MINIMUM_BTCPAY_VERSION, $info->getVersion()));
PrestaShopLogger::addLog(\sprintf('[ERROR] BTCPay server version is too low. Expected %s or higher, received %s.', Constants::MINIMUM_BTCPAY_VERSION, $info->getVersion()), PrestaShopLogger::LOG_SEVERITY_LEVEL_ERROR);
// Make sure to reset the API key
$this->getConfiguration()->set(Constants::CONFIGURATION_BTCPAY_API_KEY, null);
return $this->redirectToRoute('admin_btcpay_configure');
}
// Ensure we have a payment methods setup
if (empty($client->payment()->getPaymentMethods($storeId))) {
$this->addFlash('error', \sprintf("This plugin expects a payment method to have been setup for store '%s'.", $client->store()->getStore($storeId)->offsetGet('name')));
PrestaShopLogger::addLog(\sprintf("[ERROR] This plugin expects a payment method to have been setup for store '%s'.", $client->store()->getStore($storeId)->offsetGet('name')), PrestaShopLogger::LOG_SEVERITY_LEVEL_ERROR);
// Make sure to reset the API key
$this->getConfiguration()->set(Constants::CONFIGURATION_BTCPAY_API_KEY, null);
return $this->redirectToRoute('admin_btcpay_configure');
}
// Ensure we have a webhook
$client->webhook()->ensureWebhook($storeId);
} catch (Throwable $throwable) {
$this->addFlash('error', \sprintf('BTCPay Server plugin: %s', $throwable->getMessage()));
PrestaShopLogger::addLog(\sprintf('[ERROR] An error occurred during configuration validation: %s', $throwable), PrestaShopLogger::LOG_SEVERITY_LEVEL_ERROR, $throwable->getCode());
return $this->redirectToRoute('admin_btcpay_configure');
}
// Store the API key and store ID we received
$this->getConfiguration()->set(Constants::CONFIGURATION_BTCPAY_API_KEY, $validateRequest->getApiKey());
$this->getConfiguration()->set(Constants::CONFIGURATION_BTCPAY_STORE_ID, $storeId);
$this->addFlash('success', 'BTCPay Server plugin: Your store and server have been linked!');
return $this->redirectToRoute('admin_btcpay_configure');
}
protected function getConfiguration(): ShopConfigurationInterface
{
// Fallback in case 8.0 is used // TODO: Remove once we make 9.0 the minimum
if (\version_compare(\_PS_VERSION_, '8.1.0', '<')) {
return $this->configuration;
}
return parent::getConfiguration();
}
/**
* @throws Exception
*/
private function processApiKey(Server $configuration): RedirectResponse
{
// Get configuration container
$shopConfiguration = $this->getConfiguration();
// Build the client
$client = new Client($configuration->getHost(), $configuration->getApiKey());
try {
// Validate created API key and return any errors we encounter
$validateKey = new ValidateApiKey(new ParameterBag($client->apiKey()->getCurrent()->getData()));
if (0 !== \count($errors = $this->validator->validate($validateKey))) {
/** @var ConstraintViolationInterface $error */
foreach ($errors as $error) {
$this->addFlash('error', $error->getMessage());
}
// Make sure to reset the API key
$shopConfiguration->set(Constants::CONFIGURATION_BTCPAY_API_KEY, null);
return $this->redirectToRoute('admin_btcpay_configure');
}
// Get the store ID
$storeId = $validateKey->getStoreID();
// Grab the store
$store = $client->store()->getStore($storeId);
// Ensure we have a valid BTCPay Server version
if (null !== ($info = $client->server()->getInfo()) && \version_compare($info->getVersion(), Constants::MINIMUM_BTCPAY_VERSION, '<')) {
$this->addFlash('error', \sprintf('BTCPay server version is too low. Expected %s or higher, received %s.', Constants::MINIMUM_BTCPAY_VERSION, $info->getVersion()));
PrestaShopLogger::addLog(\sprintf('[ERROR] BTCPay server version is too low. Expected %s or higher, received %s.', Constants::MINIMUM_BTCPAY_VERSION, $info->getVersion()), PrestaShopLogger::LOG_SEVERITY_LEVEL_ERROR);
// Make sure to reset the API key
$shopConfiguration->set(Constants::CONFIGURATION_BTCPAY_API_KEY, null);
return $this->redirectToRoute('admin_btcpay_configure');
}
// Ensure we have a payment methods setup
if (empty($client->payment()->getPaymentMethods($storeId))) {
$this->addFlash('error', \sprintf("This plugin expects a payment method to have been setup for store '%s'.", $store->offsetGet('name')));
PrestaShopLogger::addLog(\sprintf("[ERROR] This plugin expects a payment method to have been setup for store '%s'.", $store->offsetGet('name')), PrestaShopLogger::LOG_SEVERITY_LEVEL_ERROR);
// Make sure to reset the API key
$shopConfiguration->set(Constants::CONFIGURATION_BTCPAY_API_KEY, null);
return $this->redirectToRoute('admin_btcpay_configure');
}
// Save the new store ID
$shopConfiguration->set(Constants::CONFIGURATION_BTCPAY_STORE_ID, $storeId);
// Ensure we have a webhook
$client->webhook()->ensureWebhook($storeId);
} catch (Throwable $throwable) {
$this->addFlash('error', \sprintf('BTCPay Server plugin: %s', $throwable->getMessage()));
PrestaShopLogger::addLog(\sprintf('[ERROR] An error occurred during setup: %s', $throwable), PrestaShopLogger::LOG_SEVERITY_LEVEL_ERROR, $throwable->getCode());
// Ensure nothing is saved
$shopConfiguration->set(Constants::CONFIGURATION_BTCPAY_API_KEY, null);
$shopConfiguration->set(Constants::CONFIGURATION_BTCPAY_STORE_ID, null);
$shopConfiguration->set(Constants::CONFIGURATION_BTCPAY_WEBHOOK_ID, null);
$shopConfiguration->set(Constants::CONFIGURATION_BTCPAY_WEBHOOK_SECRET, null);
return $this->redirectToRoute('admin_btcpay_configure');
}
// Store the API key and store ID we received
$shopConfiguration->set(Constants::CONFIGURATION_BTCPAY_API_KEY, $validateKey->getApiKey());
$shopConfiguration->set(Constants::CONFIGURATION_BTCPAY_STORE_ID, $storeId);
$this->addFlash('success', 'BTCPay Server plugin: Your store and server have been linked!');
// Return home
return $this->redirectToRoute('admin_btcpay_configure');
}
/**
* @throws Exception
*/
private function processRedirect(Request $request, Server $configuration): RedirectResponse
{
// Get the store name and build the redirect URL
$storeName = $this->getContext()->shop->name;
$redirectUrl = $request->getSchemeAndHttpHost() . $this->getAdminLink('btcpay', ['route' => 'admin_btcpay_validate'], true);
// Create the authorization URL (with redirect)
$authorizeUrl = ApiKey::getAuthorizeUrl($configuration->getHost(), Constants::BTCPAY_PERMISSIONS, $storeName, true, true, $redirectUrl, $storeName);
// If there is no API key, redirect no matter what
if (empty($apiKey = $this->getConfiguration()->get(Constants::CONFIGURATION_BTCPAY_API_KEY))) {
return $this->redirect($authorizeUrl);
}
// If we have an apiKey, check if it's valid by fetching the storeId
try {
$client = new Client($configuration->getHost(), $apiKey);
// If we don't have a store ID, abort right away
if (null === ($storeID = $this->getConfiguration()->get(Constants::CONFIGURATION_BTCPAY_STORE_ID))) {
return $this->redirect($authorizeUrl);
}
// Ensure we have a webhook
$client->webhook()->ensureWebhook($storeID);
} catch (Throwable) {
// Reset BTCPay details
$this->getConfiguration()->set(Constants::CONFIGURATION_BTCPAY_API_KEY, null);
$this->getConfiguration()->set(Constants::CONFIGURATION_BTCPAY_WEBHOOK_ID, null);
$this->getConfiguration()->set(Constants::CONFIGURATION_BTCPAY_WEBHOOK_SECRET, null);
// Redirect away to get proper details
return $this->redirect($authorizeUrl);
}
// Return home
return $this->redirectToRoute('admin_btcpay_configure');
}
private function getResponse(Request $request, FormInterface $serverForm, FormInterface $generalForm, string $authorizeUrl, ?Client $client): Response
public function editAction(Request $request): Response
{
return $this->render('@Modules/btcpay/views/templates/admin/configure.html.twig', [
'server_form' => $serverForm->createView(),
'general_form' => $generalForm->createView(),
'form' => $this->get('prestashop.module.btcpay.form_handler')->getForm()->createView(),
'help_link' => $this->generateSidebarLink($request->attributes->get('_legacy_controller')),
'enableSidebar' => true,
]);
}
/**
* @AdminSecurity("is_granted('update', request.get('_legacy_controller'))", message="Access denied.")
*
* @return RedirectResponse|Response
*
* @throws \Exception
*/
public function editProcessAction(Request $request): Response
{
/** @var FormHandlerInterface $formHandler */
$formHandler = $this->get('prestashop.module.btcpay.form_handler');
$form = $formHandler->getForm();
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
$saveErrors = $formHandler->save($data);
if (0 === \count($saveErrors)) {
$this->addFlash('success', $this->trans('Successful update.', 'Admin.Notifications.Success'));
return $this->redirectToRoute('admin_btcpay_configure');
}
$this->flashErrors($saveErrors);
}
return $this->render('@Modules/btcpay/views/templates/admin/configure.html.twig', [
'form' => $form->createView(),
'help_link' => $this->generateSidebarLink($request->attributes->get('_legacy_controller')),
'storeId' => $this->getConfiguration()->get(Constants::CONFIGURATION_BTCPAY_STORE_ID),
'webhookId' => $this->getConfiguration()->get(Constants::CONFIGURATION_BTCPAY_WEBHOOK_ID),
'latestVersion' => $this->versioning->latest(),
'moduleVersion' => $this->module->version,
'authorizeUrl' => $authorizeUrl,
'client' => $client,
'enableSidebar' => true,
]);
}

View File

@ -1,11 +0,0 @@
<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

View File

@ -1,11 +0,0 @@
<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

View File

@ -1,11 +0,0 @@
<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

View File

@ -1,11 +0,0 @@
<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

View File

@ -2,10 +2,6 @@
namespace BTCPay\Entity;
if (!\defined('_PS_VERSION_')) {
exit;
}
/**
* phpcs:disable Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps
*/
@ -46,21 +42,55 @@ class BitcoinPayment extends \ObjectModel
*/
public $amount;
/**
* @var string|null
*/
public $bitcoin_price;
/**
* @var string|null
*/
public $bitcoin_paid;
/**
* @var string|null
*/
public $bitcoin_address;
/**
* @var string|null
*/
public $bitcoin_refund_address;
/**
* @var string|null
*/
public $redirect;
/**
* @var string|null
*/
public $rate;
/**
* {@inheritdoc}
*/
public static $definition = [
'table' => 'bitcoin_payment',
'primary' => 'id',
'fields' => [
'cart_id' => ['type' => self::TYPE_INT, 'required' => true, 'validate' => 'isUnsignedInt'],
'order_id' => ['type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'],
'status' => ['type' => self::TYPE_STRING, 'required' => true, 'validate' => 'isString'],
'invoice_id' => ['type' => self::TYPE_STRING, 'validate' => 'isString'],
'invoice_reference' => ['type' => self::TYPE_STRING, 'validate' => 'isString'],
'redirect' => ['type' => self::TYPE_STRING, 'validate' => 'isUrl'],
'cart_id' => ['type' => self::TYPE_INT, 'required' => true, 'validate' => 'isUnsignedInt'],
'order_id' => ['type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'],
'status' => ['type' => self::TYPE_STRING, 'required' => true, 'validate' => 'isString'],
'invoice_id' => ['type' => self::TYPE_STRING, 'validate' => 'isString'],
'invoice_reference' => ['type' => self::TYPE_STRING, 'validate' => 'isString'],
'amount' => ['type' => self::TYPE_STRING, 'validate' => 'isString'],
'bitcoin_price' => ['type' => self::TYPE_STRING, 'validate' => 'isString'],
'bitcoin_paid' => ['type' => self::TYPE_STRING, 'validate' => 'isString'],
'bitcoin_address' => ['type' => self::TYPE_STRING, 'validate' => 'isString'],
'bitcoin_refund_address' => ['type' => self::TYPE_STRING, 'validate' => 'isString'],
'redirect' => ['type' => self::TYPE_STRING, 'validate' => 'isUrl'],
'rate' => ['type' => self::TYPE_STRING, 'validate' => 'isString'],
],
];
@ -94,11 +124,6 @@ class BitcoinPayment extends \ObjectModel
$this->order_id = $order_id;
}
public function hasOrder(): bool
{
return false === empty($this->order_id);
}
public function getStatus(): string
{
return $this->status;
@ -109,10 +134,6 @@ class BitcoinPayment extends \ObjectModel
$this->status = $status;
}
/**
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
*/
public function getStatusName(): string
{
$name = $this->getStatus();
@ -123,7 +144,7 @@ class BitcoinPayment extends \ObjectModel
}
if (\is_array($orderState->name) && !empty($orderState->name)) {
return \array_pop($orderState->name);
return array_pop($orderState->name);
}
}
@ -160,6 +181,46 @@ class BitcoinPayment extends \ObjectModel
$this->amount = $amount;
}
public function getBitcoinPrice(): ?string
{
return $this->bitcoin_price;
}
public function setBitcoinPrice(?string $bitcoin_price): void
{
$this->bitcoin_price = $bitcoin_price;
}
public function getBitcoinPaid(): ?string
{
return $this->bitcoin_paid;
}
public function setBitcoinPaid(?string $bitcoin_paid): void
{
$this->bitcoin_paid = $bitcoin_paid;
}
public function getBitcoinAddress(): ?string
{
return $this->bitcoin_address;
}
public function setBitcoinAddress(?string $bitcoin_address): void
{
$this->bitcoin_address = $bitcoin_address;
}
public function getBitcoinRefundAddress(): ?string
{
return $this->bitcoin_refund_address;
}
public function setBitcoinRefundAddress(?string $bitcoin_refund_address): void
{
$this->bitcoin_refund_address = $bitcoin_refund_address;
}
public function getRedirect(): ?string
{
return $this->redirect;
@ -170,6 +231,16 @@ class BitcoinPayment extends \ObjectModel
$this->redirect = $redirect;
}
public function getRate(): ?string
{
return $this->rate;
}
public function setRate(?string $rate): void
{
$this->rate = $rate;
}
public function toArray(): array
{
return [
@ -180,7 +251,12 @@ class BitcoinPayment extends \ObjectModel
'invoice_id' => $this->getInvoiceId(),
'invoice_reference' => $this->getInvoiceReference(),
'amount' => $this->getAmount(),
'btc_price' => $this->getBitcoinPrice(),
'btc_paid' => $this->getBitcoinPaid(),
'btc_address' => $this->getBitcoinAddress(),
'btc_refundaddress' => $this->getBitcoinRefundAddress(),
'redirect' => $this->getRedirect(),
'rate' => $this->getRate(),
];
}
}

View File

@ -1,11 +0,0 @@
<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

View File

@ -2,10 +2,6 @@
namespace BTCPay\Exception;
if (!\defined('_PS_VERSION_')) {
exit;
}
class BTCPayException extends \RuntimeException
{
}

View File

@ -1,11 +0,0 @@
<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

View File

@ -1,49 +0,0 @@
<?php
namespace BTCPay\Factory;
use BTCPay\Repository\CustomerThreadRepository;
if (!\defined('_PS_VERSION_')) {
exit;
}
class CustomerMessage
{
/**
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
*/
public static function create(\CustomerThread $ct, string $message): \CustomerMessage
{
// Create a customer message
$cm = new \CustomerMessage();
$cm->id_customer_thread = $ct->id;
$cm->id_employee = 0;
$cm->message = $message;
$cm->private = true;
$cm->read = true;
// Ensure it is actually created
$cm->add();
return $cm;
}
/**
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
*/
public static function addToOrder(\Shop $shop, \Order $order, string $message): void
{
// Find or create the customer thread
$ct = CustomerThreadRepository::fetchOrCreate($shop, $order);
// Ensure the thread is open
$ct->status = 'open';
$ct->update();
// Create the message
self::create($ct, $message);
}
}

View File

@ -1,36 +0,0 @@
<?php
namespace BTCPay\Factory;
if (!\defined('_PS_VERSION_')) {
exit;
}
class CustomerThread
{
/**
* @throws \PrestaShopException
* @throws \PrestaShopDatabaseException
*/
public static function create(\Shop $shop, \Order $order): \CustomerThread
{
// Grab the customer from the order
$customer = $order->getCustomer();
// Create a customer thread
$ct = new \CustomerThread();
$ct->id_contact = 0;
$ct->id_customer = $order->id_customer;
$ct->id_shop = (int) $shop->id;
$ct->id_order = (int) $order->id;
$ct->id_lang = \Language::getIdByIso('en');
$ct->email = $customer->email;
$ct->status = 'open';
$ct->token = \Tools::passwdGen(12);
// Ensure it is actually created
$ct->add();
return $ct;
}
}

View File

@ -1,11 +0,0 @@
<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

View File

@ -0,0 +1,143 @@
<?php
namespace BTCPay\Form;
use BTCPay\Form\Data\Configuration;
use BTCPay\Server\Client;
use BTCPayServer\PrivateKey;
use BTCPayServer\PublicKey;
use BTCPayServer\SinKey;
use PrestaShop\PrestaShop\Core\Form\FormDataProviderInterface;
class ConfigureFormDataProvider implements FormDataProviderInterface
{
/**
* @return Configuration[]
*/
public function getData(): array
{
// Get BTCPay URL or use sane default
$serverUrl = \Configuration::get('BTCPAY_URL');
if (true === empty($serverUrl)) {
$serverUrl = 'https://testnet.demo.btcpayserver.org';
}
$configuration = new Configuration(
$serverUrl,
\Configuration::get('BTCPAY_TXSPEED'),
\Configuration::get('BTCPAY_ORDERMODE'),
\Configuration::get('BTCPAY_PAIRINGCODE')
);
return ['btcpay' => $configuration];
}
public function setData(array $data): array
{
/** @var Configuration $configuration */
$configuration = $data['btcpay'];
\Configuration::updateValue('BTCPAY_URL', rtrim($configuration->getUrl(), '/\\'));
\Configuration::updateValue('BTCPAY_TXSPEED', $configuration->getTransactionSpeed());
\Configuration::updateValue('BTCPAY_ORDERMODE', $configuration->getOrderMode());
// Only update the pairing code if it's different
if (\Configuration::get('BTCPAY_PAIRINGCODE') === $configuration->getPairingCode()) {
return [];
}
// Try and update the pairing code
if (!empty($errors = $this->processPairingCode($configuration->getUrl(), $configuration->getPairingCode()))) {
return $errors;
}
\Configuration::updateValue('BTCPAY_PAIRINGCODE', $configuration->getPairingCode());
return [];
}
private function processPairingCode(string $serverUrl, string $pairing_code): ?array
{
// Generate Private Key for api security
$privateKey = new PrivateKey();
$privateKey->generate();
// Generate Public Key
$publicKey = new PublicKey();
$publicKey->setPrivateKey($privateKey);
$publicKey->generate();
// Get SIN Key
$sinKey = new SinKey();
$sinKey->setPublicKey($publicKey);
$sinKey->generate();
// Create an API Client
$client = new Client();
$client->setUri(Client::getURI($serverUrl));
$client->setPrivateKey($privateKey);
$client->setPublicKey($publicKey);
$label = \Configuration::get('PS_SHOP_NAME');
try {
// Ignore notices and warnings for now
$old_error_reporting = error_reporting();
error_reporting(\E_ALL & ~\E_NOTICE & ~\E_STRICT & ~\E_DEPRECATED & ~\E_WARNING);
$token = $client->createToken(
[
'id' => (string) $sinKey,
'pairingCode' => $pairing_code,
'label' => $label,
]
);
error_reporting($old_error_reporting);
} catch (\Exception $e) {
\PrestaShopLogger::addLog('[ERROR] Issue creating token:' . $e->getMessage(), 3);
return [
[
'key' => sprintf('Failed to create token: %s', $e->getMessage()),
'domain' => 'Admin.Catalog.Notification',
'parameters' => [],
],
];
}
if (false === isset($token)) {
return [
[
'key' => 'Failed to create token, you are maybe using an already activated pairing code.',
'domain' => 'Admin.Catalog.Notification',
'parameters' => [],
],
];
}
// Encrypt or return errors
if (\is_array($pubEncrypted = $client->getEncryption()->encrypt($publicKey))) {
return $pubEncrypted;
}
// Encrypt or return errors
if (\is_array($tokenEncrypted = $client->getEncryption()->encrypt($token))) {
return $tokenEncrypted;
}
// Encrypt or return errors
if (\is_array($keyEncrypted = $client->getEncryption()->encrypt($privateKey))) {
return $keyEncrypted;
}
// Update our configuration with the new data
\Configuration::updateValue('BTCPAY_LABEL', $label);
\Configuration::updateValue('BTCPAY_PUB', (string) $pubEncrypted);
\Configuration::updateValue('BTCPAY_SIN', (string) $sinKey);
\Configuration::updateValue('BTCPAY_TOKEN', (string) $tokenEncrypted);
\Configuration::updateValue('BTCPAY_KEY', (string) $keyEncrypted);
return null;
}
}

View File

@ -0,0 +1,91 @@
<?php
namespace BTCPay\Form\Data;
use Symfony\Component\Validator\Constraints as Assert;
class Configuration
{
/**
* @Assert\Url()
* @Assert\NotNull()
* @Assert\NotBlank()
*
* @var string
*/
private $url;
/**
* @Assert\NotNull()
* @Assert\NotBlank()
* @Assert\Choice(choices=\BTCPay\Constants::TRANSACTION_SPEEDS, message="Invalid transaction speed")
*
* @var string
*/
private $transactionSpeed;
/**
* @Assert\NotNull()
* @Assert\NotBlank()
* @Assert\Choice(choices=\BTCPay\Constants::ORDER_MODES, message="Invalid order mode")
*
* @var string
*/
private $orderMode;
/**
* @Assert\NotBlank()
* @Assert\Regex(pattern="/^[a-zA-Z0-9]{7}$/", message="Invalid pairing code")
*
* @var string|null
*/
private $pairingCode;
public function __construct(string $url, string $transactionSpeed, string $orderMode, ?string $pairingCode)
{
$this->url = $url;
$this->transactionSpeed = $transactionSpeed;
$this->orderMode = $orderMode;
$this->pairingCode = $pairingCode;
}
public function getUrl(): string
{
return $this->url;
}
public function setUrl(string $url): void
{
$this->url = $url;
}
public function getTransactionSpeed(): string
{
return $this->transactionSpeed;
}
public function setTransactionSpeed(string $transaction_speed): void
{
$this->transactionSpeed = $transaction_speed;
}
public function getOrderMode(): string
{
return $this->orderMode;
}
public function setOrderMode(string $order_mode): void
{
$this->orderMode = $order_mode;
}
public function getPairingCode(): ?string
{
return $this->pairingCode;
}
public function setPairingCode(?string $pairing_code): void
{
$this->pairingCode = $pairing_code;
}
}

View File

@ -1,124 +0,0 @@
<?php
namespace BTCPay\Form\Data;
use BTCPay\Constants;
use BTCPayServer\Client\InvoiceCheckoutOptions;
use PrestaShop\PrestaShop\Core\Domain\Configuration\ShopConfigurationInterface;
use Symfony\Component\Validator\Constraints as Assert;
if (!\defined('_PS_VERSION_')) {
exit;
}
class General
{
/**
* @Assert\NotBlank()
* @Assert\Choice(choices=\BTCPay\Constants::TRANSACTION_SPEEDS, message="Invalid transaction speed")
*
* @var string
*/
private $speed;
/**
* @Assert\NotBlank()
* @Assert\Choice(choices=\BTCPay\Constants::ORDER_MODES, message="Invalid order mode")
*
* @var string
*/
private $orderMode;
/**
* @Assert\Choice(choices={true, false})
*/
private $protectOrders;
/**
* @Assert\Choice(choices={true, false})
*/
private $shareMetadata;
public function __construct(string $speed, string $orderMode, bool $protectOrders, bool $shareMetadata)
{
$this->speed = $speed;
$this->orderMode = $orderMode;
$this->protectOrders = $protectOrders;
$this->shareMetadata = $shareMetadata;
}
public static function create(ShopConfigurationInterface $configuration): self
{
return new self(
$configuration->get(Constants::CONFIGURATION_SPEED_MODE, InvoiceCheckoutOptions::SPEED_MEDIUM),
$configuration->get(Constants::CONFIGURATION_ORDER_MODE, Constants::ORDER_MODE_BEFORE),
(bool) $configuration->get(Constants::CONFIGURATION_PROTECT_ORDERS, true),
(bool) $configuration->get(Constants::CONFIGURATION_SHARE_METADATA, false),
);
}
public static function fromArray(array $data): self
{
return new self(
$data['speed'],
$data['orderMode'],
$data['protectOrders'],
$data['shareMetadata'],
);
}
public function getSpeed(): string
{
return $this->speed;
}
public function setSpeed(string $speed): void
{
$this->speed = $speed;
}
public function getOrderMode(): string
{
return $this->orderMode;
}
public function setOrderMode(string $order_mode): void
{
$this->orderMode = $order_mode;
}
public function getProtectOrders(): bool
{
return $this->protectOrders;
}
public function setProtectOrders(bool $protectOrders): void
{
$this->protectOrders = $protectOrders;
}
public function shareMetadata(): bool
{
return $this->shareMetadata;
}
public function setShareMetadata(bool $shareMetadata): void
{
$this->shareMetadata = $shareMetadata;
}
public function equals(self $general): bool
{
return $this->toArray() === $general->toArray();
}
public function toArray(): array
{
return [
'speed' => $this->speed,
'orderMode' => $this->orderMode,
'protectOrders' => $this->protectOrders,
'shareMetadata' => $this->shareMetadata,
];
}
}

View File

@ -1,84 +0,0 @@
<?php
namespace BTCPay\Form\Data;
use BTCPay\Constants;
use PrestaShop\PrestaShop\Core\Domain\Configuration\ShopConfigurationInterface;
use Symfony\Component\Validator\Constraints as Assert;
if (!\defined('_PS_VERSION_')) {
exit;
}
class Server
{
/**
* @Assert\Url()
* @Assert\NotBlank()
*
* @var string|null
*/
private $host;
/**
* @Assert\Type(type="alnum")
*
* @var string|null
*/
private $apiKey;
public function __construct(string $host, string $apiKey = null)
{
$this->host = $host;
$this->apiKey = $apiKey;
}
public static function create(ShopConfigurationInterface $configuration): self
{
return new self(
$configuration->get(Constants::CONFIGURATION_BTCPAY_HOST, Constants::CONFIGURATION_DEFAULT_HOST),
$configuration->get(Constants::CONFIGURATION_BTCPAY_API_KEY, null),
);
}
public static function fromArray(array $data): self
{
return new self(
$data['host'],
$data['apiKey'],
);
}
public function getHost(): ?string
{
return $this->host;
}
public function setHost(?string $host): void
{
$this->host = $host;
}
public function getApiKey(): ?string
{
return $this->apiKey;
}
public function setApiKey(?string $apiKey): void
{
$this->apiKey = $apiKey;
}
public function equals(self $configuration): bool
{
return $this->toArray() === $configuration->toArray();
}
public function toArray(): array
{
return [
'host' => $this->host,
'apiKey' => $this->apiKey,
];
}
}

View File

@ -1,11 +0,0 @@
<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

View File

@ -1,54 +0,0 @@
<?php
namespace BTCPay\Form;
use BTCPay\Constants;
use BTCPay\Form\Data\General;
use PrestaShop\PrestaShop\Adapter\Configuration as PrestaShopConfiguration;
use PrestaShop\PrestaShop\Core\Form\FormDataProviderInterface;
if (!\defined('_PS_VERSION_')) {
exit;
}
class GeneralFormDataProvider implements FormDataProviderInterface
{
/**
* @var PrestaShopConfiguration
*/
private $configuration;
public function __construct()
{
$this->configuration = new PrestaShopConfiguration();
}
public function getData(): General
{
return General::create($this->configuration);
}
/**
* @throws \Exception
*/
public function setData(array $data): array
{
// Re-create configuration element with form data
$configuration = General::fromArray($data);
if ($this->configuration->get(Constants::CONFIGURATION_SPEED_MODE) !== ($speedMode = $configuration->getSpeed()) && !empty($speedMode)) {
$this->configuration->set(Constants::CONFIGURATION_SPEED_MODE, $speedMode);
}
if ($this->configuration->get(Constants::CONFIGURATION_ORDER_MODE) !== ($orderMode = $configuration->getOrderMode()) && !empty($orderMode)) {
$this->configuration->set(Constants::CONFIGURATION_ORDER_MODE, $orderMode);
}
if ($this->configuration->get(Constants::CONFIGURATION_SHARE_METADATA) !== ($shareMetadata = $configuration->shareMetadata())) {
$this->configuration->set(Constants::CONFIGURATION_SHARE_METADATA, $shareMetadata);
}
// All is fine
return [];
}
}

View File

@ -1,50 +0,0 @@
<?php
namespace BTCPay\Form;
use BTCPay\Constants;
use BTCPay\Form\Data\Server;
use PrestaShop\PrestaShop\Adapter\Configuration as PrestaShopConfiguration;
use PrestaShop\PrestaShop\Core\Form\FormDataProviderInterface;
if (!\defined('_PS_VERSION_')) {
exit;
}
class ServerFormDataProvider implements FormDataProviderInterface
{
/**
* @var PrestaShopConfiguration
*/
private $configuration;
public function __construct()
{
$this->configuration = new PrestaShopConfiguration();
}
public function getData(): Server
{
return Server::create($this->configuration);
}
/**
* @throws \Exception
*/
public function setData(array $data): array
{
// Re-create configuration element with form data
$configuration = Server::fromArray($data);
if ($this->configuration->get(Constants::CONFIGURATION_BTCPAY_HOST) !== \rtrim(\trim(($host = $configuration->getHost())), '/\\') && !empty($host)) {
$this->configuration->set(Constants::CONFIGURATION_BTCPAY_HOST, \rtrim(\trim($host), '/\\'));
}
if ($this->configuration->get(Constants::CONFIGURATION_BTCPAY_API_KEY) !== \rtrim(\trim(($apiKey = $configuration->getApiKey())), '/\\') && !empty($apiKey)) {
$this->configuration->set(Constants::CONFIGURATION_BTCPAY_API_KEY, \rtrim(\trim($apiKey), '/\\'));
}
// All is fine
return [];
}
}

View File

@ -0,0 +1,62 @@
<?php
namespace BTCPay\Form\Type;
use BTCPay\Constants;
use BTCPay\Form\Data\Configuration;
use BTCPayServer\Invoice;
use PrestaShopBundle\Form\Admin\Type\TranslatorAwareType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class ConfigureType extends TranslatorAwareType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('url', UrlType::class, [
'label' => $this->trans('BTCPay server url', 'Modules.Btcpay.Admin'),
])
->add('transaction_speed', ChoiceType::class, [
'choices' => [
$this->trans('Low', 'Modules.Btcpay.Admin') => Invoice::TRANSACTION_SPEED_LOW,
$this->trans('Medium', 'Modules.Btcpay.Admin') => Invoice::TRANSACTION_SPEED_MEDIUM,
$this->trans('High', 'Modules.Btcpay.Admin') => Invoice::TRANSACTION_SPEED_HIGH,
],
'label' => $this->trans('Transaction speed', 'Modules.Btcpay.Admin'),
])
->add('order_mode', ChoiceType::class, [
'choices' => [
$this->trans('Order before payment', 'Modules.Btcpay.Admin') => Constants::ORDER_MODE_BEFORE,
$this->trans('Order after payment', 'Modules.Btcpay.Admin') => Constants::ORDER_MODE_AFTER,
],
'label' => $this->trans('Order mode', 'Modules.Btcpay.Admin'),
])
->add('pairing_code', TextType::class, [
'label' => $this->trans('Pairing code', 'Modules.Btcpay.Admin'),
'required' => false,
]);
}
/**
* {@inheritdoc}
*/
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults(['data_class' => Configuration::class]);
}
/**
* {@inheritdoc}
*/
public function getBlockPrefix(): string
{
return 'module_btcpay';
}
}

View File

@ -1,68 +0,0 @@
<?php
namespace BTCPay\Form\Type;
use BTCPay\Constants;
use BTCPay\Form\Data\General;
use BTCPayServer\Client\InvoiceCheckoutOptions;
use PrestaShopBundle\Form\Admin\Type\TranslatorAwareType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
if (!\defined('_PS_VERSION_')) {
exit;
}
class GeneralType extends TranslatorAwareType
{
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('speed', ChoiceType::class, [
'choices' => [
$this->trans('Low', 'Modules.Btcpay.Admin') => InvoiceCheckoutOptions::SPEED_LOW,
$this->trans('Medium', 'Modules.Btcpay.Admin') => InvoiceCheckoutOptions::SPEED_MEDIUM,
$this->trans('High', 'Modules.Btcpay.Admin') => InvoiceCheckoutOptions::SPEED_HIGH,
],
'label' => $this->trans('Transaction speed', 'Modules.Btcpay.Admin'),
'help' => $this->trans('Determines the transaction fee that we recommend to the customer.', 'Modules.Btcpay.Admin'),
'empty_data' => InvoiceCheckoutOptions::SPEED_MEDIUM,
])
->add('orderMode', ChoiceType::class, [
'choices' => [
$this->trans('Order before payment', 'Modules.Btcpay.Admin') => Constants::ORDER_MODE_BEFORE,
$this->trans('Order after payment', 'Modules.Btcpay.Admin') => Constants::ORDER_MODE_AFTER,
],
'label' => $this->trans('Order creation method', 'Modules.Btcpay.Admin'),
'help' => $this->trans('Will we create the order as soon as the user gets redirect to BTCPay Server or do we wait for the webhook.', 'Modules.Btcpay.Admin'),
'empty_data' => Constants::ORDER_MODE_BEFORE,
])
->add('protectOrders', ChoiceType::class, [
'choices' => [
$this->trans('Yes', 'Modules.Btcpay.Admin') => true,
$this->trans('No', 'Modules.Btcpay.Admin') => false,
],
'label' => $this->trans('Protect order status', 'Modules.Btcpay.Admin'),
'help' => $this->trans('Will protect the order status from changing to "failed" if it already has a "paid" order state. This will protect an order from being cancelled via webhook, if it was paid via a different payment gateway.', 'Modules.Btcpay.Admin'),
'empty_data' => true,
])
->add('shareMetadata', ChoiceType::class, [
'choices' => [
$this->trans('Yes', 'Modules.Btcpay.Admin') => true,
$this->trans('No', 'Modules.Btcpay.Admin') => false,
],
'label' => $this->trans('Send customer data to BTCPay Server', 'Modules.Btcpay.Admin'),
'help' => $this->trans('If you want customer email, address, etc. sent to BTCPay Server enable this option. By default for privacy and GDPR reasons this is disabled.', 'Modules.Btcpay.Admin'),
'empty_data' => false,
]);
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults(['data_class' => General::class]);
}
}

View File

@ -1,56 +0,0 @@
<?php
namespace BTCPay\Form\Type;
use BTCPay\Form\Data\Server;
use PrestaShop\PrestaShop\Adapter\Configuration;
use PrestaShopBundle\Form\Admin\Type\TranslatorAwareType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\Extension\Core\Type\UrlType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\Translation\TranslatorInterface;
if (!\defined('_PS_VERSION_')) {
exit;
}
class ServerType extends TranslatorAwareType
{
public function __construct(TranslatorInterface $translator, array $locales)
{
parent::__construct($translator, $locales);
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options): void
{
/** @var Server $data */
$data = $builder->getData() ?? Server::create(new Configuration());
$builder
->add('host', UrlType::class, [
'label' => $this->trans('BTCPay Server URL', 'Modules.Btcpay.Admin'),
'help' => $this->trans('The URL/host to your BTCPay Server instance. Make sure your node is reachable from the internet.', 'Modules.Btcpay.Admin'),
'required' => true,
])
->add('apiKey', TextType::class, [
'label' => $this->trans('BTCPay Server API key', 'Modules.Btcpay.Admin'),
'attr' => [
'pattern' => '[a-zA-Z0-9]+',
'placeholder' => empty($data->getApiKey())
? $this->trans('Keep blank to be redirected for authentication', 'Modules.Btcpay.Admin')
: $this->trans('Removing the API key will disconnect your store', 'Modules.Btcpay.Admin'),
],
'required' => false,
'empty_data' => null,
]);
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults(['data_class' => Server::class]);
}
}

View File

@ -1,11 +0,0 @@
<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

View File

@ -1,11 +0,0 @@
<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

View File

@ -1,84 +0,0 @@
<?php
namespace BTCPay\Github;
use Composer\Semver\Comparator;
if (!\defined('_PS_VERSION_')) {
exit;
}
class Latest
{
/**
* @var int
*/
private $id;
/**
* @var string
*/
private $name;
/**
* @var string
*/
private $tagName;
/**
* @var string
*/
private $commit;
/**
* @var string
*/
private $url;
public function __construct(int $id, string $name, string $tagName, string $commit, string $url)
{
$this->id = $id;
$this->name = $name;
$this->tagName = $tagName;
$this->commit = $commit;
$this->url = $url;
}
public static function create(array $data): self
{
return new self($data['id'], $data['name'], $data['tag_name'], $data['target_commitish'], $data['html_url']);
}
public function getId(): int
{
return $this->id;
}
public function getName(): string
{
return $this->name;
}
public function getTagName(): string
{
return $this->tagName;
}
public function getCommit(): string
{
return $this->commit;
}
public function getUrl(): string
{
return $this->url;
}
public function newer(string $currentVersion): bool
{
return Comparator::greaterThan(
\str_replace('v', '', $this->getTagName()),
\str_replace('v', '', $currentVersion)
);
}
}

View File

@ -1,80 +0,0 @@
<?php
namespace BTCPay\Github;
use BTCPay\Constants;
use BTCPay\Server\CurlAdapter;
use Symfony\Component\Cache\Adapter\FilesystemAdapter;
use Symfony\Component\HttpFoundation\Request;
if (!\defined('_PS_VERSION_')) {
exit;
}
class Versioning
{
private const HEADERS = [
'Accept' => 'application/json',
'Content-Type' => 'application/json',
'User-Agent' => 'btcpayserver/prestashop-plugin',
];
/**
* @var FilesystemAdapter
*/
private $cache;
/**
* @var CurlAdapter
*/
private $client;
public function __construct()
{
$this->cache = new FilesystemAdapter();
$this->client = new CurlAdapter();
}
public function latest(): ?Latest
{
try {
// Check if we have a recent check cached
$cachedUpdate = $this->cache->getItem(Constants::LASTEST_VERSION_CACHE_KEY);
if ($cachedUpdate->isHit() && !empty($cachedData = $cachedUpdate->get())) {
return Latest::create($cachedData);
}
// Fetch the latest version
$response = $this->client->request(Request::METHOD_GET, Constants::GITHUB_API_LATEST_ENDPOINT, self::HEADERS);
// If the request failed, stop bothering
if (200 !== $response->getStatus()) {
\PrestaShopLogger::addLog(\sprintf('[WARNING] Could not check for latest version, received status: %s', $response->getBody()), \PrestaShopLogger::LOG_SEVERITY_LEVEL_WARNING, $response->getStatus());
return null;
}
// Decode JSON response
$data = \json_decode($response->getBody(), true, 512, \JSON_THROW_ON_ERROR);
// If the data is empty (or the request failed), return null
if (empty($data) || false === \array_key_exists('tag_name', $data) || (\array_key_exists('message', $data) && 'Not Found' === $data['message'])) {
return null;
}
// Set updated data
$cachedUpdate->expiresAfter(Constants::LASTEST_VERSION_CACHE_EXPIRATION);
$cachedUpdate->set($data);
// Save updated data
$this->cache->save($cachedUpdate);
// Finally, return the data
return Latest::create($cachedUpdate->get());
} catch (\Throwable $exception) {
\PrestaShopLogger::addLog(\sprintf('[INFO] Could not check for latest version, caught exception: %s', $exception->getMessage()), \PrestaShopLogger::LOG_SEVERITY_LEVEL_INFORMATIVE, $exception->getCode());
return null;
}
}
}

View File

@ -1,11 +0,0 @@
<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

View File

@ -2,40 +2,22 @@
namespace BTCPay\Installer;
use BTCPay\Constants;
use BTCPayServer\Client\InvoiceCheckoutOptions;
use PrestaShop\PrestaShop\Adapter\Configuration;
if (!\defined('_PS_VERSION_')) {
exit;
}
use Configuration;
class Config
{
/**
* @var Configuration
*/
private $configuration;
public function __construct()
{
$this->configuration = new Configuration();
}
/**
* @throws \Exception
*/
public function install(): array
{
// Ensure sane defaults
if (!$this->configuration->set(Constants::CONFIGURATION_BTCPAY_HOST, Constants::CONFIGURATION_DEFAULT_HOST)
|| !$this->configuration->set(Constants::CONFIGURATION_SPEED_MODE, InvoiceCheckoutOptions::SPEED_MEDIUM)
|| !$this->configuration->set(Constants::CONFIGURATION_ORDER_MODE, Constants::ORDER_MODE_BEFORE)
|| !$this->configuration->set(Constants::CONFIGURATION_BTCPAY_API_KEY, null)
|| !$this->configuration->set(Constants::CONFIGURATION_BTCPAY_STORE_ID, null)
|| !$this->configuration->set(Constants::CONFIGURATION_BTCPAY_WEBHOOK_ID, null)
|| !$this->configuration->set(Constants::CONFIGURATION_BTCPAY_WEBHOOK_SECRET, null)
|| !$this->configuration->set(Constants::CONFIGURATION_SHARE_METADATA, false)) {
// Init clear configurations
if (!Configuration::updateValue('BTCPAY_URL', null)
|| !Configuration::updateValue('BTCPAY_LABEL', null)
|| !Configuration::updateValue('BTCPAY_PAIRINGCODE', null)
|| !Configuration::updateValue('BTCPAY_KEY', null)
|| !Configuration::updateValue('BTCPAY_PUB', null)
|| !Configuration::updateValue('BTCPAY_SIN', null)
|| !Configuration::updateValue('BTCPAY_TOKEN', null)
|| !Configuration::updateValue('BTCPAY_TXSPEED', null)
|| !Configuration::updateValue('BTCPAY_ORDERMODE', null)) {
return [
[
'key' => 'Could not init configuration',
@ -48,20 +30,18 @@ class Config
return [];
}
/**
* @throws \Exception
*/
public function uninstall(): array
{
// Remove configuration
if (!$this->configuration->remove(Constants::CONFIGURATION_BTCPAY_HOST)
|| !$this->configuration->remove(Constants::CONFIGURATION_SPEED_MODE)
|| !$this->configuration->remove(Constants::CONFIGURATION_ORDER_MODE)
|| !$this->configuration->remove(Constants::CONFIGURATION_BTCPAY_API_KEY)
|| !$this->configuration->remove(Constants::CONFIGURATION_BTCPAY_STORE_ID)
|| !$this->configuration->remove(Constants::CONFIGURATION_BTCPAY_WEBHOOK_ID)
|| !$this->configuration->remove(Constants::CONFIGURATION_BTCPAY_WEBHOOK_SECRET)
|| !$this->configuration->remove(Constants::CONFIGURATION_SHARE_METADATA)) {
if (!Configuration::deleteByName('BTCPAY_URL')
|| !Configuration::deleteByName('BTCPAY_LABEL')
|| !Configuration::deleteByName('BTCPAY_PAIRINGCODE')
|| !Configuration::deleteByName('BTCPAY_KEY')
|| !Configuration::deleteByName('BTCPAY_PUB')
|| !Configuration::deleteByName('BTCPAY_SIN')
|| !Configuration::deleteByName('BTCPAY_TOKEN')
|| !Configuration::deleteByName('BTCPAY_TXSPEED')
|| !Configuration::deleteByName('BTCPAY_ORDERMODE')) {
return [
[
'key' => 'Could not clear configuration',

View File

@ -4,10 +4,6 @@ namespace BTCPay\Installer;
use BTCPay;
if (!\defined('_PS_VERSION_')) {
exit;
}
class Hooks
{
/**
@ -24,6 +20,8 @@ class Hooks
{
if (!$this->module->registerHook('displayAdminOrderMainBottom')
|| !$this->module->registerHook('displayOrderDetail')
|| !$this->module->registerHook('displayPaymentEU')
|| !$this->module->registerHook('payment')
|| !$this->module->registerHook('paymentReturn')
|| !$this->module->registerHook('paymentOptions')
|| !$this->module->registerHook('actionCartSave')) {

View File

@ -2,14 +2,10 @@
namespace BTCPay\Installer;
use BTCPay\Constants;
use Configuration;
use Language;
use OrderState;
use PrestaShop\PrestaShop\Adapter\Configuration;
if (!\defined('_PS_VERSION_')) {
exit;
}
use PrestaShopCollection;
class OrderStates
{
@ -18,32 +14,20 @@ class OrderStates
*/
private $moduleName;
/**
* @var Configuration
*/
private $configuration;
public function __construct(string $moduleName)
{
$this->moduleName = $moduleName;
$this->configuration = new Configuration();
$this->moduleName = $moduleName;
}
/**
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
* @throws \Exception
*/
public function install(): array
{
$errors = [];
// Check and insert "awaiting payment" if needed.
if (!$this->configuration->get(Constants::CONFIGURATION_ORDER_STATE_WAITING)
|| !\Validate::isLoadedObject(new OrderState($this->configuration->get(Constants::CONFIGURATION_ORDER_STATE_WAITING)))) {
if (false === $this->installAwaiting()) {
if (!Configuration::get('BTCPAY_OS_WAITING') || !\Validate::isLoadedObject(new OrderState(Configuration::get('BTCPAY_OS_WAITING')))) {
if (false === ($this->installAwaiting())) {
$errors[] = [
'key' => \sprintf('Could not add new order state: %s', Constants::CONFIGURATION_ORDER_STATE_WAITING),
'key' => 'Could not add new order state: BTCPAY_OS_WAITING',
'parameters' => [],
'domain' => 'Admin.Modules.Notification',
];
@ -51,11 +35,10 @@ class OrderStates
}
// Check and insert "confirming payment" if needed.
if (!$this->configuration->get(Constants::CONFIGURATION_ORDER_STATE_CONFIRMING)
|| !\Validate::isLoadedObject(new OrderState($this->configuration->get(Constants::CONFIGURATION_ORDER_STATE_CONFIRMING)))) {
if (false === $this->installConfirming()) {
if (!Configuration::get('BTCPAY_OS_CONFIRMING') || !\Validate::isLoadedObject(new OrderState(Configuration::get('BTCPAY_OS_CONFIRMING')))) {
if (false === ($this->installConfirming())) {
$errors[] = [
'key' => \sprintf('Could not add new order state: %s', Constants::CONFIGURATION_ORDER_STATE_CONFIRMING),
'key' => 'Could not add new order state: BTCPAY_OS_CONFIRMING',
'parameters' => [],
'domain' => 'Admin.Modules.Notification',
];
@ -63,11 +46,10 @@ class OrderStates
}
// Check and insert "failed payment" if needed.
if (!$this->configuration->get(Constants::CONFIGURATION_ORDER_STATE_FAILED)
|| !\Validate::isLoadedObject(new OrderState($this->configuration->get(Constants::CONFIGURATION_ORDER_STATE_FAILED)))) {
if (false === $this->installFailed()) {
if (!Configuration::get('BTCPAY_OS_FAILED') || !\Validate::isLoadedObject(new OrderState(Configuration::get('BTCPAY_OS_FAILED')))) {
if (false === ($this->installFailed())) {
$errors[] = [
'key' => \sprintf('Could not add new order state: %s', Constants::CONFIGURATION_ORDER_STATE_FAILED),
'key' => 'Could not add new order state: BTCPAY_OS_FAILED',
'parameters' => [],
'domain' => 'Admin.Modules.Notification',
];
@ -75,11 +57,35 @@ class OrderStates
}
// Check and insert "payment succeeded" if needed.
if (!$this->configuration->get(Constants::CONFIGURATION_ORDER_STATE_PAID)
|| !\Validate::isLoadedObject(new OrderState($this->configuration->get(Constants::CONFIGURATION_ORDER_STATE_PAID)))) {
if (false === $this->installPaid()) {
if (!Configuration::get('BTCPAY_OS_PAID') || !\Validate::isLoadedObject(new OrderState(Configuration::get('BTCPAY_OS_PAID')))) {
if (false === ($this->installPaid())) {
$errors[] = [
'key' => \sprintf('Could not add new order state: %s', Constants::CONFIGURATION_ORDER_STATE_PAID),
'key' => 'Could not add new order state: BTCPAY_OS_PAID',
'parameters' => [],
'domain' => 'Admin.Modules.Notification',
];
}
}
return $errors;
}
public function uninstall(): array
{
$collection = new PrestaShopCollection('OrderState');
$collection->where('module_name', '=', $this->moduleName);
if (empty($orderStates = $collection->getResults())) {
return [];
}
$errors = [];
/** @var OrderState $orderState */
foreach ($orderStates as $orderState) {
if (false === $orderState->delete()) {
$errors[] = [
'key' => 'Could not delete order state ' . $orderState->name,
'parameters' => [],
'domain' => 'Admin.Modules.Notification',
];
@ -89,11 +95,6 @@ class OrderStates
return $errors;
}
/**
* @throws \PrestaShopException
* @throws \PrestaShopDatabaseException
* @throws \Exception
*/
private function installAwaiting(): bool
{
$order_state = new OrderState();
@ -103,7 +104,7 @@ class OrderStates
$order_state->module_name = $this->moduleName;
foreach (Language::getLanguages(true, false, true) as $languageId) {
$order_state->name[$languageId] = 'Awaiting crypto payment';
$order_state->name[$languageId] = 'Awaiting Bitcoin payment';
}
if (false === $order_state->add()) {
@ -111,16 +112,11 @@ class OrderStates
}
$this->installImage($order_state, 'os_bitcoin_waiting.png');
$this->configuration->set(Constants::CONFIGURATION_ORDER_STATE_WAITING, (int) $order_state->id);
Configuration::updateValue('BTCPAY_OS_WAITING', (int) $order_state->id);
return true;
}
/**
* @throws \PrestaShopException
* @throws \PrestaShopDatabaseException
* @throws \Exception
*/
private function installConfirming(): bool
{
$order_state = new OrderState();
@ -130,7 +126,7 @@ class OrderStates
$order_state->module_name = $this->moduleName;
foreach (Language::getLanguages(true, false, true) as $languageId) {
$order_state->name[$languageId] = 'Waiting for confirmations';
$order_state->name[$languageId] = 'Waiting for Bitcoin confirmations';
}
if (false === $order_state->add()) {
@ -138,16 +134,11 @@ class OrderStates
}
$this->installImage($order_state, 'os_bitcoin_confirming.png');
$this->configuration->set(Constants::CONFIGURATION_ORDER_STATE_CONFIRMING, (int) $order_state->id);
Configuration::updateValue('BTCPAY_OS_CONFIRMING', (int) $order_state->id);
return true;
}
/**
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
* @throws \Exception
*/
private function installFailed(): bool
{
$order_state = new OrderState();
@ -160,7 +151,7 @@ class OrderStates
$order_state->module_name = $this->moduleName;
foreach (Language::getLanguages(true, false, true) as $languageId) {
$order_state->name[$languageId] = 'Crypto transaction failed';
$order_state->name[$languageId] = 'Bitcoin transaction failed';
}
if (false === $order_state->add()) {
@ -168,16 +159,11 @@ class OrderStates
}
$this->installImage($order_state, 'os_bitcoin_failed.png');
$this->configuration->set(Constants::CONFIGURATION_ORDER_STATE_FAILED, (int) $order_state->id);
Configuration::updateValue('BTCPAY_OS_FAILED', (int) $order_state->id);
return true;
}
/**
* @throws \PrestaShopException
* @throws \PrestaShopDatabaseException
* @throws \Exception
*/
private function installPaid(): bool
{
$order_state = new OrderState();
@ -189,11 +175,10 @@ class OrderStates
$order_state->color = '#108510';
$order_state->logable = true;
$order_state->unremovable = true;
$order_state->invoice = true;
$order_state->module_name = $this->moduleName;
foreach (Language::getLanguages(true, false, true) as $languageId) {
$order_state->name[$languageId] = 'Paid with crypto';
$order_state->name[$languageId] = 'Paid with Bitcoin';
}
if (false === $order_state->add()) {
@ -201,15 +186,15 @@ class OrderStates
}
$this->installImage($order_state, 'os_bitcoin_paid.png');
$this->configuration->set(Constants::CONFIGURATION_ORDER_STATE_PAID, (int) $order_state->id);
Configuration::updateValue('BTCPAY_OS_PAID', (int) $order_state->id);
return true;
}
private function installImage(OrderState $order_state, string $image_name): void
{
$source = \_PS_MODULE_DIR_ . $this->moduleName . '/views/images/' . $image_name;
$destination = \_PS_ROOT_DIR_ . '/img/os/' . (int) $order_state->id . '.gif';
\copy($source, $destination);
$source = _PS_MODULE_DIR_ . $this->moduleName . '/views/images/' . $image_name;
$destination = _PS_ROOT_DIR_ . '/img/os/' . (int) $order_state->id . '.gif';
copy($source, $destination);
}
}

View File

@ -2,39 +2,27 @@
namespace BTCPay\Installer;
use BTCPay\Repository\TableRepository;
if (!\defined('_PS_VERSION_')) {
exit;
}
use BTCPay\Repository\BitcoinPaymentRepository;
class Tables
{
/**
* @var TableRepository
* @var BitcoinPaymentRepository
*/
private $tableRepository;
private $repository;
public function __construct(TableRepository $repository)
public function __construct(BitcoinPaymentRepository $repository)
{
$this->tableRepository = $repository;
$this->repository = $repository;
}
/**
* @throws \JsonException
* @throws \Doctrine\DBAL\Exception
*/
public function install(): array
{
return $this->tableRepository->createTables();
return $this->repository->createTables();
}
/**
* @throws \JsonException
* @throws \Doctrine\DBAL\Exception
*/
public function uninstall(): array
{
return $this->tableRepository->dropTables();
return $this->repository->dropTables();
}
}

View File

@ -1,50 +0,0 @@
<?php
namespace BTCPay\Installer;
use BTCPay\Server\Client;
use PrestaShop\PrestaShop\Adapter\Configuration;
if (!\defined('_PS_VERSION_')) {
exit;
}
class Webhook
{
/**
* @var Configuration
*/
private $configuration;
public function __construct()
{
$this->configuration = new Configuration();
}
/**
* @throws \Exception
*/
public function uninstall(): array
{
// Build the client from our stored configuration
$client = Client::createFromConfiguration($this->configuration);
// If there is no client, return now
if (null === $client) {
return [];
}
// Remove the current webhook to prevent issues in the future.
if (false === ($client->webhook()->removeCurrent())) {
return [
[
'key' => 'Could not remove webhook from the server. Please double check it is actually gone.',
'parameters' => [],
'domain' => 'Admin.Modules.Notification',
],
];
}
return [];
}
}

View File

@ -1,11 +0,0 @@
<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

View File

@ -1,352 +0,0 @@
<?php
namespace BTCPay\Invoice;
use BTCPay\Constants;
use BTCPay\Entity\BitcoinPayment;
use BTCPay\Repository\OrderPaymentRepository;
use BTCPay\Server\Client;
use BTCPayServer\Result\Invoice;
use PrestaShop\PrestaShop\Adapter\Configuration;
if (!\defined('_PS_VERSION_')) {
exit;
}
class Processor
{
/**
* @var \BTCPay
*/
private $module;
/**
* @var \Context
*/
private $context;
/**
* @var Client
*/
private $client;
/**
* @var Configuration
*/
private $configuration;
public function __construct(\BTCPay $module, \Context $context, Configuration $configuration, Client $client)
{
$this->module = $module;
$this->context = $context;
$this->configuration = $configuration;
$this->client = $client;
}
/**
* @throws \JsonException
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
*/
public function invoiceSettled(BitcoinPayment $bitcoinPayment): void
{
// Get the order
$order = new \Order($bitcoinPayment->getOrderId());
// Set the default status to be the current status
$orderStatus = $order->current_state;
// Get the store ID
$storeID = $this->configuration->get(Constants::CONFIGURATION_BTCPAY_STORE_ID);
// Grab the invoice from the server
$invoice = $this->client->invoice()->getInvoice($storeID, $bitcoinPayment->getInvoiceId());
// Ensure the invoice is not processing
if ($invoice->isProcessing()) {
\PrestaShopLogger::addLog(\sprintf("[ERROR] Invoice '%s' should not be processing when 'InvoiceSettled' has been received", $invoice->getId()), \PrestaShopLogger::LOG_SEVERITY_LEVEL_ERROR, null, 'Order', $order->id);
return;
}
// Change state if it's settled
if ($invoice->isSettled()) {
$orderStatus = (string) $this->configuration->get(Constants::CONFIGURATION_ORDER_STATE_PAID);
}
// Transaction was marked completed via BTCPay Server
if ($invoice->isMarked()) {
$orderStatus = (string) $this->configuration->get(Constants::CONFIGURATION_ORDER_STATE_PAID);
}
// If nothing changed, return
if ((string) $order->current_state === $orderStatus) {
\PrestaShopLogger::addLog('[INFO] The state is the same as the one received', \PrestaShopLogger::LOG_SEVERITY_LEVEL_INFORMATIVE, null, 'Order', $order->id);
return;
}
// Store the updated status
$this->updateOrderStatus($bitcoinPayment, $orderStatus);
}
/**
* @throws \JsonException
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
*/
public function invoiceFailed(BitcoinPayment $bitcoinPayment): void
{
// Get the order
$order = new \Order($bitcoinPayment->getOrderId());
// Set the default status to be the current status
$orderStatus = $order->current_state;
// Get the store ID
$storeID = $this->configuration->get(Constants::CONFIGURATION_BTCPAY_STORE_ID);
// Grab the invoice from the server
$invoice = $this->client->invoice()->getInvoice($storeID, $bitcoinPayment->getInvoiceId());
// Change the order status if needed
if ($invoice->isInvalid() || $invoice->isExpired()) {
// Expiration for the invoice has passed, so mark it failed
$orderStatus = (string) $this->configuration->get(Constants::CONFIGURATION_ORDER_STATE_FAILED);
}
// Transaction was marked as invalid via BTCPay Server
if ($invoice->isMarked()) {
$orderStatus = (string) $this->configuration->get(Constants::CONFIGURATION_ORDER_STATE_FAILED);
}
// If nothing changed, return
if ((string) $order->current_state === $orderStatus) {
\PrestaShopLogger::addLog('[INFO] The state is the same as the one received', \PrestaShopLogger::LOG_SEVERITY_LEVEL_INFORMATIVE, null, 'Order', $order->id);
return;
}
// Update the status
$this->updateOrderStatus($bitcoinPayment, $orderStatus);
}
/**
* @throws \JsonException
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
*/
public function paymentReceived(BitcoinPayment $bitcoinPayment): void
{
// Get the order
$order = new \Order($bitcoinPayment->getOrderId());
// Get the store ID
$storeID = $this->configuration->get(Constants::CONFIGURATION_BTCPAY_STORE_ID);
// Grab the invoice from the server
$invoice = $this->client->invoice()->getInvoice($storeID, $bitcoinPayment->getInvoiceId());
// Set the default status to be the current status
$orderStatus = $order->current_state;
// If partially paid, we are still waiting for more
if ($invoice->isPartiallyPaid()) {
$orderStatus = (string) $this->configuration->get(Constants::CONFIGURATION_ORDER_STATE_WAITING);
}
// Transaction received, but we have to wait some confirmation
if ($invoice->isProcessing()) {
$orderStatus = (string) $this->configuration->get(Constants::CONFIGURATION_ORDER_STATE_CONFIRMING);
}
// Transaction received, but paid late
if ($invoice->isPaidLate()) {
// Transaction received but we have to wait some confirmation
$orderStatus = (string) $this->configuration->get(Constants::CONFIGURATION_ORDER_STATE_CONFIRMING);
}
// Transaction received, but overpaid
if ($invoice->isOverpaid()) {
// Transaction received but we have to wait some confirmation
$orderStatus = (string) $this->configuration->get(Constants::CONFIGURATION_ORDER_STATE_CONFIRMING);
}
// Invoice confirmed on the network
if ($invoice->isSettled()) {
// Transaction received and already confirmed
$orderStatus = (string) $this->configuration->get(Constants::CONFIGURATION_ORDER_STATE_PAID);
}
// If nothing changed, return
if ((string) $order->current_state === $orderStatus) {
\PrestaShopLogger::addLog(\sprintf("[INFO] The state is the same as the one received for invoice '%s'", $invoice->getId()), \PrestaShopLogger::LOG_SEVERITY_LEVEL_INFORMATIVE, null, 'Order', $order->id);
return;
}
// Store the updated status
$this->updateOrderStatus($bitcoinPayment, $orderStatus);
}
/**
* @throws \JsonException
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
*/
public function paymentReceivedCreateAfter(BitcoinPayment $bitcoinPayment): void
{
// Generate an order only if there is not another one with this cart
if (null !== \Order::getByCartId($bitcoinPayment->getCartId())) {
// We already have an order, so process as normal
$this->paymentReceived($bitcoinPayment);
return;
}
// Get the store ID
$storeID = $this->configuration->get(Constants::CONFIGURATION_BTCPAY_STORE_ID);
// Grab the invoice from the server
$invoice = $this->client->invoice()->getInvoice($storeID, $bitcoinPayment->getInvoiceId());
// Set an initial state
$orderStatus = (string) $this->configuration->get(Constants::CONFIGURATION_ORDER_STATE_WAITING);
// If partially paid, we are still waiting for more
if ($invoice->isPartiallyPaid()) {
$orderStatus = (string) $this->configuration->get(Constants::CONFIGURATION_ORDER_STATE_WAITING);
}
// Transaction received, but we have to wait some confirmation
if ($invoice->isProcessing()) {
$orderStatus = (string) $this->configuration->get(Constants::CONFIGURATION_ORDER_STATE_CONFIRMING);
}
// Transaction received, but paid late
if ($invoice->isPaidLate()) {
// Transaction received but we have to wait some confirmation
$orderStatus = (string) $this->configuration->get(Constants::CONFIGURATION_ORDER_STATE_CONFIRMING);
// Add a regular log
\PrestaShopLogger::addLog(\sprintf("[INFO] User paid after expiration for this invoice '%s'", $invoice->getId()), \PrestaShopLogger::LOG_SEVERITY_LEVEL_INFORMATIVE);
}
// Transaction received, but overpaid
if ($invoice->isOverpaid()) {
// Transaction received but we have to wait some confirmation
$orderStatus = (string) $this->configuration->get(Constants::CONFIGURATION_ORDER_STATE_CONFIRMING);
}
// Invoice confirmed on the network
if ($invoice->isSettled()) {
// Transaction received and already confirmed
$orderStatus = (string) $this->configuration->get(Constants::CONFIGURATION_ORDER_STATE_PAID);
}
// Fetch the secure key, which is used to check if the order has been made from this store
if (null === ($invoiceData = $invoice->getData()) || !\array_key_exists('metadata', $invoiceData) || !\array_key_exists('posData', $invoiceData['metadata'])) {
\PrestaShopLogger::addLog('[ERROR] Secure key was not defined', \PrestaShopLogger::LOG_SEVERITY_LEVEL_ERROR);
return;
}
// Grab the secure key
$secureKey = $invoiceData['metadata']['posData'];
\PrestaShopLogger::addLog(\sprintf("[INFO] Creating actual order for invoice '%s'", $invoice->getId()), \PrestaShopLogger::LOG_SEVERITY_LEVEL_INFORMATIVE, null, 'BitcoinPayment', $bitcoinPayment->getId());
$this->module->validateOrder(
$bitcoinPayment->getCartId(),
$orderStatus,
$bitcoinPayment->getAmount(),
$this->module->displayName,
null,
[],
null,
false,
$secureKey,
$this->context->shop
);
// Get the new order ID
$order = \Order::getByCartId($bitcoinPayment->getCartId());
// Store the new order ID and set the proper status
$bitcoinPayment->setOrderId($order->id);
$bitcoinPayment->setStatus($orderStatus);
// Update the object
if (false === $bitcoinPayment->update(true)) {
$error = \sprintf('[ERROR] Could not update bitcoin_payment: %s', \Db::getInstance()->getMsgError());
\PrestaShopLogger::addLog($error, \PrestaShopLogger::LOG_SEVERITY_LEVEL_ERROR, null, 'BitcoinPayment', $bitcoinPayment->getId());
throw new \RuntimeException($error);
}
}
/**
* @throws \JsonException
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
*/
public function paymentSettled(BitcoinPayment $bitcoinPayment): void
{
// Get the order
$order = new \Order($bitcoinPayment->getOrderId());
// Get the store ID
$storeID = $this->configuration->get(Constants::CONFIGURATION_BTCPAY_STORE_ID);
// Grab the payments from the server
$paymentMethods = $this->client->invoice()->getPaymentMethods($storeID, $bitcoinPayment->getInvoiceId());
// Process all payments
foreach ($paymentMethods as $paymentMethod) {
// Grab all payments per payment method
$payments = $paymentMethod->getPayments();
// Process all payments
foreach ($payments as $payment) {
// Payment is not yet settled, continue
if (Invoice::STATUS_SETTLED !== $payment->getStatus()) {
continue;
}
// Payment is known, continue
if (OrderPaymentRepository::hasPayment($order, $payment)) {
continue;
}
// Add the payment
$order->addOrderPayment(\bcmul($payment->getValue(), $paymentMethod->getRate()), null, $payment->getTransactionId());
}
}
}
/**
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
*/
private function updateOrderStatus(BitcoinPayment $bitcoinPayment, string $orderStatus): void
{
// Set the status
$bitcoinPayment->setStatus($orderStatus);
// Update the object
if (false === $bitcoinPayment->update(true)) {
$error = \sprintf('[ERROR] Could not update bitcoin_payment: %s', \Db::getInstance()->getMsgError());
\PrestaShopLogger::addLog($error, \PrestaShopLogger::LOG_SEVERITY_LEVEL_ERROR, null, 'BitcoinPayment', $bitcoinPayment->getId());
throw new \RuntimeException($error);
}
// Add the order change to the order history table
$orderHistory = new \OrderHistory();
$orderHistory->id_order = $bitcoinPayment->getOrderId();
// Store the change and make sure to create an invoice using existing payments (in case the status is changed to 'paid with crypto')
$orderHistory->changeIdOrderState($orderStatus, $bitcoinPayment->getOrderId(), true);
$orderHistory->add();
}
}

View File

@ -1,11 +0,0 @@
<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

View File

@ -0,0 +1,116 @@
<?php
namespace BTCPay;
use BTCPay\Entity\BitcoinPayment;
class LegacyOrderBitcoinRepository
{
/**
* @var \Db
*/
private $connection;
public function __construct()
{
$this->connection = \Db::getInstance();
}
public function create(int $cartId, string $status, string $invoiceId): BitcoinPayment
{
$orderBitcoin = new BitcoinPayment();
$orderBitcoin->setCartId($cartId);
$orderBitcoin->setStatus($status);
$orderBitcoin->setInvoiceId($invoiceId);
if (false === $orderBitcoin->save(true)) {
\PrestaShopLogger::addLog('[ERROR] Could not store bitcoin_payment', 3);
throw new \RuntimeException('[ERROR] Could not store bitcoin_payment');
}
\PrestaShopLogger::addLog('Created bitcoin_payment for invoice ' . $invoiceId);
return $orderBitcoin;
}
public function getOneByInvoiceID(string $invoiceId): ?BitcoinPayment
{
$query = new \DbQuery();
$query->select('bp.*')
->from('bitcoin_payment', 'bp')
->where(sprintf('bp.invoice_id = "%s"', $invoiceId))
->limit(1);
$result = $this->connection->query($query);
if (0 !== ($errorCode = (int) $result->errorCode())) {
throw new \PrestaShopDatabaseException(json_encode($result->errorInfo()), $errorCode);
}
if (false === ($object = $result->fetchObject(BitcoinPayment::class))) {
return null;
}
return $object;
}
public function getOneByInvoiceReference(string $invoiceReference): ?BitcoinPayment
{
$query = new \DbQuery();
$query->select('bp.*')
->from('bitcoin_payment', 'bp')
->where(sprintf('bp.invoice_reference = "%s"', $invoiceReference))
->limit(1);
$result = $this->connection->query($query);
if (0 !== ($errorCode = (int) $result->errorCode())) {
throw new \PrestaShopDatabaseException(json_encode($result->errorInfo()), $errorCode);
}
if (false === ($object = $result->fetchObject(BitcoinPayment::class))) {
return null;
}
return $object;
}
public function getOneByCartID(int $cartID): ?BitcoinPayment
{
$query = new \DbQuery();
$query->select('bp.*')
->from('bitcoin_payment', 'bp')
->where(sprintf('bp.cart_id = "%s"', $cartID))
->limit(1);
$result = $this->connection->query($query);
if (0 !== ($errorCode = (int) $result->errorCode())) {
throw new \PrestaShopDatabaseException(json_encode($result->errorInfo()), $errorCode);
}
if (false === ($object = $result->fetchObject(BitcoinPayment::class))) {
return null;
}
return $object;
}
public function getOneByOrderID(int $orderID): ?BitcoinPayment
{
$query = new \DbQuery();
$query->select('bp.*')
->from('bitcoin_payment', 'bp')
->where(sprintf('bp.order_id = "%s"', $orderID))
->limit(1);
$result = $this->connection->query($query);
if (0 !== ($errorCode = (int) $result->errorCode())) {
throw new \PrestaShopDatabaseException(json_encode($result->errorInfo()), $errorCode);
}
if (false === ($object = \Db::getInstance()->query($query)->fetchObject(BitcoinPayment::class))) {
return null;
}
return $object;
}
}

View File

@ -2,128 +2,88 @@
namespace BTCPay\Repository;
use BTCPay\Entity\BitcoinPayment;
if (!\defined('_PS_VERSION_')) {
exit;
}
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
class BitcoinPaymentRepository
{
/**
* @throws \PrestaShopException
* @var Connection
*/
public static function create(int $cartId, string $status, string $invoiceId): BitcoinPayment
private $connection;
/**
* @var string
*/
private $prefix;
public function __construct(Connection $connection, string $prefix)
{
$bitcoinPayment = new BitcoinPayment();
$bitcoinPayment->setCartId($cartId);
$bitcoinPayment->setStatus($status);
$bitcoinPayment->setInvoiceId($invoiceId);
if (false === $bitcoinPayment->save(true)) {
\PrestaShopLogger::addLog('[ERROR] Could not store bitcoin_payment', \PrestaShopLogger::LOG_SEVERITY_LEVEL_ERROR);
throw new \RuntimeException('[ERROR] Could not store bitcoin_payment');
}
\PrestaShopLogger::addLog('[INFO] Created bitcoin_payment for invoice ' . $invoiceId, \PrestaShopLogger::LOG_SEVERITY_LEVEL_INFORMATIVE, null, 'BitcoinPayment', $bitcoinPayment->getId());
return $bitcoinPayment;
$this->connection = $connection;
$this->prefix = $prefix;
}
/**
* @throws \PrestaShopDatabaseException
* @throws \JsonException
* @throws DBALException
*/
public static function getOneByInvoiceID(string $invoiceId): ?BitcoinPayment
public function createTables(): array
{
$query = new \DbQuery();
$query->select('bp.*')
->from('bitcoin_payment', 'bp')
->where(\sprintf('bp.invoice_id = "%s"', $invoiceId))
->limit(1);
$errors = [];
$engine = _MYSQL_ENGINE_;
$result = \Db::getInstance()->query($query);
if (0 !== ($errorCode = (int) $result->errorCode())) {
throw new \PrestaShopDatabaseException(\json_encode($result->errorInfo(), \JSON_THROW_ON_ERROR), $errorCode);
$queries = [
"CREATE TABLE IF NOT EXISTS `{$this->prefix}bitcoin_payment`(
`id` int(11) NOT NULL AUTO_INCREMENT,
`cart_id` int(11) NOT NULL,
`order_id` int(11),
`status` varchar(255) NOT NULL,
`invoice_id` varchar(255),
`invoice_reference` varchar(255),
`amount` varchar(255),
`bitcoin_price` varchar(255),
`bitcoin_paid` varchar(255),
`bitcoin_address` varchar(255),
`bitcoin_refund_address` varchar(255),
`redirect` varchar(255),
`rate` varchar(255),
PRIMARY KEY (`id`),
UNIQUE KEY `invoice_id` (`invoice_id`)
) ENGINE=$engine DEFAULT CHARSET=utf8",
];
foreach ($queries as $query) {
// Execute query
$statement = $this->connection->executeQuery($query);
if (0 !== (int) $statement->errorCode()) {
$errors[] = [
'key' => json_encode($statement->errorInfo()),
'parameters' => [],
'domain' => 'Admin.Modules.Notification',
];
}
}
if (false === ($object = $result->fetchObject(BitcoinPayment::class))) {
return null;
}
return $object;
return $errors;
}
/**
* @throws \PrestaShopDatabaseException
* @throws \JsonException
* @throws DBALException
*/
public static function getOneByInvoiceReference(string $invoiceReference): ?BitcoinPayment
public function dropTables(): array
{
$query = new \DbQuery();
$query->select('bp.*')
->from('bitcoin_payment', 'bp')
->where(\sprintf('bp.invoice_reference = "%s"', $invoiceReference))
->limit(1);
$errors = [];
$query = "DROP TABLE IF EXISTS `{$this->prefix}bitcoin_payment`";
$result = \Db::getInstance()->query($query);
if (0 !== ($errorCode = (int) $result->errorCode())) {
throw new \PrestaShopDatabaseException(\json_encode($result->errorInfo(), \JSON_THROW_ON_ERROR), $errorCode);
// Execute query
$statement = $this->connection->executeQuery($query);
if (0 !== (int) $statement->errorCode()) {
$errors[] = [
'key' => json_encode($statement->errorInfo()),
'parameters' => [],
'domain' => 'Admin.Modules.Notification',
];
}
if (false === ($object = $result->fetchObject(BitcoinPayment::class))) {
return null;
}
return $object;
}
/**
* @throws \PrestaShopDatabaseException
* @throws \JsonException
*/
public static function getOneByCartID(int $cartID): ?BitcoinPayment
{
$query = new \DbQuery();
$query->select('bp.*')
->from('bitcoin_payment', 'bp')
->where(\sprintf('bp.cart_id = "%s"', $cartID))
->limit(1);
$result = \Db::getInstance()->query($query);
if (0 !== ($errorCode = (int) $result->errorCode())) {
throw new \PrestaShopDatabaseException(\json_encode($result->errorInfo(), \JSON_THROW_ON_ERROR), $errorCode);
}
if (false === ($object = $result->fetchObject(BitcoinPayment::class))) {
return null;
}
return $object;
}
/**
* @throws \PrestaShopDatabaseException
* @throws \JsonException
*/
public static function getOneByOrderID(int $orderID): ?BitcoinPayment
{
$query = new \DbQuery();
$query->select('bp.*')
->from('bitcoin_payment', 'bp')
->where(\sprintf('bp.order_id = "%s"', $orderID))
->limit(1);
$result = \Db::getInstance()->query($query);
if (0 !== ($errorCode = (int) $result->errorCode())) {
throw new \PrestaShopDatabaseException(\json_encode($result->errorInfo(), \JSON_THROW_ON_ERROR), $errorCode);
}
if (false === ($object = $result->fetchObject(BitcoinPayment::class))) {
return null;
}
return $object;
return $errors;
}
}

View File

@ -1,30 +0,0 @@
<?php
namespace BTCPay\Repository;
use BTCPay\Factory\CustomerThread;
if (!\defined('_PS_VERSION_')) {
exit;
}
class CustomerThreadRepository
{
/**
* @throws \PrestaShopException
* @throws \PrestaShopDatabaseException
*/
public static function fetchOrCreate(\Shop $shop, \Order $order): \CustomerThread
{
// Get the customer
$customer = $order->getCustomer();
// Check if we need to create a thread, if so, create it and return it
if (false === ($threadId = \CustomerThread::getIdCustomerThreadByEmailAndIdOrder($customer->email, $order->id))) {
return CustomerThread::create($shop, $order);
}
// Return the existing thread
return new \CustomerThread((int) $threadId);
}
}

View File

@ -1,25 +0,0 @@
<?php
namespace BTCPay\Repository;
use BTCPayServer\Result\InvoicePayment;
use PrestaShopCollection;
if (!\defined('_PS_VERSION_')) {
exit;
}
class OrderPaymentRepository
{
/**
* @throws \PrestaShopException
*/
public static function hasPayment(\Order $order, InvoicePayment $payment): bool
{
$order_payments = new PrestaShopCollection(\OrderPayment::class);
$order_payments->where('order_reference', '=', $order->reference);
$order_payments->where('transaction_id', '=', $payment->getTransactionId());
return false === empty($order_payments->getResults());
}
}

View File

@ -1,80 +0,0 @@
<?php
namespace BTCPay\Repository;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception;
if (!\defined('_PS_VERSION_')) {
exit;
}
class TableRepository
{
/**
* @var Connection
*/
private $connection;
public function __construct(Connection $connection)
{
$this->connection = $connection;
$this->prefix = \_DB_PREFIX_;
}
/**
* @throws \JsonException
*/
public function createTables(): array
{
$errors = [];
$engine = \_MYSQL_ENGINE_;
$queries = [
"CREATE TABLE IF NOT EXISTS `{$this->prefix}bitcoin_payment`(
`id` int(11) NOT NULL AUTO_INCREMENT,
`cart_id` int(11) NOT NULL,
`order_id` int(11),
`status` varchar(255) NOT NULL,
`invoice_id` varchar(255),
`invoice_reference` varchar(255),
`amount` varchar(255),
`bitcoin_price` varchar(255),
`bitcoin_paid` varchar(255),
`bitcoin_address` varchar(255),
`bitcoin_refund_address` varchar(255),
`redirect` varchar(255),
`rate` varchar(255),
PRIMARY KEY (`id`),
UNIQUE KEY `invoice_id` (`invoice_id`)
) ENGINE=$engine DEFAULT CHARSET=utf8",
];
try {
foreach ($queries as $query) {
$this->connection->executeQuery($query);
}
} catch (Exception $e) {
$errors[] = ['key' => \json_encode($e->getMessage(), \JSON_THROW_ON_ERROR), 'parameters' => [], 'domain' => 'Admin.Modules.Notification'];
}
return $errors;
}
/**
* @throws \JsonException
*/
public function dropTables(): array
{
$errors = [];
$query = "DROP TABLE IF EXISTS `{$this->prefix}bitcoin_payment`";
try {
$this->connection->executeQuery($query);
} catch (Exception $e) {
$errors[] = ['key' => \json_encode($e->getMessage(), \JSON_THROW_ON_ERROR), 'parameters' => [], 'domain' => 'Admin.Modules.Notification'];
}
return $errors;
}
}

View File

@ -1,11 +0,0 @@
<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

View File

@ -2,141 +2,37 @@
namespace BTCPay\Server;
use BTCPay\Constants;
use BTCPay\Repository\BitcoinPaymentRepository;
use BTCPayServer\Client\AbstractClient;
use BTCPayServer\Client\ApiKey as ApiKeyClient;
use BTCPayServer\Client\Invoice as InvoiceClient;
use BTCPayServer\Client\Server as ServerClient;
use BTCPayServer\Client\Store as StoreClient;
use BTCPayServer\Client\StorePaymentMethod;
use PrestaShop\PrestaShop\Adapter\Configuration;
use PrestaShop\PrestaShop\Core\Domain\Configuration\ShopConfigurationInterface;
use BTCPay\LegacyOrderBitcoinRepository;
use BTCPayServer\Client\Client as BaseClient;
use BTCPayServer\PrivateKey;
use BTCPayServer\TokenInterface;
if (!\defined('_PS_VERSION_')) {
exit;
}
class Client extends AbstractClient
class Client extends BaseClient
{
/**
* @var InvoiceClient
* @var Encryption
*/
private $invoice;
private $encryption;
/**
* @var ApiKeyClient
* @var LegacyOrderBitcoinRepository
*/
private $apiKey;
private $repository;
/**
* @var ServerClient
*/
private $server;
/**
* @var StoreClient
*/
private $store;
/**
* @var StorePaymentMethod
*/
private $payment;
/**
* @var Webhook
*/
private $webhook;
/**
* @var Configuration
*/
private $configuration;
public function __construct(string $baseUrl, string $apiKey)
public function __construct()
{
$httpClient = new CurlAdapter();
$this->encryption = new Encryption();
$this->repository = new LegacyOrderBitcoinRepository();
parent::__construct($baseUrl, $apiKey, $httpClient);
$this->apiKey = new ApiKeyClient($baseUrl, $apiKey, $httpClient);
$this->invoice = new InvoiceClient($baseUrl, $apiKey, $httpClient);
$this->server = new ServerClient($baseUrl, $apiKey, $httpClient);
$this->store = new StoreClient($baseUrl, $apiKey, $httpClient);
$this->payment = new StorePaymentMethod($baseUrl, $apiKey, $httpClient);
$this->webhook = new Webhook($baseUrl, $apiKey, $httpClient);
$this->configuration = new Configuration();
// Set our own CURL adapter, always
$this->setAdapter(new CurlAdapter());
}
public static function createFromConfiguration(ShopConfigurationInterface $configuration): ?self
public function getEncryption(): Encryption
{
$host = $configuration->get(Constants::CONFIGURATION_BTCPAY_HOST, null);
$apiKey = $configuration->get(Constants::CONFIGURATION_BTCPAY_API_KEY, null);
// Cannot create a client, if we do not have valid configuration
if (empty($host) || empty($apiKey)) {
return null;
}
return new self($host, $apiKey);
return $this->encryption;
}
public function getBaseUrl(): string
{
return parent::getBaseUrl();
}
public function invoice(): InvoiceClient
{
return $this->invoice;
}
public function apiKey(): ApiKeyClient
{
return $this->apiKey;
}
public function server(): ServerClient
{
return $this->server;
}
public function store(): StoreClient
{
return $this->store;
}
public function payment(): StorePaymentMethod
{
return $this->payment;
}
public function webhook(): Webhook
{
return $this->webhook;
}
public function isValid(): bool
{
try {
// Test the server connection
$this->server()->getInfo();
// Test the store connection
$this->store()->getStore($this->configuration->get(Constants::CONFIGURATION_BTCPAY_STORE_ID))->getName();
} catch (\Throwable) {
return false;
}
return true;
}
/**
* @throws \PrestaShopDatabaseException
* @throws \JsonException
*/
public function getBTCPayRedirect(\Cart $cart): ?string
{
// Check if we have a cart ID we can use
@ -144,23 +40,62 @@ class Client extends AbstractClient
return null;
}
if (null === ($bitcoinPayment = BitcoinPaymentRepository::getOneByCartID($cart->id))) {
if (null === ($orderBitcoin = $this->repository->getOneByCartID($cart->id))) {
return null;
}
if (empty($redirect = $bitcoinPayment->getRedirect())) {
if (empty($redirect = $orderBitcoin->getRedirect())) {
return null;
}
// Get the store ID
$storeID = $this->configuration->get(Constants::CONFIGURATION_BTCPAY_STORE_ID);
$errorReporting = error_reporting();
error_reporting(\E_ALL & ~\E_NOTICE & ~\E_STRICT & ~\E_DEPRECATED & ~\E_WARNING);
$invoice = $this->getInvoice($orderBitcoin->getInvoiceId());
error_reporting($errorReporting);
// Check the invoice status
$invoice = $this->invoice->getInvoice($storeID, $bitcoinPayment->getInvoiceId());
if ($invoice->isInvalid() || $invoice->isExpired()) {
$status = $invoice->getStatus();
if ('invalid' === $status || 'expired' === $status) {
return null;
}
return $redirect;
}
public static function createFromConfiguration(): self
{
$client = new self();
$client->setUri(self::getURI(\Configuration::get('BTCPAY_URL')));
$privateKey = $client->getEncryption()->decrypt(\Configuration::get('BTCPAY_KEY'));
if (!$privateKey instanceof PrivateKey) {
throw new \RuntimeException('Could not decrypted the stored private key', 3);
}
$token = $client->getEncryption()->decrypt(\Configuration::get('BTCPAY_TOKEN'));
if (!$token instanceof TokenInterface) {
throw new \RuntimeException('Could not decrypted the stored token', 3);
}
$client->setPrivateKey($privateKey);
$client->setPublicKey($privateKey->getPublicKey());
// We need to do some extra work for the token
$client->setToken(Token::createToken($token->getToken()));
return $client;
}
/**
* Inject ports into the URL, because the client somehow demands a port.
*/
public static function getURI(string $url): string
{
// Sanitize URL first
$url = rtrim($url, '/\\');
if ('https' === (parse_url($url, \PHP_URL_SCHEME))) {
return sprintf('%s:443', $url);
}
return sprintf('%s:80', $url);
}
}

View File

@ -2,15 +2,12 @@
namespace BTCPay\Server;
use BTCPayServer\Http\CurlClient;
use BTCPayServer\Http\ResponseInterface;
use BTCPayServer\Client\Adapter\CurlAdapter as BaseCurlAdapter;
use BTCPayServer\Client\RequestInterface;
use BTCPayServer\Client\ResponseInterface;
use STS\Backoff\Backoff;
if (!\defined('_PS_VERSION_')) {
exit;
}
class CurlAdapter extends CurlClient
class CurlAdapter extends BaseCurlAdapter
{
/**
* @var int
@ -22,9 +19,11 @@ class CurlAdapter extends CurlClient
*/
private $backoff;
public function __construct()
public function __construct(array $curlOptions = [])
{
$this->backoff = new Backoff(self::$defaultMaxAttempts, Backoff::$defaultStrategy, self::$defaultMaxAttempts * 5 * 1000, true);
parent::__construct($curlOptions);
$this->backoff = new Backoff(self::$defaultMaxAttempts, Backoff::$defaultStrategy, self::$defaultMaxAttempts * 10 * 1000, true);
}
/**
@ -32,16 +31,16 @@ class CurlAdapter extends CurlClient
*
* Four things can happen:
* 1. This call succeeds and we return like normal
* 2. The call fails, and we retry 3 times using the default strategy, if any retry succeeds, we return it
* 3. The call fails, and we retry 3 times using the default strategy, if all retries fail, it will throw the last error
* 4. The maximum waiting time exceeds, and we will just throw the last error
* 2. The call fails and we retry 3 times using the default strategy, if any retry succeeds, we return it
* 3. The call fails and we retry 3 times using the default strategy, if all retries fail, it will throw the last error
* 4. The maximum waiting time exceeds and we will just throw the last error
*
* @throws \Exception
*/
public function request(string $method, string $url, array $headers = [], string $body = ''): ResponseInterface
public function sendRequest(RequestInterface $request): ResponseInterface
{
return $this->backoff->run(function () use ($method, $url, $headers, $body) {
return parent::request($method, $url, $headers, $body);
return $this->backoff->run(function () use ($request) {
return parent::sendRequest($request);
});
}
}

View File

@ -1,91 +0,0 @@
<?php
namespace BTCPay\Server\Data;
use BTCPay\Constants;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\Validator\Constraints as Assert;
if (!\defined('_PS_VERSION_')) {
exit;
}
class ValidateApiKey
{
/**
* @Assert\NotBlank()
*
* @var string|null
*/
private $apiKey;
/**
* @Assert\All({@Assert\NotBlank()})
* @Assert\NotBlank()
*
* @var string[]
*/
private $permissions;
public function __construct(ParameterBag $request)
{
$this->apiKey = $request->get('apiKey');
$this->permissions = $request->get('permissions', []);
}
public function getApiKey(): ?string
{
return $this->apiKey;
}
public function getStoreID(): string
{
return \explode(':', $this->permissions[0])[1];
}
/**
* @Assert\IsTrue(message="This plugin expects all passed permissions to be given. Please remove and recreate the API key")
*/
public function hasRequiredPermissions(): bool
{
$permissions = \array_reduce($this->permissions, static function (array $carry, string $permission) {
return \array_merge($carry, [\explode(':', $permission)[0]]);
}, []);
return empty(\array_merge(
\array_diff(Constants::BTCPAY_PERMISSIONS, $permissions),
\array_diff($permissions, Constants::BTCPAY_PERMISSIONS)
));
}
/**
* @Assert\IsTrue(message="This plugin requires one store (and one store only) to be authorized. Please remove and recreate the API key.")
*/
public function hasSingleStore(): bool
{
$storeId = null;
foreach ($this->permissions as $perms) {
if (2 !== \count($exploded = \explode(':', $perms))) {
return false;
}
if (null === ($receivedStoreId = $exploded[1])) {
return false;
}
if ($storeId === $receivedStoreId) {
continue;
}
if (null === $storeId) {
$storeId = $receivedStoreId;
continue;
}
return false;
}
return true;
}
}

View File

@ -1,11 +0,0 @@
<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

View File

@ -0,0 +1,82 @@
<?php
namespace BTCPay\Server;
use BTCPayServer\Crypto\OpenSSLExtension;
use BTCPayServer\Key;
use BTCPayServer\PrivateKey;
use BTCPayServer\PublicKey;
use BTCPayServer\SinKey;
use BTCPayServer\Token;
use BTCPayServer\TokenInterface;
class Encryption
{
/**
* @param object|Key|Token $data
*
* @return array|string
*/
public function encrypt(object $data)
{
if (empty($data)) {
return [
'key' => 'The BTCPay payment plugin was called to encrypt data but no data was passed!',
'domain' => 'Admin.Catalog.Notification',
'parameters' => [],
];
}
if (40 !== \strlen($fingerprint = sha1(sha1(__DIR__)))) {
return [
'key' => 'Invalid server fingerprint generated!',
'domain' => 'Admin.Catalog.Notification',
'parameters' => [],
];
}
$openssl = new OpenSSLExtension();
if (empty($encrypted = $openssl->encrypt(base64_encode(serialize($data)), $fingerprint, '1234567890123456'))) {
return [
'key' => 'The BTCPay payment plugin was called to serialize an encrypted object and failed!',
'domain' => 'Admin.Catalog.Notification',
'parameters' => [],
];
}
return $encrypted;
}
/**
* @return PrivateKey|PublicKey|SinKey|TokenInterface|array
*/
public function decrypt(string $data)
{
if (empty($data)) {
return [
'key' => 'The BTCPay payment plugin was called to decrypt data but no data was passed!',
'domain' => 'Admin.Catalog.Notification',
'parameters' => [],
];
}
if (40 !== \strlen($fingerprint = sha1(sha1(__DIR__)))) {
return [
'key' => 'Invalid server fingerprint generated!',
'domain' => 'Admin.Catalog.Notification',
'parameters' => [],
];
}
$openssl = new OpenSSLExtension();
if (false === ($decrypted = base64_decode($openssl->decrypt($data, $fingerprint, '1234567890123456'), true))) {
return [
'key' => 'The BTCPay payment plugin was called to unserialize a decrypted object and failed! The decrypt function was called with "%s"',
'domain' => 'Admin.Catalog.Notification',
'parameters' => [],
];
}
return unserialize($decrypted, [PrivateKey::class, PublicKey::class, SinKey::class, Token::class]);
}
}

View File

@ -2,232 +2,167 @@
namespace BTCPay\Server;
use BTCPay\Constants;
use BTCPay\Exception\BTCPayException;
use BTCPay\Repository\BitcoinPaymentRepository;
use BTCPayServer\Client\InvoiceCheckoutOptions;
use BTCPayServer\Util\PreciseNumber;
use PrestaShop\PrestaShop\Adapter\Configuration;
if (!\defined('_PS_VERSION_')) {
exit;
}
use BTCPay\LegacyOrderBitcoinRepository;
use BTCPayServer\Buyer;
use BTCPayServer\Currency as BTCPayCurrency;
use BTCPayServer\Invoice;
use BTCPayServer\Item;
class Factory
{
/**
* @var LegacyOrderBitcoinRepository
*/
private $repository;
/**
* @var \Link
*/
private $link;
/**
* @var Configuration
* @var string
*/
private $configuration;
private $moduleName;
/**
* @var \Context
*/
private $context;
/**
* @var \BTCPay
*/
private $module;
public function __construct(\BTCPay $module, \Context $context)
public function __construct(LegacyOrderBitcoinRepository $repository, \Link $link, string $moduleName)
{
$this->link = new \Link();
$this->configuration = new Configuration();
$this->context = $context;
$this->module = $module;
$this->repository = $repository;
$this->link = $link;
$this->moduleName = $moduleName;
}
/**
* @throws \PrestaShopDatabaseException
* @throws \JsonException
*/
public function createPaymentRequest(\Customer $customer, \Cart $cart): ?string
{
// Check if we have a cart ID we can use
if (empty($cart->id)) {
\PrestaShopLogger::addLog('[ERROR] The BTCPay payment plugin was called to process a payment but the cart ID was missing.', \PrestaShopLogger::LOG_SEVERITY_LEVEL_ERROR);
\PrestaShopLogger::addLog(
'[Error] The BTCPay payment plugin was called to process a payment but the cart ID was missing.',
3
);
return null;
}
// Build the client from our stored configuration
$client = Client::createFromConfiguration($this->configuration);
// Ensure the client is ready for use
if (null === $client || false === $client->isValid()) {
\PrestaShopLogger::addLog("[ERROR] The BTCPay payment plugin was called to process a payment but the client doesn't exist.", \PrestaShopLogger::LOG_SEVERITY_LEVEL_ERROR);
return null;
try {
$client = Client::createFromConfiguration();
} catch (\Exception $e) {
\PrestaShopLogger::addLog('[ERROR] ' . $e->getMessage(), $e->getCode());
throw new BTCPayException($e->getMessage(), $e->getCode(), $e);
}
// Make sure we have a webhook, before we redirect anyone anywhere
$client->webhook()->ensureWebhook($this->configuration->get(Constants::CONFIGURATION_BTCPAY_STORE_ID));
// If another BTCPay invoice was created before, returns the original one
if (null !== ($redirect = $client->getBTCPayRedirect($cart))) {
\PrestaShopLogger::addLog('[WARNING] Existing BTCPay invoice has already been created for this cart, redirecting...', \PrestaShopLogger::LOG_SEVERITY_LEVEL_WARNING, null, 'Cart', $cart->id);
\PrestaShopLogger::addLog(
'[WARNING] Existing BTCPay invoice has already been created, redirecting to it...',
2
);
return $redirect;
}
// Setup the Invoice
$invoice = new Invoice();
$invoice->setFullNotifications(true);
$invoice->setExtendedNotifications(true);
// Get and set transaction speed
if (empty($transactionSpeed = \Configuration::get('BTCPAY_TXSPEED'))) {
$transactionSpeed = 'default';
}
$invoice->setTransactionSpeed($transactionSpeed);
// Set an order reference instead of ID, since we haven't made one yet
$invoiceReference = \Tools::passwdGen(20);
$invoice->setOrderId($invoiceReference);
// Get shopping currency, currently tested with be EUR
$currency = \Currency::getCurrencyInstance((int) $cart->id_currency);
$btcpayCurrency = new BTCPayCurrency($currency->iso_code);
$invoice->setCurrency($btcpayCurrency);
// Add a basic item to our invoice
$orderTotal = $cart->getOrderTotal(true);
$invoice->setItem(
(new Item())
->setCode('Cart: ' . $cart->id)
->setDescription('Your purchase')
->setPrice($orderTotal)
);
// Set POS data so we can verify later on that the call is legit
$invoice->setPosData($customer->secure_key);
// Create a buyer we can add to the invoice
$buyer = new Buyer();
// Add customer information to buyer
$customer = new \Customer((int) $cart->id_customer);
$buyer->setEmail($customer->email);
$buyer->setFirstName($customer->firstname);
$buyer->setLastName($customer->lastname);
// Add address information to buyer
$address = new \Address($cart->id_address_delivery);
$buyer->setAddress([$address->address1, $address->address2]);
$buyer->setCountry($address->country);
$buyer->setZip($address->postcode);
$buyer->setCity($address->city);
// Add the state if available
if (0 !== ($stateId = $address->id_state)) {
$buyer->setState((new \State($stateId))->name);
}
// Finally, add the build buyer to the invoice
$invoice->setBuyer($buyer);
// Create the redirect URL once payment has been done.
$invoice->setRedirectUrl($this->link->getModuleLink($this->moduleName, 'validation', ['invoice_reference' => $invoiceReference], true));
// This is the callback url for invoice paid.
$invoice->setNotificationUrl($this->link->getModuleLink($this->moduleName, 'ipn', [], true));
// Ask BTCPay to create an invoice with cart information
try {
// Setup some stuff
$currency = \Currency::getCurrencyInstance($cart->id_currency);
$address = new \Address($cart->id_address_delivery);
$invoiceReference = \Tools::passwdGen(20);
$errorReporting = error_reporting();
error_reporting(\E_ALL & ~\E_NOTICE & ~\E_STRICT & ~\E_DEPRECATED & ~\E_WARNING);
$invoice = $client->createInvoice($invoice);
error_reporting($errorReporting);
// Get totals
$taxAmount = (string) $cart->getOrderTotal(true, \Cart::ONLY_PRODUCTS) - $cart->getOrderTotal(false, \Cart::ONLY_PRODUCTS);
$orderTotal = (string) $cart->getOrderTotal(true);
// Setup custom checkout options, defaults get picked from store config.
$checkoutOptions = new InvoiceCheckoutOptions();
$checkoutOptions
->setSpeedPolicy($this->configuration->get(Constants::CONFIGURATION_SPEED_MODE, InvoiceCheckoutOptions::SPEED_MEDIUM))
->setRedirectURL($this->link->getModuleLink($this->module->name, 'validation', ['invoice_reference' => $invoiceReference], true));
// Get the store ID
$storeID = $this->configuration->get(Constants::CONFIGURATION_BTCPAY_STORE_ID);
// Build the metadata
$metadata = $this->createMetadata($customer, $address, $invoiceReference, $taxAmount);
// Ask BTCPay to create an invoice with cart information
$invoice = $client->invoice()->createInvoice(
$storeID,
$currency->iso_code,
PreciseNumber::parseString($orderTotal),
$invoiceReference,
$customer->email,
$metadata,
$checkoutOptions
);
// Process response
$invoiceResponse = $invoice->getData();
$invoiceId = $invoiceResponse['id'];
$invoiceUrl = $invoiceResponse['checkoutLink'];
\PrestaShopLogger::addLog(\sprintf('[INFO] Invoice %s created, see %s', $invoiceId, $invoiceUrl));
$orderStatus = (string) $this->configuration->get(Constants::CONFIGURATION_ORDER_STATE_WAITING);
\PrestaShopLogger::addLog('Invoice ' . $invoice->getId() . ' created, see ' . $invoice->getUrl(), 2);
// Register invoice into bitcoin_payment table, if we didn't have one before.
if (null === ($bitcoinPayment = BitcoinPaymentRepository::getOneByCartID($cart->id))) {
$bitcoinPayment = BitcoinPaymentRepository::create($cart->id, $orderStatus, $invoiceId);
if (null === ($orderBitcoin = $this->repository->getOneByCartID($cart->id))) {
$orderBitcoin = $this->repository->create($cart->id, (string) \Configuration::get('BTCPAY_OS_WAITING'), $invoice->getId());
}
$bitcoinPayment->setInvoiceId($invoiceId);
$bitcoinPayment->setInvoiceReference($invoiceReference);
$bitcoinPayment->setAmount($orderTotal);
$bitcoinPayment->setRedirect($invoiceUrl);
$orderBitcoin->setInvoiceId($invoice->getId());
$orderBitcoin->setInvoiceReference($invoiceReference);
$orderBitcoin->setAmount((string) $orderTotal);
$orderBitcoin->setRedirect($invoice->getUrl());
if (false === $bitcoinPayment->save(true)) {
$error = \sprintf('[ERROR] Could not store bitcoin_payment: %s', \Db::getInstance()->getMsgError());
\PrestaShopLogger::addLog($error, \PrestaShopLogger::LOG_SEVERITY_LEVEL_ERROR, null, 'BitcoinPayment', $bitcoinPayment->getId());
$response = \json_decode($client->getResponse()->getBody(), false, 512, \JSON_THROW_ON_ERROR);
$orderBitcoin->setRate($response->data->rate);
$orderBitcoin->setBitcoinPrice($response->data->btcPrice);
$orderBitcoin->setBitcoinPaid($response->data->btcPaid);
$orderBitcoin->setBitcoinAddress($response->data->bitcoinAddress);
throw new \RuntimeException($error);
if (false === $orderBitcoin->save(true)) {
\PrestaShopLogger::addLog('[ERROR] Could not store bitcoin_payment', 3);
throw new \RuntimeException('[ERROR] Could not store bitcoin_payment');
}
// If we create the order after payment, do not make the order yet
if (Constants::ORDER_MODE_AFTER === $this->configuration->get(Constants::CONFIGURATION_ORDER_MODE)) {
// Update the object, so we can always validate it afterward
if (false === $bitcoinPayment->update(true)) {
$error = \sprintf('[ERROR] Could not update bitcoin_payment: %s', \Db::getInstance()->getMsgError());
\PrestaShopLogger::addLog($error, \PrestaShopLogger::LOG_SEVERITY_LEVEL_ERROR, null, 'BitcoinPayment', $bitcoinPayment->getId());
\PrestaShopLogger::addLog('Invoice ' . $invoice->getId() . ' updated', 2);
throw new \RuntimeException($error);
}
\PrestaShopLogger::addLog(\sprintf('[INFO] Invoice %s has been updated', $invoiceId), \PrestaShopLogger::LOG_SEVERITY_LEVEL_INFORMATIVE, null, 'BitcoinPayment', $bitcoinPayment->getId());
// Redirect user to payment
return $bitcoinPayment->getRedirect();
}
\PrestaShopLogger::addLog(\sprintf('[INFO] Invoice %s has been updated, creating actual order', $invoiceId), \PrestaShopLogger::LOG_SEVERITY_LEVEL_INFORMATIVE, null, 'BitcoinPayment', $bitcoinPayment->getId());
$this->module->validateOrder(
$bitcoinPayment->getCartId(),
$bitcoinPayment->getStatus(),
$bitcoinPayment->getAmount(),
$this->module->displayName,
null,
[],
null,
false,
$customer->secure_key,
$this->context->shop
);
// Get the new order ID
$orderId = (int) \Order::getIdByCartId($bitcoinPayment->getCartId());
$bitcoinPayment->setOrderId($orderId);
$bitcoinPayment->setStatus($orderStatus);
// Update the object
if (false === $bitcoinPayment->update(true)) {
$error = \sprintf('[ERROR] Could not update bitcoin_payment: %s', \Db::getInstance()->getMsgError());
\PrestaShopLogger::addLog($error, \PrestaShopLogger::LOG_SEVERITY_LEVEL_ERROR, null, 'BitcoinPayment', $bitcoinPayment->getId());
throw new \RuntimeException($error);
}
// Redirect user to payment
return $bitcoinPayment->getRedirect();
} catch (\Throwable $throwable) {
\PrestaShopLogger::addLog(\sprintf('[ERROR] %s', $throwable->getMessage()), \PrestaShopLogger::LOG_SEVERITY_LEVEL_ERROR, $throwable->getCode());
throw new BTCPayException($throwable->getMessage(), $throwable->getCode(), $throwable);
return $orderBitcoin->getRedirect();
} catch (\Exception $e) {
\PrestaShopLogger::addLog('[ERROR] ' . $e->getMessage(), 3);
throw new BTCPayException($e->getMessage(), $e->getCode(), $e);
}
}
/**
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
*/
private function createMetadata(\Customer $customer, \Address $address, string $invoiceReference, string $taxAmount): array
{
$metadata = [
'posData' => $customer->secure_key,
'itemCode' => \sprintf('invoice-reference-%s', $invoiceReference),
'itemDesc' => \sprintf('Purchase from %s', $this->context->shop->name),
'taxIncluded' => $taxAmount,
];
// Only include personal details if enabled, if not, return here (force to bool, as the default could be not bool)
if (false === (bool) $this->configuration->get(Constants::CONFIGURATION_SHARE_METADATA, false)) {
return $metadata;
}
$metadata = \array_merge($metadata, [
'buyerName' => \sprintf('%s %s', $customer->firstname, $customer->lastname),
'buyerAddress1' => $address->address1,
'buyerAddress2' => $address->address2,
'buyerCity' => $address->city,
'buyerZip' => $address->postcode,
'buyerCountry' => $address->country,
]);
// Set state if available
if (0 !== ($stateId = $address->id_state)) {
$metadata['buyerState'] = (new \State($stateId))->name;
}
// Set phone/mobile phone number if available
if (!empty($address->phone)) {
$metadata['buyerPhone'] = $address->phone;
} elseif (!empty($address->phone_mobile)) {
$metadata['buyerPhone'] = $address->phone_mobile;
}
return $metadata;
}
}

View File

@ -0,0 +1,386 @@
<?php
namespace BTCPay\Server;
use BTCPay\Constants;
use BTCPay\LegacyOrderBitcoinRepository;
use Symfony\Component\HttpFoundation\Request;
class IPN
{
/**
* @var LegacyOrderBitcoinRepository
*/
private $repository;
public function __construct(LegacyOrderBitcoinRepository $repository)
{
$this->repository = $repository;
}
public function process(\BTCPay $btcpay, Request $request): void
{
$json = json_decode($request->getContent(), true);
if (false === $json || null === $json) {
return;
}
// Check if we received an event
if (!\array_key_exists('event', $json)) {
return;
}
// Get event from JSON
$event = $json['event'];
// Check if event code exist and is not empty
if (!\array_key_exists('code', $event) || empty($event['code'])) {
return;
}
// Check if event name exist and is not empty
if (!\array_key_exists('name', $event) || empty($event['name'])) {
return;
}
// Check if event data exist and is not empty
if (!\array_key_exists('data', $json) || empty($json['data'])) {
return;
}
// Get data
$data = $json['data'];
// Check if ID exists and that it's not empty.
if (!\array_key_exists('id', $data) || empty($data['id'])) {
return;
}
// Get order mode
$orderMode = \Configuration::get('BTCPAY_ORDERMODE');
// Invoice created - Before
if ('invoice_created' === $event['name'] && Constants::ORDER_MODE_BEFORE === $orderMode) {
$this->invoiceCreated($data, $btcpay);
return;
}
// Invoice created - After
if ('invoice_created' === $event['name'] && Constants::ORDER_MODE_AFTER === $orderMode) {
\PrestaShopLogger::addLog('[INFO] Received invoice_created event, but not creating order because order mode is ' . Constants::ORDER_MODE_AFTER, 1);
return;
}
// Payment Received - Before
if ('invoice_receivedPayment' === $event['name'] && Constants::ORDER_MODE_BEFORE === $orderMode) {
$this->receivedPaymentBefore($data);
return;
}
// Payment Received - After
if ('invoice_receivedPayment' === $event['name'] && Constants::ORDER_MODE_AFTER === $orderMode) {
$this->receivedPaymentAfter($data, $btcpay);
return;
}
// Pending full payment, not much to do but wait
if ('invoice_paidInFull' === $event['name']) {
return;
}
// Payment failed
if ('invoice_failedToConfirm' === $event['name'] || 'invoice_markedInvalid' === $event['name'] || 'invoice_expired' === $event['name']) {
$this->failedPayment($data);
return;
}
// Payment confirmed
if ('invoice_confirmed' === $event['name'] || 'invoice_markedComplete' === $event['name']) {
$this->paymentConfirmed($data);
return;
}
// Payment completed
if ('invoice_completed' === $event['name']) {
\PrestaShopLogger::addLog('[INFO] BTCPay server has told us that invoice "' . $data['id'] . '" is finished', 1);
return;
}
// Log other IPN's.
\PrestaShopLogger::addLog('[Error] Could not process IPN', 3);
\PrestaShopLogger::addLog('[INFO] Received IPN: ' . $request->getContent(), 1);
}
private function invoiceCreated(array $data, \BTCPay $btcpay): void
{
$invoiceId = (string) $data['id'];
\PrestaShopLogger::addLog('[Info] invoice created for ' . $invoiceId, 1);
if (null === ($orderBitcoin = $this->repository->getOneByInvoiceID($invoiceId))) {
\PrestaShopLogger::addLog('[Error] Could not load order', 3);
throw new \RuntimeException('[Error] Could not load order');
}
// waiting payment
$orderStatus = \Configuration::get('BTCPAY_OS_WAITING');
// on Order, just say payment processor is BTCPay
$displayName = $btcpay->displayName;
// fetch secure key, used to check cart comes from your prestashop
$secure_key = $data['posData'];
if (false === isset($secure_key)) {
\PrestaShopLogger::addLog('[Error] No securekey', 3);
throw new \RuntimeException('[Error] No securekey');
}
// rate in fiat currency
$rate = $data['rate'];
if (false === isset($rate)) {
\PrestaShopLogger::addLog('[Error] No rate', 3);
throw new \RuntimeException('[Error] No rate');
}
// generate an order only if their is not another one with this cart
$orderId = \Order::getIdByCartId($orderBitcoin->getCartId());
if (false === $orderId || 0 === $orderId) {
$btcpay->validateOrder(
$orderBitcoin->getCartId(),
$orderStatus,
$orderBitcoin->getAmount(),
$displayName, //bitcoin btcpay
null, //message should be new Message
[], //extravars for mail
null, //currency special
false, // don't touch amount
$secure_key
);
// Get the new order ID
$orderId = (int) \Order::getIdByCartId($orderBitcoin->getCartId());
$orderBitcoin->setOrderId($orderId);
$orderBitcoin->setBitcoinPaid('0.0');
$orderBitcoin->setStatus((string) $orderStatus);
// Update the object
if (false === $orderBitcoin->update(true)) {
$error = '[Error] Could not update bitcoin_payment: ' . \Db::getInstance()->getMsgError();
\PrestaShopLogger::addLog($error, 3);
throw new \RuntimeException($error);
}
return;
}
// Order already paid
\PrestaShopLogger::addLog('[Error] already created order', 1);
throw new \RuntimeException('[Error] already created order');
}
private function receivedPaymentBefore(array $data): void
{
$invoiceId = (string) $data['id'];
\PrestaShopLogger::addLog('[Info] payment received for ' . $invoiceId, 1);
if (null === ($orderBitcoin = $this->repository->getOneByInvoiceID($invoiceId))) {
\PrestaShopLogger::addLog('[Error] Could not load order', 3);
throw new \RuntimeException('[Error] Could not load order');
}
$orderId = $orderBitcoin->getOrderId();
// waiting confirmation
$orderStatus = \Configuration::get('BTCPAY_OS_CONFIRMING');
$orderBitcoin->setBitcoinPaid($data['btcPaid']);
$orderBitcoin->setStatus((string) $orderStatus);
// Update the object
if (false === $orderBitcoin->update(true)) {
$error = '[Error] Could not update bitcoin_payment: ' . \Db::getInstance()->getMsgError();
\PrestaShopLogger::addLog($error, 3);
throw new \RuntimeException($error);
}
// add Order status change to Order history table
$orderHistory = new \OrderHistory();
$orderHistory->id_order = $orderId;
// bitcoin confirmation ok
$orderHistory->changeIdOrderState($orderStatus, $orderId, true);
//add with email is mandatory to add new order state in order_history
$orderHistory->add(true);
}
private function receivedPaymentAfter(array $data, \BTCPay $btcpay): void
{
$invoiceId = (string) $data['id'];
\PrestaShopLogger::addLog('[Info] payment received for ' . $invoiceId, 1);
if (null === ($orderBitcoin = $this->repository->getOneByInvoiceID($invoiceId))) {
\PrestaShopLogger::addLog('[Error] Could not load order', 3);
throw new \RuntimeException('[Error] Could not load order');
}
// waiting confirmation
$orderStatus = \Configuration::get('BTCPAY_OS_CONFIRMING');
// on Order, just say payment processor is BTCPay
$displayName = $btcpay->displayName;
// fetch secure key, used to check cart comes from your prestashop
$secure_key = $data['posData'];
if (false === isset($secure_key)) {
\PrestaShopLogger::addLog('[Error] No securekey', 3);
throw new \RuntimeException('[Error] No securekey');
}
// rate in fiat currency
$rate = $data['rate'];
if (false === isset($rate)) {
\PrestaShopLogger::addLog('[Error] No rate', 3);
throw new \RuntimeException('[Error] No rate');
}
// generate an order only if their is not another one with this cart
$orderId = \Order::getIdByCartId($orderBitcoin->getCartId());
if (false === $orderId || 0 === $orderId) {
$btcpay->validateOrder(
$orderBitcoin->getCartId(),
$orderStatus,
$orderBitcoin->getAmount(),
$displayName, //bitcoin btcpay
$rate, //message
[], //extravars
null, //currency special
false, // don't touch amount
$secure_key
);
// Get the new order ID
$orderId = (int) \Order::getIdByCartId($orderBitcoin->getCartId());
$orderBitcoin->setOrderId($orderId);
$orderBitcoin->setBitcoinPaid($data['btcPaid']);
$orderBitcoin->setStatus((string) $orderStatus);
// Update the object
if (false === $orderBitcoin->update(true)) {
$error = '[Error] Could not update bitcoin_payment: ' . \Db::getInstance()->getMsgError();
\PrestaShopLogger::addLog($error, 3);
throw new \RuntimeException($error);
}
return;
}
// Order already paid
\PrestaShopLogger::addLog('[Error] already paid order', 1);
throw new \RuntimeException('[Error] already paid order');
}
private function failedPayment(array $data): void
{
$invoiceId = (string) $data['id'];
\PrestaShopLogger::addLog('[Info] payment failed for ' . $invoiceId, 1);
if (null === ($orderBitcoin = $this->repository->getOneByInvoiceID($invoiceId))) {
\PrestaShopLogger::addLog('[Error] Could not load order', 3);
throw new \RuntimeException('[Error] Could not load order');
}
// wait for confirm
$orderStatus = \Configuration::get('BTCPAY_OS_CONFIRMING');
if ('invalid' === $data['status'] || 'expired' === $data['status']) {
// time setup on invoice is expired
$orderStatus = \Configuration::get('BTCPAY_OS_FAILED');
}
$orderBitcoin->setStatus((string) $orderStatus);
// Update the object
if (false === $orderBitcoin->update(true)) {
$error = '[Error] Could not update bitcoin_payment: ' . \Db::getInstance()->getMsgError();
\PrestaShopLogger::addLog($error, 3);
throw new \RuntimeException($error);
}
// add Order status change to Order history table
$orderHistory = new \OrderHistory();
$orderHistory->id_order = $orderBitcoin->getOrderId();
// bitcoin confirmation ok
$orderHistory->changeIdOrderState($orderStatus, $orderBitcoin->getOrderId(), true);
$orderHistory->add(true);
}
private function paymentConfirmed(array $data): void
{
$invoiceId = (string) $data['id'];
\PrestaShopLogger::addLog('[Info] payment confirmed for ' . $invoiceId, 1);
if (null === ($orderBitcoin = $this->repository->getOneByInvoiceID($invoiceId))) {
\PrestaShopLogger::addLog('[Error] Could not load order', 3);
throw new \RuntimeException('[Error] Could not load order');
}
$order = new \Order($orderBitcoin->getOrderId());
// wait for confirm
$orderStatus = \Configuration::get('BTCPAY_OS_CONFIRMING');
if ('invalid' === $data['status'] || 'expired' === $data['status']) {
// time setup on invoice is expired
$orderStatus = \Configuration::get('BTCPAY_OS_FAILED');
}
if ('paid' === $data['status']) {
// TX received but we have to wait some confirmation
$orderStatus = \Configuration::get('BTCPAY_OS_CONFIRMING');
}
if ('confirmed' === $data['status'] || 'complete' === $data['status']) {
//Transaction confirmed
$orderStatus = \Configuration::get('BTCPAY_OS_PAID');
}
$orderBitcoin->setStatus((string) $orderStatus);
// Update the object
if (false === $orderBitcoin->update(true)) {
$error = '[Error] Could not update bitcoin_payment: ' . \Db::getInstance()->getMsgError();
\PrestaShopLogger::addLog($error, 3);
throw new \RuntimeException($error);
}
// add Order status change to Order history table
if ($order->current_state !== $orderStatus) {
$orderHistory = new \OrderHistory();
$orderHistory->id_order = $orderBitcoin->getOrderId();
// bitcoin confirmation ok
$orderHistory->changeIdOrderState($orderStatus, $orderBitcoin->getOrderId(), true);
$orderHistory->add(true);
} else {
\PrestaShopLogger::addLog('[Info] current state is not different than new order status in invoice confirmed', 1);
}
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace BTCPay\Server;
use BTCPayServer\Token as BTCPayToken;
class Token extends BTCPayToken
{
public static function createToken(string $token): self
{
return (new self())
->setToken($token)
->setFacade('merchant');
}
}

View File

@ -1,118 +0,0 @@
<?php
namespace BTCPay\Server;
use BTCPay\Constants;
use BTCPay\Exception\BTCPayException;
use BTCPayServer\Http\ClientInterface;
use PrestaShop\PrestaShop\Adapter\Configuration;
use Symfony\Component\HttpFoundation\Response;
if (!\defined('_PS_VERSION_')) {
exit;
}
class Webhook extends \BTCPayServer\Client\Webhook
{
/**
* @var Configuration
*/
private $configuration;
/**
* @var \Link
*/
private $link;
public function __construct(string $baseUrl, string $apiKey, ClientInterface $client = null)
{
parent::__construct($baseUrl, $apiKey, $client);
$this->configuration = new Configuration();
$this->link = new \Link();
}
/**
* @throws \JsonException
* @throws \Exception
*/
public function ensureWebhook(string $storeId): void
{
// Check if we have an existing webhook, if so, just cancel now (empty check is required).
if (false === empty($this->getCurrent($storeId, $this->configuration->get(Constants::CONFIGURATION_BTCPAY_WEBHOOK_ID)))) {
return;
}
// Generate new webhook secret
$secret = \bin2hex(\random_bytes(24));
// Build the webhook URL
$webhookURL = $this->link->getModuleLink('btcpay', 'webhook', [], true);
// Create the brand-new webhook
$webhook = $this->createWebhook($storeId, $webhookURL, null, $secret);
// Ensure we actually made a proper webhook
if (empty($webhook->getId()) || empty($webhook->getSecret())) {
throw new BTCPayException("Webhook wasn't created correctly.", Response::HTTP_INTERNAL_SERVER_ERROR);
}
// Ensure the webhook was created with the secret we provided
if ($webhook->getSecret() !== $secret) {
throw new BTCPayException("Webhook secret doesn't match our secret.", Response::HTTP_INTERNAL_SERVER_ERROR);
}
// Store the webhook secret we made, so we can check that the webhook is actually made by us
$this->configuration->set(Constants::CONFIGURATION_BTCPAY_WEBHOOK_SECRET, $secret);
// Store the ID, so we can check if we already have a valid webhook
$this->configuration->set(Constants::CONFIGURATION_BTCPAY_WEBHOOK_ID, $webhook->getId());
}
public function getCurrent(string $storeId, ?string $webhookId): ?\BTCPayServer\Result\Webhook
{
try {
// We need to check for empty here as twig passes a null variable as "" instead of null in configure.html.twig.
if (empty($webhookId)) {
return null;
}
if (null === ($webhook = $this->getWebhook($storeId, $webhookId))) {
return null;
}
return !empty($webhook->getData()) ? $webhook : null;
} catch (\Throwable $throwable) {
$warning = \sprintf("[WARNING] expected webhook '%s' for store '%s' to exist, but it didn't. Exception received: %s", $webhookId, $storeId, $throwable->getMessage());
\PrestaShopLogger::addLog($warning, \PrestaShopLogger::LOG_SEVERITY_LEVEL_WARNING, $throwable->getCode());
return null;
}
}
public function removeCurrent(): bool
{
if (empty($storeId = $this->configuration->get(Constants::CONFIGURATION_BTCPAY_STORE_ID))) {
return false;
}
if (empty($webhookId = $this->configuration->get(Constants::CONFIGURATION_BTCPAY_WEBHOOK_ID))) {
return false;
}
try {
if (null === $this->getWebhook($storeId, $webhookId)) {
return false;
}
$this->deleteWebhook($storeId, $webhookId);
return true;
} catch (\Throwable $throwable) {
$message = \sprintf("[WARNING] Could not remove webhook '%s' from the store '%s'. Please double check it is actually gone. Exception received: %s", $webhookId, $storeId, $throwable->getMessage());
\PrestaShopLogger::addLog($message, \PrestaShopLogger::LOG_SEVERITY_LEVEL_WARNING, $throwable->getCode());
return false;
}
}
}

View File

@ -1,259 +0,0 @@
<?php
namespace BTCPay\Server;
use BTCPay\Constants;
use BTCPay\Factory\CustomerMessage;
use BTCPay\Invoice\Processor;
use BTCPay\Repository\BitcoinPaymentRepository;
use PrestaShop\PrestaShop\Adapter\Configuration;
use Symfony\Component\HttpFoundation\Request;
if (!\defined('_PS_VERSION_')) {
exit;
}
class WebhookHandler
{
/**
* @var \Context
*/
private $context;
/**
* @var Configuration
*/
private $configuration;
/**
* @var Processor
*/
private $processor;
public function __construct(\BTCPay $module, \Context $context, Client $client)
{
$this->context = $context;
$this->configuration = new Configuration();
$this->processor = new Processor($module, $context, $this->configuration, $client);
}
/**
* @throws \PrestaShopDatabaseException
* @throws \JsonException
* @throws \PrestaShopException
*/
public function process(Request $request): void
{
$data = \json_decode($request->getContent(), true, 512, \JSON_THROW_ON_ERROR);
if (false === $data || null === $data) {
return;
}
// Check if we received an event
if (!\array_key_exists('type', $data) || !\array_key_exists('invoiceId', $data)) {
return;
}
// If it's a test, just accept it
if (\str_contains($data['invoiceId'], '__test__')) {
\PrestaShopLogger::addLog(\sprintf('[INFO] Received test IPN: %s', \json_encode($data, \JSON_THROW_ON_ERROR)));
return;
}
// Get the data type
$eventType = (string) $data['type'];
// Get order mode
$orderMode = $this->configuration->get(Constants::CONFIGURATION_ORDER_MODE);
// Payment has been received (order already exists)
if (Constants::ORDER_MODE_BEFORE === $orderMode && \in_array($eventType, ['InvoiceProcessing', 'InvoicePaidInFull', 'InvoiceReceivedPayment'], true)) {
$this->paymentReceived($data);
return;
}
// Payment has been received (order needs to be made)
if (Constants::ORDER_MODE_AFTER === $orderMode && \in_array($eventType, ['InvoiceProcessing', 'InvoicePaidInFull', 'InvoiceReceivedPayment'], true)) {
$this->paymentReceivedDelayedOrder($data);
return;
}
// Payment has been confirmed
if ('InvoicePaymentSettled' === $eventType) {
$this->paymentSettled($data);
return;
}
// Invoice has failed or expired
if (\in_array($eventType, ['InvoiceInvalid', 'InvoiceExpired'], true)) {
$this->invoiceFailed($data);
return;
}
// Invoice has been confirmed
if ('InvoiceSettled' === $eventType) {
$this->invoiceSettled($data);
return;
}
// Invoice was crated, but either order already exists or will be created on first payment, skip
if ('InvoiceCreated' === $eventType) {
return;
}
// Log other IPN's.
\PrestaShopLogger::addLog('[INFO] Received IPN that we did not process IPN');
\PrestaShopLogger::addLog(\sprintf('[INFO] Received IPN: %s', \json_encode($data, \JSON_THROW_ON_ERROR)));
}
/**
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
* @throws \JsonException
*/
private function paymentReceived(array $data): void
{
$invoiceId = (string) $data['invoiceId'];
\PrestaShopLogger::addLog(\sprintf('[INFO] Payment received for invoice %s', $invoiceId));
if (null === ($bitcoinPayment = BitcoinPaymentRepository::getOneByInvoiceID($invoiceId))) {
$error = \sprintf('[WARNING] Could not load order with invoice ID %s', $invoiceId);
\PrestaShopLogger::addLog(\sprintf('[INFO] Received IPN: %s', \json_encode($data, \JSON_THROW_ON_ERROR)));
\PrestaShopLogger::addLog($error, \PrestaShopLogger::LOG_SEVERITY_LEVEL_WARNING);
// Don't bother retrying
return;
}
$this->processor->paymentReceived($bitcoinPayment);
}
/**
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
* @throws \JsonException
*/
private function paymentReceivedDelayedOrder(array $data): void
{
$invoiceId = (string) $data['invoiceId'];
\PrestaShopLogger::addLog(\sprintf('[INFO] Payment received for invoice %s', $invoiceId));
if (null === ($bitcoinPayment = BitcoinPaymentRepository::getOneByInvoiceID($invoiceId))) {
$error = \sprintf('[WARNING] Could not load order with invoice ID %s', $invoiceId);
\PrestaShopLogger::addLog(\sprintf('[INFO] Received IPN: %s', \json_encode($data, \JSON_THROW_ON_ERROR)));
\PrestaShopLogger::addLog($error, \PrestaShopLogger::LOG_SEVERITY_LEVEL_WARNING);
// Don't bother retrying
return;
}
// Deal with the actual invoice now
$this->processor->paymentReceivedCreateAfter($bitcoinPayment);
}
/**
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
* @throws \JsonException
*/
private function paymentSettled(array $data): void
{
$invoiceId = (string) $data['invoiceId'];
\PrestaShopLogger::addLog(\sprintf('[INFO] One of the payments has settled for invoice %s', $invoiceId));
if (null === ($bitcoinPayment = BitcoinPaymentRepository::getOneByInvoiceID($invoiceId))) {
$error = \sprintf('[WARNING] Could not load order with invoice ID %s', $invoiceId);
\PrestaShopLogger::addLog(\sprintf('[INFO] Received IPN: %s', \json_encode($data, \JSON_THROW_ON_ERROR)));
\PrestaShopLogger::addLog($error, \PrestaShopLogger::LOG_SEVERITY_LEVEL_WARNING);
// Don't bother retrying
return;
}
// Deal with the actual invoice now
$this->processor->paymentSettled($bitcoinPayment);
}
/**
* @throws \JsonException
* @throws \PrestaShopDatabaseException
* @throws \PrestaShopException
*/
private function invoiceFailed(array $data): void
{
$invoiceId = (string) $data['invoiceId'];
\PrestaShopLogger::addLog(\sprintf("[INFO] Invoice '%s' failed, either because it wasn't paid, it expired or it was marked as invalid", $invoiceId));
if (null === ($bitcoinPayment = BitcoinPaymentRepository::getOneByInvoiceID($invoiceId))) {
$error = \sprintf('[WARNING] Could not load order with invoice ID %s', $invoiceId);
\PrestaShopLogger::addLog(\sprintf('[INFO] Received IPN: %s', \json_encode($data, \JSON_THROW_ON_ERROR)));
\PrestaShopLogger::addLog($error, \PrestaShopLogger::LOG_SEVERITY_LEVEL_WARNING);
// Don't bother retrying
return;
}
// If there is no order, don't bother updating it
if (false === $bitcoinPayment->hasOrder()) {
return;
}
// Check if protection is disabled, if so, just process the failure
if (false === $this->configuration->get(Constants::CONFIGURATION_PROTECT_ORDERS, true)) {
$this->processor->invoiceFailed($bitcoinPayment);
}
// Otherwise, will need to check the order so fetch it
$order = new \Order($bitcoinPayment->getOrderId());
// Check if the order has been paid, if so, add a note and abort
if (\Validate::isLoadedObject($orderState = $order->getCurrentOrderState()) && $orderState->paid) {
// Ensure we log this IPN
\PrestaShopLogger::addLog(\sprintf('[INFO] Received IPN: %s', \json_encode($data, \JSON_THROW_ON_ERROR)));
\PrestaShopLogger::addLog(\sprintf("[WARN] Webhook ('%s') received from BTCPay Server, but the order was already marked as paid.", $data['type']), \PrestaShopLogger::LOG_SEVERITY_LEVEL_WARNING, null, 'Order', $order->id);
// Build a simple note and add it to the order
$note = \sprintf("BTCPay Server: Webhook ('%s') received, but the order was already marked as paid.", $data['type']);
CustomerMessage::addToOrder($this->context->shop, $order, $note);
// Don't bother with the rest
return;
}
// The order has not been set to paid, process the failure
$this->processor->invoiceFailed($bitcoinPayment);
}
/**
* @throws \JsonException
* @throws \PrestaShopException
* @throws \PrestaShopDatabaseException
*/
private function invoiceSettled(array $data): void
{
$invoiceId = (string) $data['invoiceId'];
\PrestaShopLogger::addLog(\sprintf("[INFO] Invoice '%s' has been settled and thus fully paid", $invoiceId));
if (null === ($bitcoinPayment = BitcoinPaymentRepository::getOneByInvoiceID($invoiceId))) {
$error = \sprintf('[WARNING] Could not load order with invoice ID %s', $invoiceId);
\PrestaShopLogger::addLog(\sprintf('[INFO] Received IPN: %s', \json_encode($data, \JSON_THROW_ON_ERROR)));
\PrestaShopLogger::addLog($error, \PrestaShopLogger::LOG_SEVERITY_LEVEL_WARNING);
// Don't bother retrying
return;
}
// If there is no order, don't bother updating it
if (false === $bitcoinPayment->hasOrder()) {
return;
}
$this->processor->invoiceSettled($bitcoinPayment);
}
}

View File

@ -1,11 +0,0 @@
<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

View File

@ -1,11 +0,0 @@
<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

View File

@ -1,11 +0,0 @@
<?php
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Location: ../');
exit;

View File

@ -1,20 +1,10 @@
<?php
use PrestaShop\PrestaShop\Core\Module\ModuleInterface;
if (!defined('_PS_VERSION_')) {
exit;
}
/**
* @param BTCpay|ModuleInterface|mixed $module
*/
function upgrade_module_2_0_0(mixed $module): bool
function upgrade_module_2_0_0(): bool
{
if (!$module instanceof BTCPay) {
throw new LogicException('Received invalid module');
}
// Add invoice reference
return Db::getInstance()->Execute('ALTER TABLE `' . _DB_PREFIX_ . 'order_bitcoin` ADD invoice_reference varchar(255) AFTER invoice_id');
}

View File

@ -1,6 +1,5 @@
<?php
use BTCPay\Constants;
use PrestaShop\PrestaShop\Core\Module\ModuleInterface;
if (!defined('_PS_VERSION_')) {
@ -8,11 +7,9 @@ if (!defined('_PS_VERSION_')) {
}
/**
* @param BTCpay|ModuleInterface|mixed $module
*
* @throws JsonException
* @param BTCpay $module
*/
function upgrade_module_3_0_0(mixed $module): bool
function upgrade_module_3_0_0(ModuleInterface $module): bool
{
if (!$module instanceof BTCPay) {
throw new LogicException('Received invalid module');
@ -34,7 +31,7 @@ function updateDatabase(): bool
/** @var PDO $connection */
$connection = Db::getInstance()->connect();
// Start a transaction, so we don't mess up the database if something goes wrong
// Start a transaction so we don't fuck up the database if shit goes wrong
$connection->beginTransaction();
$queries = [
@ -52,7 +49,7 @@ function updateDatabase(): bool
];
foreach ($queries as $query) {
// Execute the query
// Execute query
if (false === $connection->query($query)) {
// Cancel the transaction
if (false === $connection->rollBack()) {
@ -102,10 +99,10 @@ function updateConfig(): bool
// Add old order states to the configuration
$order_states = [
Constants::CONFIGURATION_ORDER_STATE_WAITING => 39,
Constants::CONFIGURATION_ORDER_STATE_CONFIRMING => 40,
Constants::CONFIGURATION_ORDER_STATE_FAILED => 41,
Constants::CONFIGURATION_ORDER_STATE_PAID => 42,
'BTCPAY_OS_WAITING' => 39,
'BTCPAY_OS_CONFIRMING' => 40,
'BTCPAY_OS_FAILED' => 41,
'BTCPAY_OS_PAID' => 42,
];
foreach ($order_states as $order_state => $id) {

Some files were not shown because too many files have changed in this diff Show More