Skip to content

Commit

Permalink
Merge pull request #2 from wayforpay/service-url
Browse files Browse the repository at this point in the history
Service URL handler
Return URL handler
  • Loading branch information
vladdnepr authored Sep 30, 2019
2 parents b70aa46 + a235b7b commit 9c4ce83
Show file tree
Hide file tree
Showing 16 changed files with 510 additions and 23 deletions.
56 changes: 39 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ PHP SDK for payment system [WayForPay](https://wayforpay.com).
- [Complete 3DS](#complete-3ds)
- [Check](#check)
- [Refund](#refund)
- [Service URL](#service-url)
- [Return URL](#return-url)
- [TODO](#todo)
- [Contributing](#contributing)

Expand Down Expand Up @@ -63,34 +65,26 @@ All examples require `composer install` before using after cloning from GitHub.

See [purchase.php](examples/purchase.php).

```bash
$ php examples/purchase.php > pay.html
```

After you can see at `pay.html` form with pay button. Open file in your browser and press `Pay`.

You can open file via default browser in Linux-based OS like:
Run PHP built-in server

```bash
$ x-www-browser pay.html
$ php -S localhost:8000
```

Then open `http://localhost:8000/examples/purchase.php` in browser.

#### Purchase Widget

See [purchase-widget.php](examples/purchase-widget.php).

```bash
$ php examples/purchase-widget.php > widget.html
```

After you can see at `widget.html` widget with pay button. Open file in your browser and press `Pay`.

You can open file via default browser in Linux-based OS like:
Run PHP built-in server

```bash
$ x-www-browser widget.html
$ php -S localhost:8000
```

Then open `http://localhost:8000/examples/purchase-widget.php` in browser.

#### Transactions List

See [transaction-list.php](examples/transaction-list.php).
Expand Down Expand Up @@ -170,7 +164,7 @@ Order status: Refunded
Response will be instance of `CheckResponse`. Order can be retrieved via
`getOrder` method.

### Refund
#### Refund

```bash
$ php examples/refund.php
Expand All @@ -180,6 +174,34 @@ Order status: Refunded

Response will be instance of `RufundResponse`.

### Service URL

You can set service URL in wizard via

```php
$wizard->setServiceUrl('http://localhost:8000/examples/serviceUrl.php')
```

After payment processing WayForPay send payment data to specified URL. You can parse and check data like in example.

See [serviceUrl.php](examples/serviceUrl.php).

#### ⚠️⚠️⚠️ WARNING ⚠️⚠️⚠️

*Service URL must be accessible via Internet. WayForPay can't send data to local machine!*

### Return URL

You can set service URL in wizard via

```php
$wizard->setReturnUrl('http://localhost:8000/examples/returnUrl.php')
```

After payment processing WayForPay send payment data to specified URL. You can parse and check data like in example.

See [returnUrl.php](examples/returnUrl.php).

## TODO

* Methods
Expand Down
2 changes: 2 additions & 0 deletions examples/purchase-widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
->setProducts(new ProductCollection(array(
new Product('test', 0.01, 1)
)))
->setReturnUrl('http://localhost:8000/examples/returnUrl.php')
->setServiceUrl('http://localhost:8000/examples/serviceUrl.php')
->getForm()
->getWidget();

Expand Down
2 changes: 2 additions & 0 deletions examples/purchase.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@
->setProducts(new ProductCollection(array(
new Product('test', 0.01, 1)
)))
->setReturnUrl('http://localhost:8000/examples/returnUrl.php')
->setServiceUrl('http://localhost:8000/examples/serviceUrl.php')
->getForm()
->getAsString();

Expand Down
36 changes: 36 additions & 0 deletions examples/returnUrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
/*
* This file is part of the WayForPay project.
*
* @link https://github.com/wayforpay/php-sdk
*
* @author Vladislav Lyshenko <vladdnepr1989@gmail.com>
* @copyright Copyright 2019 WayForPay
* @license https://opensource.org/licenses/MIT
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

require_once __DIR__ . '/../vendor/autoload.php';

use WayForPay\SDK\Credential\AccountSecretTestCredential;
use WayForPay\SDK\Exception\WayForPaySDKException;
use WayForPay\SDK\Handler\ServiceUrlHandler;

// Use test credential or yours
$credential = new AccountSecretTestCredential();
//$credential = new AccountSecretCredential('account', 'secret');

try {
$handler = new ServiceUrlHandler($credential);
$response = $handler->parseRequestFromGlobals();

if ($response->getReason()->isOK()) {
echo "Success";
} else {
echo "Error: " . $response->getReason()->getMessage();
}
} catch (WayForPaySDKException $e) {
echo "WayForPay SDK exception: " . $e->getMessage();
}
32 changes: 32 additions & 0 deletions examples/serviceUrl.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/*
* This file is part of the WayForPay project.
*
* @link https://github.com/wayforpay/php-sdk
*
* @author Vladislav Lyshenko <vladdnepr1989@gmail.com>
* @copyright Copyright 2019 WayForPay
* @license https://opensource.org/licenses/MIT
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

require_once __DIR__ . '/../vendor/autoload.php';

use WayForPay\SDK\Credential\AccountSecretTestCredential;
use WayForPay\SDK\Exception\WayForPaySDKException;
use WayForPay\SDK\Handler\ServiceUrlHandler;

// Use test credential or yours
$credential = new AccountSecretTestCredential();
//$credential = new AccountSecretCredential('account', 'secret');

try {
$handler = new ServiceUrlHandler($credential);
$response = $handler->parseRequestFromPostRaw();

echo $handler->getSuccessResponse($response->getTransaction());
} catch (WayForPaySDKException $e) {
echo "WayForPay SDK exception: " . $e->getMessage();
}
1 change: 1 addition & 0 deletions src/Domain/PaymentSystems.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class PaymentSystems
const VISA_CHECKOUT = 'visaCheckout';
const GOOGLE_PAY = 'googlePay';
const APPLE_PAY = 'applePay';
const PAY_PARTS_MONO = 'payPartsMono';

private $default;

Expand Down
3 changes: 2 additions & 1 deletion src/Domain/TransactionBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
namespace WayForPay\SDK\Domain;

use DateTime;
use WayForPay\SDK\Exception\InvalidFieldException;

class TransactionBase
{
Expand Down Expand Up @@ -177,7 +178,7 @@ public function __construct(
$baseCurrency = null
) {
if (!in_array($status, $this->statusAllowed)) {
throw new \InvalidArgumentException(
throw new InvalidFieldException(
'Unexpected transaction type `' . $status . '`, expect one of ' .
implode(', ', $this->statusAllowed)
);
Expand Down
167 changes: 167 additions & 0 deletions src/Domain/TransactionService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
<?php
/*
* This file is part of the WayForPay project.
*
* @link https://github.com/wayforpay/php-sdk
*
* @author Vladislav Lyshenko <vladdnepr1989@gmail.com>
* @copyright Copyright 2019 WayForPay
* @license https://opensource.org/licenses/MIT
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace WayForPay\SDK\Domain;

use DateTime;

class TransactionService extends TransactionBase
{
/**
* @var CardToken
*/
private $recToken;

/**
* @var string
*/
private $authCode;

/**
* @var string
*/
private $repayUrl;

/**
* @param array $data
* @return TransactionService
* @throws \Exception
*/
public static function fromArray(array $data)
{
$default = array(
'merchantAccount' => '',
'orderReference' => '',
'merchantSignature' => '',
'amount' => 0,
'currency' => '',
'authCode' => 0,
'email' => '',
'phone' => '',
'createdDate' => 0,
'processingDate' => 0,
'cardPan' => '',
'cardType' => '',
'issuerBankCountry' => '',
'issuerBankName' => '',
'recToken' => '',
'transactionStatus' => '',
'reason' => '',
'reasonCode' => 0,
'fee' => 0,
'paymentSystem' => '',
'repayUrl' => '',
);

$data = array_merge($default, $data);

return new self(
$data['merchantTransactionType'],
$data['orderReference'],
new DateTime('@' . $data['createdDate']),
$data['amount'],
$data['currency'],
$data['transactionStatus'],
new DateTime('@' . $data['processingDate']),
$data['reasonCode'],
$data['reason'],
isset($data['email']) ? $data['email'] : null,
isset($data['phone']) ? $data['phone'] : null,
isset($data['paymentSystem']) ? $data['paymentSystem'] : null,
isset($data['cardPan']) ? $data['cardPan'] : null,
isset($data['cardType']) ? $data['cardType'] : null,
isset($data['issuerBankCountry']) ? $data['issuerBankCountry'] : null,
isset($data['issuerBankName']) ? $data['issuerBankName'] : null,
isset($data['fee']) ? $data['fee'] : null,
isset($data['baseAmount']) ? $data['baseAmount'] : null,
isset($data['baseCurrency']) ? $data['baseCurrency'] : null,
isset($data['authCode']) ? $data['authCode'] : null,
isset($data['recToken']) ? $data['recToken'] : null,
isset($data['repayUrl']) ? $data['repayUrl'] : null
);
}

public function __construct(
$merchantTransactionType,
$orderReference,
DateTime $createdDate,
$amount, $currency,
$status, DateTime
$processingDate,
$reasonCode,
$reason,
$email = null,
$phone = null,
$paymentSystem = null,
$cardPan = null,
$cardType = null,
$issuerBankCountry = null,
$issuerBankName = null,
$fee = null,
$baseAmount = null,
$baseCurrency = null,
$authCode = null,
$recToken = null,
$repayUrl = null
) {
parent::__construct(
$orderReference,
$createdDate,
$amount,
$currency,
$status,
$processingDate,
$reasonCode,
$reason,
$email,
$phone,
$paymentSystem,
$cardPan,
$cardType,
$issuerBankCountry,
$issuerBankName,
$fee,
$baseAmount,
$baseCurrency
);

$this->repayUrl = strval($repayUrl);
$this->authCode = strval($authCode);
$this->recToken = $recToken ? new CardToken($recToken) : null;
}

/**
* @return CardToken
*/
public function getRecToken()
{
return $this->recToken;
}

/**
* @return string
*/
public function getAuthCode()
{
return $this->authCode;
}

/**
* @return string
*/
public function getRepayUrl()
{
return $this->repayUrl;
}
}
2 changes: 1 addition & 1 deletion src/Exception/ApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

use WayForPay\SDK\Domain\Reason;

class ApiException extends \RuntimeException
class ApiException extends WayForPaySDKException
{
public function __construct(Reason $reason)
{
Expand Down
Loading

0 comments on commit 9c4ce83

Please sign in to comment.