forked from tripflex/whmcs-purchaseorder
/
purchaseorder.php
155 lines (116 loc) · 4.9 KB
/
purchaseorder.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
<?php
/**
* @title WHMCS Purchase Order Gateway
*
* @author Myles McNamara (get@smyl.es)
* @copyright Copyright (c) Myles McNamara 2014
* @date 8/13/14
* @link http://smyl.es
*/
function purchaseorder_config() {
$configarray = array(
"FriendlyName" => array(
"Type" => "PO",
"Value" => "Purchase Order"
),
"approvedforpo" => array(
"FriendlyName" => "Approved for PO Message",
"Type" => "textarea",
"Rows" => "5",
"Value" => "<strong style=\"color: #008000;\">Verified Account</strong><br />Invoice will be marked as paid when payment is verified for the PO.",
"Description" => "",
),
"notapprovedforpo" => array(
"FriendlyName" => "Not Approved for PO Message",
"Type" => "textarea",
"Rows" => "5",
"Value" => "<strong style=\"color: #BA3832;\">Invalid Account</strong><br />You are not authorized to pay via Purchase Order, please contact support, or choose another payment method.",
"Description" => "",
),
"verifiedaccountfield" => array(
"FriendlyName" => "Approved for PO Client Custom Field",
"Type" => "dropdown",
"Description" => "Select the custom field used to Verify/Approve a client to use Purchase Orders.",
"Options" => purchaseorder_get_custom_fields()
),
"enableautoactivatefield" => array(
"FriendlyName" => "Enable Auto Activate",
"Type" => "yesno"
),
"autoactivatefield" => array(
"FriendlyName" => "Auto Activate Client Custom Field",
"Type" => "dropdown",
"Description" => "Select the custom client field used for auto activation",
"Options" => purchaseorder_get_custom_fields()
)
);
return $configarray;
}
function purchaseorder_get_custom_fields() {
$options = '';
$request = full_query( "SELECT GROUP_CONCAT(fieldname) FROM tblcustomfields where type='client' GROUP BY type" );
$values = mysql_fetch_array( $request );
return $values[ 0 ];
}
function purchaseorder_accept_order( $orderid ) {
$command = "acceptorder";
$values[ "orderid" ] = $orderid;
$values[ "autosetup" ] = TRUE;
$values[ "sendregistrar" ] = TRUE;
$values[ "sendemail" ] = TRUE;
$results = localAPI( $command, $values );
logModuleCall( 'purchaseorder', 'activate', $values, $results );
}
// Output below cart buttons on review and checkout
function purchaseorder_orderformoutput( $params ) {
return FALSE;
}
// Gateway response array with status, rawdata, etc
function purchaseorder_orderformcheckout( $params ) {
return FALSE;
}
function purchaseorder_link( $params ) {
global $CONFIG;
$code = "";
$cart_action = filter_input( INPUT_GET, 'a', FILTER_SANITIZE_STRING );
$actual_auto_activate_value = array();
$actual_verified_account_value = array();
$sysurl = ( $CONFIG[ 'SystemSSLURL' ] ? $CONFIG[ 'SystemSSLURL' ] : $CONFIG[ 'SystemURL' ] );
$invoiceid = $params[ 'invoiceid' ];
if ( $params[ 'enableautoactivatefield' ] == 'on' && ! empty( $params[ 'autoactivatefield' ] ) ) {
$auto_activate_field_label = $params[ 'autoactivatefield' ];
$auto_activate_query = select_query( 'tblcustomfields', 'id', array( 'fieldname' => $auto_activate_field_label ) );
$auto_activate_id = mysql_fetch_array( $auto_activate_query );
}
if ( ! empty( $params[ 'verifiedaccountfield' ] ) ) {
$verified_account_field_label = $params[ 'verifiedaccountfield' ];
$verified_account_query = select_query( 'tblcustomfields', 'id', array( 'fieldname' => $verified_account_field_label ) );
$verified_account_id = mysql_fetch_array( $verified_account_query );
}
// echo "AUTO ACTIVATE ID =" . $auto_activate_id;
// echo "VERIFIED ACCOUNT ID =" . $verified_account_id;
foreach ( $params[ 'clientdetails' ][ 'customfields' ] as $customfield ) {
if ( $customfield[ 'id' ] == $auto_activate_id[ 0 ] ) {
$actual_auto_activate_value = $customfield[ 'value' ];
}
if ( $customfield[ 'id' ] == $verified_account_id[ 0 ] ) {
$actual_verified_account_value = $customfield[ 'value' ];
}
}
$orderid_req = select_query( 'tblorders', 'id', array( 'invoiceid' => $invoiceid ) );
$orderid_data = mysql_fetch_array( $orderid_req );
if ( ! empty ( $orderid_data[ 'id' ] ) ) $orderid = $orderid_data[ 'id' ];
// Make sure you set the custom field value above
if ( $actual_verified_account_value == 'on' ) {
$code .= html_entity_decode( $params[ 'approvedforpo' ] );
} else {
$code .= html_entity_decode( $params[ 'notapprovedforpo' ] );
}
if ( $actual_auto_activate_value == 'on' && ! empty( $orderid ) && $cart_action != 'complete' ) {
purchaseorder_accept_order( $orderid );
logModuleCall( 'autoactivate', 'activate', 'accepting order...', $orderid );
}
// Required to redirect after checking out
$code .= "<form method=\"POST\" action=\"{$sysurl}/viewinvoice.php?id={$invoiceid}\">";
return $code;
}