Skip to content

Instantly share code, notes, and snippets.

@tianlim
Created June 28, 2013 19:34
Show Gist options
  • Save tianlim/5887436 to your computer and use it in GitHub Desktop.
Save tianlim/5887436 to your computer and use it in GitHub Desktop.
Integrate Payment Gateway Skrill (Moneybookers) to BoxBilling
<?php
/*
*/
class Payment_Adapter_Skrill
{
private $config = array();
public function __construct($config)
{
$this->config = $config;
if(!function_exists('curl_exec')) {
throw new Exception('PHP Curl extension must be enabled in order to use Skrill gateway');
}
if(!$this->config['email']) {
throw new Exception('Payment gateway "Skrill" is not configured properly. Please update configuration parameter "Skrill Email address" at "Configuration -> Payments".');
}
}
public static function getConfig()
{
return array(
'supports_one_time_payments' => true,
'supports_subscriptions' => true,
'description' => 'Enter your Skrill email to start accepting payments by Skrill.',
'form' => array(
'email' => array('text', array(
'label' => 'Skrill email address for payments',
'validators'=>array('EmailAddress'),
),
),
),
);
}
public function getHtml($api_admin, $invoice_id, $subscription)
{
$invoice = $api_admin->invoice_get(array('id'=>$invoice_id));
$buyer = $invoice['buyer'];
if($subscription) {
$subs = $invoice['subscription'];
$data = array();
// Merchant Details
$data['pay_to_email'] = $this->config['email'];
$data['return_url'] = $this->config['return_url'];
$data['cancel_url'] = $this->config['cancel_url'];
$data['status_url'] = $this->config['notify_url'];
$data['language'] = 'EN';
// Customer Details
$data['pay_from_email'] = $buyer['email'];
$data['firstname'] = $buyer['first_name'];
$data['lastname'] = $buyer['last_name'];
$data['address'] = $buyer['address'];
$data['phone_number'] = $buyer['phone'];
$data['postal_code'] = $buyer['zip'];
$data['city'] = $buyer['city'];
$data['state'] = $buyer['state'];
$data['country'] = $buyer['country'];
// Payment Details
$data['currency'] = $invoice['currency'];
$data['detail1_description']= 'detail1_description';
$data['detail1_text'] = 'detail1_text';
// Recurring Billing
$data['rec_status_url'] = $this->config['notify_url'].'&rec_payment=cancelled';
$data['rec_amount'] = $invoice['total'];
$data['rec_period'] = $subs['cycle'];
if($subs['unit']=='M') {
$data['rec_cycle'] = 'month';
} elseif($subs['unit']=='Y') {
$data['rec_cycle'] = 'year';
} elseif($subs['unit']=='W') {
$data['rec_cycle'] = 'day';
$data['rec_period'] = 7*$subs['cycle'];
}
} else {
$data = array();
// Merchant Details
$data['pay_to_email'] = $this->config['email'];
$data['return_url'] = $this->config['return_url'];
$data['cancel_url'] = $this->config['cancel_url'];
$data['status_url'] = $this->config['notify_url'];
$data['language'] = 'EN';
// Payment Details
$data['amount'] = $invoice['total'];
$data['currency'] = $invoice['currency'];
$data['detail1_description']= 'detail1_description';
$data['detail1_text'] = 'detail1_text';
}
if($this->config['test_mode']) {
$url = ''; // todo: testing account?
} else {
$url = 'https://www.moneybookers.com/app/payment.pl';
}
return $this->_generateForm($url, $data);
}
public function processTransaction($api_admin, $id, $data, $gateway_id)
{
if(APPLICATION_ENV != 'testing' && !$this->_isIpnValid($data)) {
throw new Exception('IPN is not valid');
}
$ipn = $data['post'];
$invoice = $api_admin->invoice_get(array('id'=>$data['get']['bb_invoice_id']));
$client_id = $invoice['client']['id'];
// Determine BB txn_type
// have: payment, refund, subscription_create, subscription_cancel, unknown
if ($data['get']['rec_payment']=='cancelled')
{
$txn_type = 'subscription_cancel';
} else {
$txn_type = 'payment';
}
// Determine BB txn_status
switch ($ipn['status']){
case '2': // MB processed
$txn_status='complete';
break;
case '0': // MB pending
$txn_status='pending';
break;
case '-2': // MB failed
case '-1': // MB cancelled
case '-3': // MB chargeback
default:
$txn_status='unknown';
break;
}
// Update Transaction
$tx_data = array(
'id' => $id,
'invoice_id' => $data['get']['bb_invoice_id'],
'txn_status' => $txn_status,
'txn_id' => $ipn['mb_transaction_id'],
'amount' => $ipn['mb_amount'],
'currency' => $ipn['mb_currency'],
'type' => $txn_type,
'status' => $txn_status,
'updated_at' => date('c'),
);
$api_admin->invoice_transaction_update($tx_data);
switch ($txn_type) {
case 'payment':
// Update Client Balance
if ($txn_status='complete') {
$bd = array(
'id' => $client_id,
'amount' => $ipn['mb_amount'],
'description' => 'Skrill transaction '.$ipn['mb_transaction_id'],
'type' => 'Skrill',
'rel_id' => $ipn['mb_transaction_id'],
);
$api_admin->client_balance_add_funds($bd);
$api_admin->invoice_batch_pay_with_credits(array('client_id'=>$client_id));
}
// todo: implement creating recurring billing here.
// rec_payment_type=='recurring'
// BB sid = MB rec_payment_id
break;
case 'subscription_cancel':
// todo: find out subscription_id from MB? rec_payment_id?
// reserved code
//$s = $api_admin->invoice_subscription_get(array('sid'=>$ipn['subscr_id']));
//$api_admin->invoice_subscription_update(array('id'=>$s['id'], 'status'=>'canceled'));
break;
}
}
// todo: need validation. use $data['post']['md5sig']. read MBs_gateway_manual pdf, page 18, point IV.
private function _isIpnValid($data)
{
return true;
}
private function _generateForm($url, $data, $method = 'post')
{
$form = '';
$form .= '<form name="payment_form" action="'.$url.'" method="'.$method.'">' . PHP_EOL;
foreach($data as $key => $value) {
$form .= sprintf('<input type="hidden" name="%s" value="%s" />', $key, $value) . PHP_EOL;
}
$form .= '<input class="bb-button bb-button-submit" type="submit" value="Pay with Skrill" id="payment_button"/>'. PHP_EOL;
$form .= '</form>' . PHP_EOL . PHP_EOL;
if(isset($this->config['auto_redirect']) && $this->config['auto_redirect']) {
$form .= sprintf('<h2>%s</h2>', __('Redirecting to Skrill.com'));
$form .= "<script type='text/javascript'>$(document).ready(function(){ document.getElementById('payment_button').style.display = 'none'; document.forms['payment_form'].submit();});</script>";
}
return $form;
}
}
@dhavaljani1990
Copy link

Can it possible for Skrill to do the payment automatic without clicking on Skrill's pay button?

@MnariMohamed
Copy link

same question as above, 2021 no answer :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment