<?

### DO THIS FIRST:
include_once("/BANGK!/master_config.php");
include_once($master_config_root_path . "/shared/functions.shared");
include_once($master_config_root_path . "/shared/session.shared");
//include($_SESSION['template']['company']['file_path'] . "shared/start_HTML.shared");
##################################################################################





/*
	
// 1. Autoload the SDK Package. This will include all the files and classes to your autoloader
require __DIR__  . '/PayPal-PHP-SDK/autoload.php';
// 2. Provide your Secret Key. Replace the given one with your app clientId, and Secret
// https://developer.paypal.com/webapps/developer/applications/myapps
$apiContext = new \PayPal\Rest\ApiContext(
    new \PayPal\Auth\OAuthTokenCredential(
        'AUK_K0UjF32crbLQFAIH1VrGnSLKO00NmGKC-RNiCCeUHrwBRgGHkkWSixlZJZsbBUdFw1zIS0VOMUiH',     // ClientID
        'ELk7XDAmqYZ9UzmGewvvwjabTBQWhwv46fsKItp8KJyvjLEsJMzTV6P4OebIajWQec5LG7HfAkhJPd0Y'      // ClientSecret
    )
);


//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
// START: SaveACard
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////



// 3. Lets try to save a credit card to Vault using Vault API mentioned here
// https://developer.paypal.com/webapps/developer/docs/api/#store-a-credit-card
$creditCard = new \PayPal\Api\CreditCard();
$creditCard->setType("visa")
    ->setNumber("4417119669820331")
    ->setExpireMonth("11")
    ->setExpireYear("2019")
    ->setCvv2("012")
    ->setFirstName("Joe")
    ->setLastName("Shopper");
// 4. Make a Create Call and Print the Card
try {
    $creditCard->create($apiContext);
    echo $creditCard;
}
catch (\PayPal\Exception\PayPalConnectionException $ex) {
    // This will print the detailed information on the exception. 
    //REALLY HELPFUL FOR DEBUGGING
    echo $ex->getData();
}

logger3("creditCard",$creditCard);

*/


//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
// END: SaveACard
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////



//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
// START: CreatePaymentSample
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////

/*API:
	
	$paypal_setType
    $paypal_setNumber
    $paypal_setExpireMonth
    $paypal_setExpireYear
    $paypal_setCvv2
    $paypal_setFirstName
    $paypal_setLastName
 
 */
 
 

    
    
 
 
// This sample code demonstrate how you can process
// a direct credit card payment. Please note that direct 
// credit card payment and related features using the 
// REST API is restricted in some countries.
// API used: /v1/payments/payment

require __DIR__  . '/PayPal-PHP-SDK/autoload.php';
// 2. Provide your Secret Key. Replace the given one with your app clientId, and Secret
// https://developer.paypal.com/webapps/developer/applications/myapps


//photoBANGK! APP: SANDBOX:
# $apiContext = new \PayPal\Rest\ApiContext(
#     new \PayPal\Auth\OAuthTokenCredential(
#         'AUK_K0UjF32crbLQFAIH1VrGnSLKO00NmGKC-RNiCCeUHrwBRgGHkkWSixlZJZsbBUdFw1zIS0VOMUiH',     // ClientID
#         'ELk7XDAmqYZ9UzmGewvvwjabTBQWhwv46fsKItp8KJyvjLEsJMzTV6P4OebIajWQec5LG7HfAkhJPd0Y'      // ClientSecret
#     )
# );


# /// photoBANGK! APP: LIVE:
$apiContext = new \PayPal\Rest\ApiContext(
    new \PayPal\Auth\OAuthTokenCredential(
        'ARQ4OpUb79tF52eP3U3LFQ29X3wahLxqLmZdiASCdlEpd_WjH0-e8JFhArZPvGVeJ1szQdkt9E7eQ2HX',     // ClientID
        'EKWSvO6IdtuVLGIxDl__eyenDGWaPYQPtwLGaNODAlj0SrrswiQ3HRIccU7cbBz0VfmLEsmt6OsmRhcm'      // ClientSecret
    )
);


use PayPal\Api\Amount;
use PayPal\Api\CreditCard;
use PayPal\Api\Details;
use PayPal\Api\FundingInstrument;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\Transaction;
// ### CreditCard
// A resource representing a credit card that can be
// used to fund a payment.
$card = new CreditCard();
$card->setType($paypal_setType)
    ->setNumber($paypal_setNumber)
    ->setExpireMonth($paypal_setExpireMonth)
    ->setExpireYear($paypal_setExpireYear)
    ->setCvv2($paypal_setCvv2)
    ->setFirstName($paypal_setFirstName)
    ->setLastName($paypal_setLastName);
// ### FundingInstrument
// A resource representing a Payer's funding instrument.
// For direct credit card payments, set the CreditCard
// field on this object.
$fi = new FundingInstrument();
$fi->setCreditCard($card);
// ### Payer
// A resource representing a Payer that funds a payment
// For direct credit card payments, set payment method
// to 'credit_card' and add an array of funding instruments.
$payer = new Payer();
$payer->setPaymentMethod("credit_card")
    ->setFundingInstruments(array($fi));
// ### Itemized information
// (Optional) Lets you specify item wise
// information

$item_number = 0;
//logger3("_SESSION['gallery']['cart']",$_SESSION['gallery']['cart']);
$items = "";
$items = array();
$paypal_setTotal = "0.00";
//exit;
foreach($_SESSION['gallery']['cart'] as $key => $cart_row_details) {
	

	$tmp_item_price = "";
	if(stristr($cart_row_details['photo_media'],"screen")) {
		$tmp_item_price = $cart_row_details['price_online'];
		$tmp_image_type = "SOCIAL Size";
	}
	
	if(stristr($cart_row_details['photo_media'],"wallpaper")) {
		$tmp_item_price = $cart_row_details['price_wallpaper'];
		$tmp_image_type = "WALLPAPER Size";
	}
	
	if(stristr($cart_row_details['photo_media'],"print")) {
		$tmp_item_price = $cart_row_details['price_print'];
		$tmp_image_type = "ORIGINAL Size";
	}
	

	
	$items[$item_number] = new Item();
	$items[$item_number]->setName($cart_row_details['filename'])
	    ->setDescription($tmp_image_type)
	    ->setCurrency('USD')
	    ->setQuantity(1)
	    ->setTax(0.0)
	    ->setPrice($tmp_item_price);
	
	$paypal_setTotal = number_format($paypal_setTotal + $tmp_item_price,2);
	$item_number++;
}
//logger3("paypal_setTotal",$paypal_setTotal);
//logger3("items",$items);
//exit;
$itemList = new ItemList();
$itemList->setItems($items);
// ### Additional payment details
// Use this optional field to set additional
// payment information such as tax, shipping
// charges etc.
$details = new Details();
$details->setShipping(0.0)
    ->setTax(0.0)
    ->setSubtotal($paypal_setTotal);
// ### Amount
// Lets you specify a payment amount.
// You can also specify additional details
// such as shipping, tax.
$amount = new Amount();
$amount->setCurrency("USD")
    ->setTotal($paypal_setTotal)
    ->setDetails($details);
// ### Transaction
// A transaction defines the contract of a
// payment - what is the payment for and who
// is fulfilling it. 
//logger3("amount",$amount);
$transaction = new Transaction();
$transaction->setAmount($amount)
    ->setItemList($itemList)
    ->setDescription("photo.BANGK! Photo Download(s)")
    ->setInvoiceNumber(uniqid());
    
//test("transaction",$transaction);
//exit;
// ### Payment
// A Payment Resource; create one using
// the above types and intent set to sale 'sale'
$payment = new Payment();
$payment->setIntent("sale")
    ->setPayer($payer)
    ->setTransactions(array($transaction));


//exit;

// For Sample Purposes Only.
//$request = clone $payment;
// ### Create Payment
// Create a payment by calling the payment->create() method
// with a valid ApiContext (See bootstrap.php for more on `ApiContext`)
// The return object contains the state.

try {
    $payment->create($apiContext);
} catch (PayPal\Exception\PayPalConnectionException $ex) {
    $paypal_error_code = $ex->getCode(); // Prints the Error Code
    test("paypal_error_code",$paypal_error_code);
    $paypal_error_details = $ex->getData(); // Prints the detailed error message 
     test("paypal_error_details",$paypal_error_details);
    die($ex);
} catch (Exception $ex) {
    die($ex);
}
logger3("ex",$ex);
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
// ResultPrinter::printResult('Create Payment Using Credit Card', 'Payment', $payment->getId(), $request, $payment);


$tmp_sql = "SELECT * FROM accounts WHERE emailaddress = 'shea.writer@nsdb.com';";
$results = object2array(readTHECASHIER($tmp_sql));
logger3("results",$results);


//logger3("payment",$payment['_propMap:PayPal\Common\PayPalModel:private']);

//logger3("payment",$payment);
$_SESSION['payme']['gateway']['response'] = json_decode($payment);

//logger3("w_SESSION['payme']['gateway']['response']",$_SESSION['payme']['gateway']['response']);

$_SESSION['payme']['gateway']['response'] = object2array($_SESSION['payme']['gateway']['response']);

logger3("w_SESSION['payme']['gateway']['response']",$_SESSION['payme']['gateway']['response']);
logger3("w_SESSION['payme']['gateway']['response']['intent']",$_SESSION['payme']['gateway']['response']['intent']);
logger3("w_SESSION['payme']['gateway']['response'][id]",$_SESSION['payme']['gateway']['response'][id]);
logger3("w_SESSION['payme']['gateway']['response'][state]",$_SESSION['payme']['gateway']['response'][state]);

//exit;


$tmp_datetime = datetime();
$tmp_sql = "INSERT INTO `translog` (
`translogid`,
`platform`,
`gatewayid`,
`hash`,
`service`,
`action`,
`business_accountid`,
`business_account_number`,
`memberid`,
`accountid`,
`acquirer`,
`amount`,
`type`,
`name`,
`last_4_ssn`,
`number`,
`credit_card_number`,
`expdate`,
`expiration_month`,
`expiration_year`,
`streetaddress1`,
`streetaddress2`,
`city`,
`region`,
`country`,
`postalcode`,
`phone`,
`cvv2`,
`customer_emailaddress`,
`DOB`,
`iswipe`,
`ordernumber`,
`response_raw`,
`response`,
`result`,
`status`,
`message`,
`transactionid_from_gateway`,
`maxmind_response`,
`maxmind_result`,
`payvision_transactionId`,
`payvision_transactionGuid`,
`error`,
`avs`,
`code`,
`note`,
`session`,
`chargeback_transactionid`,
`chargeback_reason`,
`datetime_created`,
`datetime_updated`,
`UID`,
`sepa_country`,
`sepa_bank_name`,
`sepa_account_number`,
`sepa_routing_number`,
`sepa_iban`,
`sepa_bic`,
`sepa_bank_city`,
`sepa_firstname`,
`sepa_lastname`,
`sepa_street`,
`sepa_zip`,
`sepa_city`,
`ip`
) VALUES (
'$insert_translogid',
'photo.BANGK!',
'PayPal',
'". hash_me() ."',
'STANDARD',
'PAYMENT',
'". $_SESSION['payment']['business']['account']['accountid'] ."',
'". $_SESSION['payment']['business']['account']['hash'] ."',
'". $_SESSION['member']['memberid'] ."',
'". $_SESSION['account']['accountid'] ."',
'PayPal',
'". $paypal_setTotal ."',
'". $paypal_setType ."',
'". $paypal_setFirstName . " " . $paypal_setLastName ."',
'$insert_last_4_ssn',
'" . $paypal_setNumber ."',
'" . $paypal_setNumber ."',
'" . $paypal_setExpireMonth . "/". $paypal_setExpireYear ."',
'". $paypal_setExpireMonth ."',
'". $paypal_setExpireYear ."',
'$insert_streetaddress1',
'$insert_streetaddress2',
'$insert_city',
'$insert_region',
'$insert_country',
'$insert_postalcode',
'$insert_phone',
'$insert_cvv2',
'$insert_customer_emailaddress',
'$insert_DOB',
'$insert_iswipe',
'$insert_ordernumber',

'". $payment ."',

'$insert_response',

'". $_SESSION['payme']['gateway']['response']['state'] ."',

'". $_SESSION['payme']['gateway']['response']['state'] ."',

'$insert_message',

'". $_SESSION['payme']['gateway']['response']['id'] ."',

'$insert_maxmind_response',
'$insert_maxmind_result',
'$insert_payvision_transactionId',
'$insert_payvision_transactionGuid',
'$insert_error',
'$insert_avs',
'$insert_code',
'$insert_note',
'$insert_session',
'$insert_chargeback_transactionid',
'$insert_chargeback_reason',
'". $tmp_datetime ."',
'". $tmp_datetime ."',
'$insert_UID',
'$insert_sepa_country',
'$insert_sepa_bank_name',
'$insert_sepa_account_number',
'$insert_sepa_routing_number',
'$insert_sepa_iban',
'$insert_sepa_bic',
'$insert_sepa_bank_city',
'$insert_sepa_firstname',
'$insert_sepa_lastname',
'$insert_sepa_street',
'$insert_sepa_zip',
'$insert_sepa_city',
'". $_SERVER['REMOTE_ADDR'] ."'
);
";
logger3("tmp_sql",$tmp_sql);
$insert_results = insertTHECASHIER($tmp_sql);
logger3("insert_results",$insert_results);



//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
// END: CreatePaymentSample
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////







### END VERRIFIED SESSION
#
#
#
include($_SESSION['template']['company']['file_path'] . "shared/end_verified_session.shared");
?>