|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * HostBill example cc processing module with Card tokenization support |
| 5 | + * Also known as Offsite Credit Cards processing (i.e. AuthorizeNet CIM) |
| 6 | + * |
| 7 | + * How tokenization works? |
| 8 | +- Client enters his credit card details in your clientarea during purchase |
| 9 | +- HostBill send those details to gateway (using capture_token), to capture payment for due invoice |
| 10 | +- In return gateway provides re-usable token, that represents related credit card stored in gateway secure environment |
| 11 | +- HostBill removes credit card data from database, leaving last 4 digits and token safely encrypted in local DB |
| 12 | +- On next purchases/recurring charges for that customer HostBill will pass this token into gateway instead of full credit card details |
| 13 | + * |
| 14 | + * @see http://dev.hostbillapp.com/dev-kit/payment-gateway-modules/merchant-gateways-modules/ |
| 15 | + * |
| 16 | + * 2012 HostBill - Complete Client Management, Support and Billing Software |
| 17 | + * http://hostbillapp.com |
| 18 | + */ |
| 19 | + |
| 20 | +class Merchant_Token_Sample extends TokenPaymentModule { |
| 21 | + |
| 22 | + /** |
| 23 | + * Default module name to be displayed in adminarea |
| 24 | + */ |
| 25 | + protected $modname = 'Sample Merchant Gateway Module with Token support.'; |
| 26 | + /** |
| 27 | + * Description to be displayed in admianrea |
| 28 | + */ |
| 29 | + protected $description = 'Credit Card Token'; |
| 30 | + |
| 31 | + /** |
| 32 | + * List of currencies supported by gateway - if module supports all currencies - leave empty |
| 33 | + * @var array |
| 34 | + */ |
| 35 | + protected $supportedCurrencies = array('USD', 'CAD', 'EUR', 'GBP'); |
| 36 | + |
| 37 | + /** |
| 38 | + * Configuration array - types allowed: check, input, select |
| 39 | + */ |
| 40 | + protected $configuration = array( |
| 41 | + 'API Login' => array( |
| 42 | + 'value' => '', |
| 43 | + 'type' => 'input' |
| 44 | + ), |
| 45 | + 'Transaction Key' => array( |
| 46 | + 'value' => '', |
| 47 | + 'type' => 'input' |
| 48 | + ), |
| 49 | + 'MD5 Hash' => array( |
| 50 | + 'value' => '', |
| 51 | + 'type' => 'input' |
| 52 | + ), |
| 53 | + 'Enable Test Mode' => array( |
| 54 | + 'value' => '1', |
| 55 | + 'type' => 'check' |
| 56 | + ) |
| 57 | + ); |
| 58 | + |
| 59 | + /** |
| 60 | + * HostBill will call this method to attempt to charge/capture payment from credit card |
| 61 | + * |
| 62 | + * @param array $ccdetails An array with credit card details, contains following keys: |
| 63 | + * $ccdetails['cardnum'] - credit card number |
| 64 | + * $ccdetails['expdate'] - expiration date in format MMYY - i.e. 1112 |
| 65 | + * $ccdetails['cardtype'] - CC type, ie. 'Visa' |
| 66 | + * If CVV is passed it will be available under: |
| 67 | + * $ccdetails['cvv'] |
| 68 | + * @return boolean True if card was charged |
| 69 | + */ |
| 70 | + public function capture($ccdetails) { |
| 71 | + |
| 72 | + |
| 73 | + $options['x_login'] = $this->configuration['API Login']['value']; |
| 74 | + $options['x_tran_key'] = $this->configuration['Transaction Key']['value']; |
| 75 | + |
| 76 | + |
| 77 | + /* CUSTOMER INFORMATION */ |
| 78 | + $options['x_first_name'] = $this->client['firstname']; |
| 79 | + $options['x_last_name'] = $this->client['lastname']; |
| 80 | + $options['x_address'] = $this->client['address1']; |
| 81 | + $options['x_city'] = $this->client['city']; |
| 82 | + $options['x_state'] = $this->client['state']; |
| 83 | + $options['x_zip'] = $this->client['postcode']; |
| 84 | + $options['x_country'] = $this->client['country']; |
| 85 | + $options['x_phone'] = $this->client['phonenumber']; |
| 86 | + $options['x_email'] = $this->client['email']; |
| 87 | + $options['x_cust_id'] = $this->client['client_id']; |
| 88 | + |
| 89 | + |
| 90 | + /* ORDER INFORMATION */ |
| 91 | + $options['x_invoice_num'] = $this->invoice_id; |
| 92 | + $options['x_description'] = $this->subject; |
| 93 | + $options['x_amount'] = $this->amount; |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | + |
| 98 | + /* CREDIT CARD INFORMATION */ |
| 99 | + // we have token available, use it against registrar |
| 100 | + if ($ccdetails['token']) { |
| 101 | + $options['x_card_token'] = $ccdetails['cardnum']; |
| 102 | + } else { |
| 103 | + $options['x_card_num'] = $ccdetails['cardnum']; |
| 104 | + $options['x_exp_date'] = $ccdetails['expdate']; //MMYY |
| 105 | + if($ccdetails['cvv']) { |
| 106 | + //this is manual payment, client passed cvv code |
| 107 | + $options['x_card_code'] = $ccdetails['cvv']; |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + // |
| 116 | + //SEND details to your credit card processor to validate and attempt to charge |
| 117 | + // |
| 118 | + $response = $this->processData($options); |
| 119 | + |
| 120 | + switch ($response['code']) { |
| 121 | + case 1: |
| 122 | + //charge succeeded, add transaction and log it |
| 123 | + |
| 124 | + $this->logActivity(array( |
| 125 | + 'output' => $response, |
| 126 | + 'result' => PaymentModule::PAYMENT_SUCCESS |
| 127 | + )); |
| 128 | + |
| 129 | + |
| 130 | + $this->addTransaction(array( |
| 131 | + 'client_id' => $this->client['client_id'], |
| 132 | + 'invoice_id' => $this->invoice_id, |
| 133 | + 'description' => "Payment for invoice ".$this->invoice_id, |
| 134 | + 'number' => $response['Transaction ID'], |
| 135 | + 'in' => $this->amount, |
| 136 | + 'fee' => '0' |
| 137 | + )); |
| 138 | + |
| 139 | + if($response['Token']) { |
| 140 | + return $response['Token']; //return token to be stored |
| 141 | + } else { |
| 142 | + return true; //capture success |
| 143 | + } |
| 144 | + |
| 145 | + break; |
| 146 | + |
| 147 | + case 2: |
| 148 | + |
| 149 | + $this->logActivity(array( |
| 150 | + 'output' => $response, |
| 151 | + 'result' => PaymentModule::PAYMENT_FAILURE |
| 152 | + )); |
| 153 | + return false; |
| 154 | + |
| 155 | + break; |
| 156 | + |
| 157 | + |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * This OPTIONAL helper function can be called from capture method, |
| 163 | + * i.e. connect to gateway API using CURL |
| 164 | + */ |
| 165 | + private function processData($options) { |
| 166 | + //send data to cc processor, parse response |
| 167 | + } |
| 168 | + |
| 169 | +} |
| 170 | + |
0 commit comments