Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
fre-sund committed Nov 29, 2019
2 parents e4a24cd + c5c7fb4 commit 8430b24
Show file tree
Hide file tree
Showing 18 changed files with 487 additions and 25 deletions.
40 changes: 31 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
* [4.5 Using the Svea PayPage](#i4-5)
* [4.6 Svea Checkout](#i4-6)
* [4.7 Account Credit](#i4-7)
* [4.8 Examples](#i4-8)
* [4.8 Swish](#i4-8)
* [4.9 Examples](#i4-9)
* [5. WebPayItem reference](#i5)
* [5.1 Specifying item price](#i5-1)
* [5.2 WebPayItem::orderRow()](#i5-2)
Expand Down Expand Up @@ -961,26 +962,46 @@ $response = $request->doRequest();

Another complete, runnable example of an invoice order can be found in the <a href="http://github.com/sveawebpay/php-integration/blob/master/example/accountcredit/" target="_blank">example/accountcredit</a> folder.

### 4.8 Swish payments <a name="i4-8"></a>
Get a html form containing the request XML data. The form is an instance of PaymentForm, and also contains the complete html form as a string along with the form elements in an array.

If you're testing Swish payments in stage, you can set any valid number and the payment will automatically be successful.

### 4.8 Examples <a name="i4-8"></a>
```php
<?php
...
$config = ConfigurationService::getDefaultConfig();
$form = WebPay::createOrder($config)
->addOrderRow(/*add any orderrow*/)
->setCountryCode("SE") //Required, must be set to "SE" for Swish Payments
->setClientOrderNumber("33") //Required
->setCurrency("SEK") //Required, must be set to "SEK" for Swish Payments
->setPayerAlias("46701234567") //Required, set consumers mobile number
->usePaymentMethod(PaymentMethod::SWISH) //Set payment method to SWISH
->setReturnUrl("http://myurl.se") //Required, this is where the user will be redirected after a successful payment
->setCallbackUrl("http://myurl.se/callback") //Optional, however VERY recommended as redirects from Swish to returnUrl might fail
->getPaymentForm();
...
```
### 4.9 Examples <a name="i4-9"></a>

#### 4.8.1 Svea checkout order
#### 4.9.1 Svea checkout order
Full Checkout examples order can be found in the [example/checkout](example/checkout) folder.

#### 4.8.2 Svea invoice order
#### 4.9.2 Svea invoice order
An example of a synchronous (invoice) order can be found in the [example/invoiceorder](example/invoiceorder) folder.

#### 4.8.3 Card order
#### 4.9.3 Card order
An example of an asynchronous card order can be found in the [example/cardorder](example/cardorder) folder.

#### 4.8.4 Recurring card order
#### 4.9.4 Recurring card order
An example of an recurring card order, both the setup transaction and a recurring payment, can be found in the [example/cardorder_recur](example/cardorder_recur) folder.

#### 4.8.5 Svea Account Credit
#### 4.9.5 Svea Account Credit
An example of an recurring card order, both the setup transaction and a recurring payment, can be found in the [example/accoutncredit(example/accoutncredit) folder.


#### 4.9.6 Swish
An example of an asynchronous swish payment can be found in the [example/swishorder](example/swishorder) folder.
[Back to top](#index)

## 5. WebPayItem reference <a name="i5"></a>
Expand Down Expand Up @@ -1904,7 +1925,7 @@ $response = $request->creditAccountCreditOrderRows()->doRequest(); // returns Cr

#### 7.5.1 Usage
The WebPayAdmin::creditOrderRows entrypoint method is used to credit rows in an order after it has been delivered.
Supports all payment methods. (To credit a Payment Plan order, contact Svea customer service.)
Supports all payment methods.

To credit an order row in full, you specify the index of the order row to credit (and for card orders, supply the numbered order row data itself).

Expand Down Expand Up @@ -2949,4 +2970,5 @@ Used in usePaymentMethod($paymentMethod) and in usePayPage()->includePaymentMeth
| PaymentMethod::SKRILL | Card payment with Dankort, Skrill. |
| PaymentMethod::INVOICE | Invoice by PayPage. |
| PaymentMethod::PAYMENTPLAN | PaymentPlan by PayPage. |
| PaymentMethod::SWISH | Swish, only Sweden |
```
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "sveaekonomi/webpay",
"version": "3.11.2",
"version": "3.12.0",
"description": "Php integration library for Svea Ekonomis payment methods",
"license": "Apache-2.0",
"authors": [
Expand Down
75 changes: 75 additions & 0 deletions example/swishorder/landingpage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

/**
* example file, how to handle a swish order request response
*
* @author Fredrik Sundell / fre-sund
*/

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

use Svea\WebPay\Response\SveaResponse;

error_reporting( E_ALL );
ini_set('display_errors', 'On');


// get config object
$myConfig = \Svea\WebPay\Config\ConfigurationService::getTestConfig();


// the raw request response is posted to the returnurl (this page) from Svea.
$rawResponse = $_REQUEST;

// decode the raw response by passing it through the Svea\WebPay\Response\SveaResponse class
try
{
$myResponse = new SveaResponse($rawResponse, $countryCode = NULL, $myConfig);
}
catch (Exception $e)
{
echo $e->getMessage();
}

// The decoded response is available through the ->getResponse() method.
// Check the response attribute 'accepted' for true to see if the request succeeded, if not, see the attributes resultcode and/or errormessage
echo "<pre>Your request response:\n\n";

print_r( $myResponse->getResponse() );

echo "\n</pre><font color='blue'><pre>\n\n
An example of a successful request response. The 'accepted' attribute is true (1), and resultcode/errormessage is not set.
Svea\WebPay\HostedService\HostedResponse\HostedPaymentResponse Object
(
[transactionId] => 722742
[clientOrderNumber] => order #2019-11-29T14:28:35 01:00
[paymentMethod] => SWISH
[merchantId] => 1130
[amount] => 3.75
[currency] => SEK
[accepted] => 1
[resultcode] =>
[errormessage] =>
)
)";

echo "\n</pre><font color='red'><pre>\n\n
An example of a rejected request response -- 'accepted' is false (0) and resultcode/errormessage indicates that the clientOrderNumber above has been reused, which is prohibited.
Svea\HostedPaymentResponse Object
(
[transactionId] => 582828
[clientOrderNumber] => order #2019-11-29T14:28:35 01:00
[paymentMethod] => SWISH
[merchantId] => 1130
[amount] => 3.75
[currency] => SEK
[accepted] => 0
[resultcode] => 127 (CUSTOMERREFNO_ALREADY_USED)
[errormessage] => Customer reference number already used in another transaction.
)";
?>
93 changes: 93 additions & 0 deletions example/swishorder/swishorder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php
/**
* Example of Swish payment
*
* @author Fredrik Sundell / fre-sund
*/

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

use Svea\WebPay\Config\ConfigurationService;
use Svea\WebPay\Constant\PaymentMethod;
use Svea\WebPay\WebPay;
use Svea\WebPay\WebPayItem;

error_reporting(E_ALL);
ini_set('display_errors', 'On');


// get config object
$myConfig = ConfigurationService::getTestConfig(); // add your Svea credentials into config_prod.php or config_test.php file

try // Handle validation exceptions by printing errors if we're unable to create the order
{
// Start the order creation process by creating the order builder object by calling Svea\WebPay\WebPay::createOrder():
$myOrder = WebPay::createOrder($myConfig);

// You then add information to the order object by using the methods in the Svea\WebPay\BuildOrder\CreateOrderBuilder class.
// For a Card order, the following methods are required:
$myOrder->setCurrency("SEK"); // order currency
$myOrder->setClientOrderNumber("order #" . date('c')); // required - use a not previously sent client side order identifier, i.e. "order #20140519-371"

// You may also chain fluent methods together:
$myOrder
->setCustomerReference("customer #123") // optional - This should contain a customer reference, as in "customer #123".
->setOrderDate("2019-11-29") // optional - or use an ISO8601 date as produced by i.e. date('c')
->setPayerAlias("46707937643") // required for Swish payments, ignored otherwise;
->setCountryCode("SE"); // countryCode "SE" is required for Swish payments


// Then specify the items bought as order rows, using the methods in the Svea\WebPay\BuildOrder\RowBuilders\OrderRow class, and adding them to the order:
$firstBoughtItem = WebPayItem::orderRow();
$firstBoughtItem->setAmountExVat(1.00);
$firstBoughtItem->setVatPercent(25);
$firstBoughtItem->setQuantity(1);
$firstBoughtItem->setDescription("Yellow duck");
$firstBoughtItem->setArticleNumber("yel-duck-01");

// Add firstBoughtItem to order row
$myOrder->addOrderRow($firstBoughtItem);

// Add secondBoughtItem in a fluent fashion
$myOrder->addOrderRow(
WebPayItem::orderRow()
->setAmountIncVat(2.50)
->setVatPercent(12)
->setQuantity(1)
->setDescription("Blue duck")
);

// Add Swish as the payment method for the order
$mySwishOrderRequest = $myOrder->usePaymentMethod(PaymentMethod::SWISH);


// Then set any additional required request attributes as detailed below. (See Svea\PaymentMethodPayment and Svea\HostedPayment classes for details.)
$mySwishOrderRequest
->setReturnUrl("http://localhost/" . getPath() . "/landingpage.php"); // The return url where we receive and process the finished request response

// Get a payment form object which you can use to send the payment request to Svea
$mySwishOrderRequest = $mySwishOrderRequest->getPaymentForm();

// Then send the form to Svea, and receive the response on the landingpage after the customer has completed the card checkout SveaCardPay
echo "<pre>";
print_r("press submit to send the swish payment request to Svea");
print_r($mySwishOrderRequest->completeHtmlFormWithSubmitButton);
}
catch (Exception $exception)
{
echo $exception->getMessage();
}
/**
* get the path to this file, for use in specifying the returnurl etc.
*/
function getPath()
{
$myURL = $_SERVER['SCRIPT_NAME'];
$myPath = explode('/', $myURL);
unset($myPath[count($myPath) - 1]);
$myPath = implode('/', $myPath);

return $myPath;
}


16 changes: 16 additions & 0 deletions src/BuildOrder/OrderBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@ class OrderBuilder
*/
public $peppolId;

/*
* @var string Mobile number to payer, only used with Swish else ignored
*/
public $payerAlias;

/**
* @param ConfigurationProvider $config
Expand Down Expand Up @@ -327,4 +331,16 @@ public function setPeppolId($peppolId)

return $this;
}

/**
* Required if using Swish, ignored if not
* @param string $payerAlias
* @return $this
*/
public function setPayerAlias($payerAlias)
{
$this->payerAlias = $payerAlias;

return $this;
}
}
35 changes: 34 additions & 1 deletion src/BuildOrder/Validator/HostedOrderValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,40 @@ public function validate($order)
$this->errors = $this->validateCountryCode($order, $this->errors); //should be optional for hosted payment because not used
$this->errors = $this->validateRequiredFieldsForOrder($order->order, $this->errors);
$this->errors = $this->validateOrderRows($order, $this->errors);
$this->errors = $this->validatePayerAlias($order, $this->errors); // validate for swish

return $this->errors;
}

/**
* @param type $order
* @param array $errors
* @return array
*/
private function validatePayerAlias($order, $errors)
{
if (isset($order->order->payerAlias) && $order->paymentMethod == "SWISH")
{
if(ctype_digit($order->order->payerAlias) == false)
{
$errors['incorrect type'] = 'payerAlias must be numeric and can not contain any non-numeric characters';
}
if(strlen($order->order->payerAlias) != 11)
{
$errors['incorrect length'] = 'payerAlias must be 11 digits';
}
if($order->order->countryCode != "SE")
{
$errors['incorrect value'] = 'countryCode must be set to "SE" if payment method is SWISH';
}
}
elseif(isset($order->order->payerAlias) == false && isset($order->paymentMethod) && $order->paymentMethod == "SWISH")
{
$errors['missing value'] = 'payerAlias must be set if using payment method SWISH. Use function setPayerAlias()';
}
return $errors;
}

/**
* @param type $order
* @param array $errors
Expand All @@ -41,7 +71,10 @@ private function validateClientOrderNumber($order, $errors)
if (isset($order->clientOrderNumber) == false || "" == $order->clientOrderNumber) {
$errors['missing value'] = "ClientOrderNumber is required. Use function setClientOrderNumber().";
}

/*if(isset($order->clientOrderNumber) && $order->paymentMethod == "SWISH")
{
$errors['incorrect value'] = "ClientOrderNumber cannot be longer than 35 characters for Swish payments";
}*/
return $errors;
}

Expand Down
5 changes: 3 additions & 2 deletions src/Constant/PaymentMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ abstract class PaymentMethod
{
const INVOICE = 'INVOICE';
const PAYMENTPLAN = 'PAYMENTPLAN';

//DIRECT
const BANKAXESS = 'BANKAXESS';
const AKTIA_FI = 'DBAKTIAFI';
Expand All @@ -28,7 +28,8 @@ abstract class PaymentMethod
const SPANKKI_FI = 'DBSPANKKIFI';
const SWEDBANK_SE = 'DBSWEDBANKSE';
const TAPIOLA_FI = 'DBTAPIOLAFI';

const SWISH = 'SWISH';

//PaymentMethodType::CARD
const KORTCERT = 'KORTCERT';
const SVEACARDPAY = 'SVEACARDPAY';
Expand Down
5 changes: 5 additions & 0 deletions src/HostedService/Helper/HostedXmlBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ public function getPaymentXML($request, $order)
$this->XMLWriter->writeElement("ipaddress", $request['ipAddress']);
}

if(isset($request['payerAlias']))
{
$this->XMLWriter->writeElement("payeralias", $request['payerAlias']);
}

$this->XMLWriter->endElement();
$this->XMLWriter->endDocument();

Expand Down
10 changes: 8 additions & 2 deletions src/HostedService/HostedResponse/HostedResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,10 @@ protected function setErrorParams($resultcode)
$this->resultcode = $resultcode . ' (ILLEGAL_CREDIT_USER)';
$this->errormessage = 'User is not allowed to perform credit operation.';
break;

case '146':
$this->resultcode = $resultcode . ' (CUSTOMER_NOT_FOUND)';
$this->errormessage = 'Customer is not enrolled for Swish or if another payment method was used the address lookup returned no results.';
break;
case '300':
$this->resultcode = $resultcode . ' (BAD_CARDHOLDER_NAME)';
$this->errormessage = 'Invalid value for cardholder name.';
Expand Down Expand Up @@ -363,7 +366,10 @@ protected function setErrorParams($resultcode)
$this->resultcode = $resultcode . ' (TIMEOUT)';
$this->errormessage = 'Timeout at Svea.';
break;

case '342':
$this->resultcode = $resultcode . ' (TRANSACTION_ALREADY_IN_PROGRESS)';
$this->errormessage = 'There is currently another transaction in progress on this payer alias.';
break;
case '500':
$this->resultcode = $resultcode . ' (ANTIFRAUD_CARDBIN_NOT_ALLOWED)';
$this->errormessage = 'Antifraud - cardbin not allowed.';
Expand Down
Loading

0 comments on commit 8430b24

Please sign in to comment.