Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 67 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Paymentwall_Base::setSecretKey('YOUR_SECRET_KEY'); // available in your Paymentw
```

#### Widget Call
[Web API details](http://www.paymentwall.com/en/documentation/Digital-Goods-API/710#paymentwall_widget_call_flexible_widget_call)
[Web API details](https://docs.paymentwall.com/integration/widget/digital-goods#widget-call)

The widget is a payment page hosted by Paymentwall that embeds the entire payment flow: selecting the payment method, completing the billing details, and providing customer support via the Help section. You can redirect the users to this page or embed it via iframe. Below is an example that renders an iframe with Paymentwall Widget.

Expand Down Expand Up @@ -91,6 +91,72 @@ if ($pingback->validate()) {
}
```

## Digital Checkout API

#### Initializing Paymentwall

Using Paymentwall PHP Library v2:
```php
require_once('/path/to/paymentwall-php/lib/paymentwall.php');
Paymentwall_Config::getInstance()->set(array(
'api_type' => Paymentwall_Config::API_CHECKOUT,
'public_key' => 'YOUR_PUBLIC_KEY',
'private_key' => 'YOUR_PRIVATE_KEY'
));
```

#### Widget Call
[Web API details](https://docs.paymentwall.com/integration/widget/digital-goods#widget-call)

The widget is a payment page hosted by Paymentwall that embeds the entire payment flow: selecting the payment method, completing the billing details, and providing customer support via the Help section. You can redirect the users to this page or embed it via iframe. Below is an example that renders an iframe with Paymentwall Widget.

```php
$widget = new Paymentwall_Widget(
'user40012', // id of the end-user who's making the payment
'pw', // widget code, e.g. pw; can be picked inside of your merchant account
array( // product details for Flexible Widget Call. To let users select the product on Paymentwall's end, leave this array empty
new Paymentwall_Product(
'product301', // id of the product in your system
9.99, // price
'USD', // currency code
'Gold Membership', // product name
Paymentwall_Product::TYPE_SUBSCRIPTION, // this is a time-based product; for one-time products, use Paymentwall_Product::TYPE_FIXED and omit the following 3 array elements
1, // duration is 1
Paymentwall_Product::PERIOD_TYPE_MONTH, // month
true // recurring
)
),
array('email' => 'user@hostname.com') // additional parameters
);
echo $widget->getHtmlCode();
```

#### Pingback Processing

The Pingback is a webhook notifying about a payment being made. Pingbacks are sent via HTTP/HTTPS to your servers. To process pingbacks use the following code:
```php
require_once('/path/to/paymentwall-php/lib/paymentwall.php');
Paymentwall_Config::getInstance()->set(array(
'api_type' => Paymentwall_Config::API_CHECKOUT,
'public_key' => 'YOUR_PUBLIC_KEY',
'private_key' => 'YOUR_PRIVATE_KEY'
));
$pingback = new Paymentwall_Pingback($_GET, $_SERVER['REMOTE_ADDR']);
if ($pingback->validate()) {
$productId = $pingback->getProduct()->getId();
if ($pingback->isDeliverable()) {
// deliver the product
} else if ($pingback->isCancelable()) {
// withdraw the product
} else if ($pingback->isUnderReview()) {
// set "pending" status to order
}
echo 'OK'; // Paymentwall expects response to be OK, otherwise the pingback will be resent
} else {
echo $pingback->getErrorSummary();
}
```

## Virtual Currency API

#### Initializing Paymentwall
Expand Down
36 changes: 30 additions & 6 deletions lib/Paymentwall/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,44 @@

class Paymentwall_Config
{
const VERSION = '2.0.0';

const API_BASE_URL = 'https://api.paymentwall.com/api';
const VERSION = '2.2.2';
const BASE_URL = 'https://api.paymentwall.com';

/**
* @deprecated 2.2.2
*/
const API_BASE_URL = self::BASE_URL . '/api';
const API_VC = 1;
const API_GOODS = 2;
const API_CART = 3;
const API_CHECKOUT = 4;

protected $baseUrl = self::BASE_URL;
protected $apiType = self::API_GOODS;
protected $publicKey;
protected $privateKey;
protected $apiBaseUrl = self::API_BASE_URL;

private static $instance;

public function setBaseUrl($url)
{
$this->baseUrl = $url;
}

public function getBaseUrl()
{
return $this->baseUrl;
}

public function getApiBaseUrl()
{
return $this->apiBaseUrl;
return $this->apiBaseUrl ? $this->apiBaseUrl : $this->getBaseUrl() . '/api';
}

/**
* @deprecated 2.2.2 Should use setBaseUrl instead
*/
public function setApiBaseUrl($url = '')
{
$this->apiBaseUrl = $url;
Expand Down Expand Up @@ -69,9 +87,15 @@ public function isTest()

public function set($config = array())
{
/**
* @deprecated 2.2.2
*/
if (isset($config['api_base_url'])) {
$this->setApiBaseUrl($config['api_base_url']);
}
if (isset($config['base_url'])) {
$this->setBaseUrl($config['base_url']);
}
if (isset($config['api_type'])) {
$this->setLocalApiType($config['api_type']);
}
Expand All @@ -84,8 +108,8 @@ public function set($config = array())
}

/**
* @return $this Returns class instance.
*/
* @return $this Returns class instance.
*/
public static function getInstance()
{
if (!isset(self::$instance)) {
Expand Down
5 changes: 5 additions & 0 deletions lib/Paymentwall/Instance.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ protected function getConfig()
return $this->config;
}

protected function getBaseUrl()
{
return $this->getConfig()->getBaseUrl();
}

protected function getApiBaseUrl()
{
return $this->getConfig()->getApiBaseUrl();
Expand Down
5 changes: 5 additions & 0 deletions lib/Paymentwall/Pingback.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ public function isSignatureValid()

$signatureParams = array('uid', 'goodsid', 'slength', 'speriod', 'type', 'ref');

} else if ($this->getApiType() == Paymentwall_Config::API_CHECKOUT) {
$signatureParams = array('uid', 'goodsid', 'slength', 'speriod', 'type', 'ref');

} else { // API_CART

$signatureParams = array('uid', 'goodsid', 'type', 'ref');
Expand Down Expand Up @@ -116,6 +119,8 @@ public function isParametersValid()
$requiredParams = array('uid', 'currency', 'type', 'ref', 'sig');
} else if ($this->getApiType() == Paymentwall_Config::API_GOODS) {
$requiredParams = array('uid', 'goodsid', 'type', 'ref', 'sig');
} else if ($this->getApiType() == Paymentwall_Config::API_CHECKOUT) {
$requiredParams = array('uid', 'goodsid', 'type', 'ref', 'sig');
} else { // Cart API
$requiredParams = array('uid', 'goodsid', 'type', 'ref', 'sig');
}
Expand Down
53 changes: 31 additions & 22 deletions lib/Paymentwall/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Paymentwall_Widget extends Paymentwall_Instance
{
const CONTROLLER_PAYMENT_VIRTUAL_CURRENCY = 'ps';
const CONTROLLER_PAYMENT_DIGITAL_GOODS = 'subscription';
const CONTROLLER_PAYMENT_CHECKOUT = 'v1/checkout/orders';
const CONTROLLER_PAYMENT_CART = 'cart';

protected $userId;
Expand All @@ -28,7 +29,9 @@ public function getUrl()

$productsNumber = count($this->products);

if ($this->getApiType() == Paymentwall_Config::API_GOODS) {
$apiTypes = array(Paymentwall_Config::API_GOODS, Paymentwall_Config::API_CHECKOUT);

if (in_array($this->getApiType(), $apiTypes)) {

if (!empty($this->products)) {

Expand Down Expand Up @@ -105,7 +108,11 @@ public function getUrl()
$signatureVersion
);

return $this->getApiBaseUrl() . '/' . $this->buildController($this->widgetCode) . '?' . http_build_query($params);
$baseUrl = function () {
return $this->getApiType() == Paymentwall_Config::API_CHECKOUT ? $this->getBaseUrl() : $this->getApiBaseUrl() ;
};

return $baseUrl() . '/' . $this->buildController($this->widgetCode) . '?' . http_build_query($params);
}

public function getHtmlCode($attributes = array())
Expand All @@ -131,24 +138,26 @@ protected function getDefaultSignatureVersion() {
return $this->getApiType() != Paymentwall_Config::API_CART ? Paymentwall_Signature_Abstract::DEFAULT_VERSION : Paymentwall_Signature_Abstract::VERSION_TWO;
}

protected function buildController($widget = '')
{
$controller = null;
$isPaymentWidget = !preg_match('/^w|s|mw/', $widget);

if ($this->getApiType()== Paymentwall_Config::API_VC) {
if ($isPaymentWidget) {
$controller = self::CONTROLLER_PAYMENT_VIRTUAL_CURRENCY;
}
} else if ($this->getApiType() == Paymentwall_Config::API_GOODS) {
/**
* @todo cover case with offer widget for digital goods for non-flexible widget call
*/
$controller = self::CONTROLLER_PAYMENT_DIGITAL_GOODS;
} else {
$controller = self::CONTROLLER_PAYMENT_CART;
}

return $controller;
}
protected function buildController($widget = '')
{
$controller = null;
$isPaymentWidget = !preg_match('/^w|s|mw/', $widget);

if ($this->getApiType()== Paymentwall_Config::API_VC) {
if ($isPaymentWidget) {
$controller = self::CONTROLLER_PAYMENT_VIRTUAL_CURRENCY;
}
} else if ($this->getApiType() == Paymentwall_Config::API_GOODS) {
/**
* @todo cover case with offer widget for digital goods for non-flexible widget call
*/
$controller = self::CONTROLLER_PAYMENT_DIGITAL_GOODS;
} else if ($this->getApiType() == Paymentwall_Config::API_CHECKOUT) {
$controller = self::CONTROLLER_PAYMENT_CHECKOUT;
} else {
$controller = self::CONTROLLER_PAYMENT_CART;
}

return $controller;
}
}