| 1 |
<?php |
| 2 |
|
| 3 |
namespace WPFunnels\Gateway; |
| 4 |
|
| 5 |
use OfficeGuyPayment; |
| 6 |
use OfficeGuyAPI; |
| 7 |
|
| 8 |
class Wpfnl_Gateway_OfficeGuy { |
| 9 |
|
| 10 |
|
| 11 |
function __construct() |
| 12 |
{ |
| 13 |
add_action('og_payment_request_handle', array($this, 'store_sigle_use_token'), 10 ,2); |
| 14 |
} |
| 15 |
|
| 16 |
|
| 17 |
/** |
| 18 |
* Store token into meta post |
| 19 |
* |
| 20 |
* @param mixed $Order |
| 21 |
* @param mixed $Request |
| 22 |
* |
| 23 |
* @return void |
| 24 |
* @since 1.0.0 |
| 25 |
*/ |
| 26 |
public function store_sigle_use_token($order, $Request) |
| 27 |
{ |
| 28 |
$token = sanitize_text_field( $Request['SingleUseToken'] ); |
| 29 |
$order->update_meta_data('SingleUseToken', $token ); |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
/** |
| 35 |
* Process the offer payment |
| 36 |
* |
| 37 |
* @param mixed $order |
| 38 |
* @param mixed $offer_product |
| 39 |
* |
| 40 |
* @return bool |
| 41 |
* @since 1.0.0 |
| 42 |
*/ |
| 43 |
public function process_payment($order, $offer_product) |
| 44 |
{ |
| 45 |
$result = array( |
| 46 |
'is_success' => false, |
| 47 |
'message' => '' |
| 48 |
); |
| 49 |
$Request = $this->PreapreOfferProductPaymentRequest($order, $offer_product); |
| 50 |
$Gateway = GetOfficeGuyGateway(); |
| 51 |
$IsOfficeGuySubscription = get_post_meta($offer_product['id'], 'OfficeGuySubscription', true) === 'yes'; |
| 52 |
if ($IsOfficeGuySubscription) |
| 53 |
$Response = OfficeGuyAPI::Post($Request, '/billing/recurring/charge/', $Gateway->settings['environment'], true); |
| 54 |
else |
| 55 |
$Response = OfficeGuyAPI::Post($Request, '/billing/payments/charge/', $Gateway->settings['environment'], true); |
| 56 |
|
| 57 |
if ( ! is_wp_error( $Response ) ) { |
| 58 |
if ( $Response['Status'] == 0 ) { |
| 59 |
$result['is_success'] = true; |
| 60 |
$result['message'] = "Payment Successful"; |
| 61 |
} else { |
| 62 |
$result['is_success'] = false; |
| 63 |
$result['message'] = $Response['UserErrorMessage']; |
| 64 |
} |
| 65 |
} |
| 66 |
return $result; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
/** |
| 71 |
* Prepare an array for SUMIT payment charge API request |
| 72 |
* |
| 73 |
* @param mixed $order |
| 74 |
* @param mixed $product |
| 75 |
* |
| 76 |
* @return array |
| 77 |
* @since 1.0.0 |
| 78 |
*/ |
| 79 |
private function PreapreOfferProductPaymentRequest($order, $product) |
| 80 |
{ |
| 81 |
$Gateway = GetOfficeGuyGateway(); |
| 82 |
$Request = array(); |
| 83 |
$Request['Credentials'] = OfficeGuyPayment::GetCredentials($Gateway); |
| 84 |
$Request['SingleUseToken'] = $order->get_meta('SingleUseToken'); |
| 85 |
|
| 86 |
$Request['Items'] = array(); |
| 87 |
$Item = OfficeGuyPayment::GetPaymentOrderItem(null, $product['id'], round($product['price'], 2), 1, $order->get_currency(), null, null, $order); |
| 88 |
array_push($Request['Items'], $Item); |
| 89 |
|
| 90 |
$Request['VATIncluded'] = 'true'; |
| 91 |
$Request['VATRate'] = OfficeGuyPayment::GetOrderVatRate($order); |
| 92 |
$Request['Customer'] = array(); |
| 93 |
$Request['Customer']['ID'] = $order->get_meta("OfficeGuyCustomerID"); |
| 94 |
$Request['Customer']['Name'] = $this->getCustomerFullName($order); |
| 95 |
$Request['Customer']['EmailAddress'] = $order->data['billing']['email']; |
| 96 |
|
| 97 |
$Request['AuthoriseOnly'] = $Gateway->settings['testing'] != 'no' ? 'true' : 'false'; |
| 98 |
$Request['DraftDocument'] = $Gateway->settings['draftdocument'] != 'no' ? 'true' : 'false'; |
| 99 |
$Request['SendDocumentByEmail'] = $Gateway->settings['emaildocument'] == 'yes' ? 'true' : 'false'; |
| 100 |
$Request['UpdateCustomerByEmail'] = 'false'; |
| 101 |
$Request['DocumentDescription'] = __('Order number', 'officeguy') . ': ' . $order->get_id(); |
| 102 |
$Request['DocumentLanguage'] = OfficeGuyPayment::GetOrderLanguage($Gateway); |
| 103 |
$Request['MerchantNumber'] = $Gateway->settings['merchantnumber']; |
| 104 |
|
| 105 |
return $Request; |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
/** |
| 110 |
* Return customer full name |
| 111 |
* |
| 112 |
* @param mixed $order |
| 113 |
* |
| 114 |
* @return string |
| 115 |
* @since 1.0.0 |
| 116 |
*/ |
| 117 |
private function getCustomerFullName($order) |
| 118 |
{ |
| 119 |
return $order->data['billing']['first_name'] . ' ' . $order->data['billing']['last_name']; |
| 120 |
} |
| 121 |
} |