<?php
namespace com\whitemagicsoftware;
require_once( 'stripe/lib/Stripe.php' );
use Stripe;
use Stripe_Charge;
use Stripe_ApiConnectionError;
use Stripe_AuthenticationError;
use Stripe_CardError;
use Stripe_Error;
use Stripe_InvalidRequestError;
class Payment extends Obj {
private $token;
private $email;
private $tally;
private $clientTotal;
function __construct() {
parent::__construct();
global $PURCHASE_KEY_PRIVATE;
global $PURCHASE_KEY_PUBLIC;
$stripe = array(
"secret_key" => $PURCHASE_KEY_PRIVATE,
"publishable_key" => $PURCHASE_KEY_PUBLIC
);
Stripe::setApiKey( $stripe['secret_key'] );
}
public function accept() {
global $PURCHASE_PRICE_BASE;
global $PURCHASE_PRICE_PROVIDER_BASE;
$baseCost = $PURCHASE_PRICE_BASE + $PURCHASE_PRICE_PROVIDER_BASE;
$result = false;
$tally = $this->getTally();
if( isset( $tally ) && is_int( $tally ) && $tally > 0 ) {
$token = $this->getToken();
$email = $this->getEmail();
$price = $this->getClientTotal();
if( isset( $token ) && isset( $email ) && isset( $price ) ) {
global $PURCHASE_PRICE_MULTIPLIER;
$total = $baseCost + ($PURCHASE_PRICE_MULTIPLIER * $tally);
if( $price == $total ) {
$result = $this->charge( $token, $email, $total );
}
}
}
return $result;
}
private function charge( $token, $email, $amount ) {
global $PURCHASE_PRICE_CURRENCY;
$result = false;
try {
$charge = Stripe_Charge::create(
array(
"amount" => $amount,
"currency" => $PURCHASE_PRICE_CURRENCY,
"card" => $token,
"description" => "$email"
)
);
$result = true;
}
catch( Stripe_CardError $ex ) {
$this->log( $ex );
}
catch( Stripe_InvalidRequestError $ex ) {
$this->log( $ex );
}
catch( Stripe_AuthenticationError $ex ) {
$this->log( $ex );
}
catch( Stripe_ApiConnectionError $ex ) {
$this->log( $ex );
}
catch( Stripe_Error $ex ) {
$this->log( $ex );
}
catch( Exception $ex ) {
$this->log( $ex );
}
return $result;
}
public function setToken( $token ) {
$this->token = $token;
}
private function getToken() {
return $this->token;
}
public function setEmail( $email ) {
$this->email = $email;
}
private function getEmail() {
return $this->email;
}
public function setTally( $tally ) {
$this->tally = $tally;
}
private function getTally() {
return $this->tally;
}
public function setClientTotal( $clientTotal ) {
$this->clientTotal = $clientTotal;
}
private function getClientTotal() {
return $this->clientTotal;
}
}