/** * Creates Transaction object for Paypal, which is tored in DB * * @param $object * @param $data * @param $gateway * * @return mixed */ public static function create_transaction_object($object, $data, $gateway) { global $psts; if (get_class() !== $gateway) { return $object; } // Basic $object->invoice_number = $data['TRANSACTIONID']; $date = new DateTime($data['TIMESTAMP']); $object->invoice_date = $date->format('Y-m-d'); $object->currency_code = strtoupper($data['CURRENCYCODE']); // Customer $object->username = ''; $object->email = $data['EMAIL']; // get custom field values $custom = isset($_POST['rp_invoice_id']) ? $_POST['rp_invoice_id'] : $_POST['custom']; @(list($pre, $blog_id, $level, $period, $amount, $currency, $timestamp, $activation_key) = explode('_', $custom)); if (empty($blog_id) || $blog_id == 0) { //Get it from Pro sites table, if not there, try to get it from signup table $blog_id = self::blog_id_from_activation_key($activation_key); } $object->blog_id = $blog_id; $object->sub_id = $data['TRANSACTIONID']; // Evidence -> evidence_from_json() try { $object->evidence = ProSites_Helper_Transaction::evidence_from_json($data['evidence_string']); $object->billing_country_code = ProSites_Helper_Transaction::country_code_from_data($data['evidence_string'], $object); $object->tax_country_code = $object->billing_country_code; $object->force_country_code = $object->billing_country_code; $object->buyer_ip = ProSites_Helper_Transaction::country_ip_from_data($data['evidence_string'], $object); } catch (Exception $e) { $object->evidence = null; } $evidence_str_decode_json = !empty($data['evidence_string']) ? json_decode($data['evidence_string']) : ''; //Get tax rate from evidence $tax_rate = !empty($evidence_str_decode_json) && !empty($evidence_str_decode_json->tax_rate) ? $evidence_str_decode_json->tax_rate : 0; //Line Object For Subscription payment $lines = array(); $line_obj = new stdClass(); $line_obj->custom_id = $line_obj->id = $data['PAYERID']; $line_obj->amount = $data['AMT']; $line_obj->quantity = !empty($data['L_QTY0']) ? $data['L_QTY0'] : 1; $line_obj->description = $data['SUBJECT']; $lines[] = $line_obj; //Check if trial is allowed $trial_days = $psts->get_setting('trial_days', 0); if ($trial_days == 0) { $trial_allowed = false; } else { $trial_allowed = true; } //If there is setup fee and trial is allowed if (!empty($data['setup_details'])) { $line_obj_sub = new stdClass(); $line_obj_sub->custom_id = $line_obj_sub->id = $data['setup_details']['TRANSACTIONID']; $line_obj_sub->amount = $data['setup_details']['AMT']; $line_obj_sub->quantity = 1; $line_obj_sub->description = "First month charges"; $lines[] = $line_obj_sub; } //If trial is not allowed, we charge the first payment in init amount $total_amt = !empty($data['setup_details']) && !$trial_allowed ? $data['setup_details']['AMT'] : $data['AMT']; $subtotal = $total_amt / ($tax_rate + 1); $subtotal = !empty($subtotal) ? round($subtotal, 2) : $subtotal; $object->level = $level; $object->period = $period; $object->transaction_lines = $lines; if (!isset($object->buyer_ip)) { $object->buyer_ip = $_SERVER['REMOTE_ADDR']; } //@Todo: Calculate subtotal, tax and other details // General (used for transaction recording) $object->total = $total_amt; $object->tax_percent = $tax_rate; $object->subtotal = $subtotal; // optional $object->tax = !empty($data['TAXAMT']) ? $data['TAXAMT'] : $total_amt - $subtotal; // optional $object->gateway = get_class(); return $object; }
public static function create_transaction_object($object, $data, $gateway) { if (get_class() !== $gateway) { return $object; } // Basic $object->invoice_number = $data->id; $object->invoice_date = date('Y-m-d', $data->date); //$object->original_transaction_key = $data->charge; $object->currency_code = strtoupper($data->currency); // Line Items $lines = array(); $customer_id = ''; $sub_id = ''; foreach ($data->lines->data as $line) { $line_obj = new stdClass(); $line_obj->custom_id = $line_obj->id = $line->id; $line_obj->amount = $line->amount / 100; $line_obj->quantity = $line->quantity; $line_obj->description = isset($line->description) ? $line->description : (isset($line->plan) && isset($line->plan->name) ? $line->plan->name : ''); $lines[] = $line_obj; if (isset($line->customer_id)) { $customer_id = $line->customer_id; } if (isset($line->type) && 'subscription' == $line->type) { $sub_id = $line->id; $object->level = isset($line->metadata->level) ? $line->metadata->level : ''; $object->period = isset($line->metadata->period) ? $line->metadata->period : ''; } } $object->transaction_lines = $lines; // Customer try { $cu = Stripe_Customer::retrieve($customer_id); $object->username = $cu->metadata->user; $object->email = $cu->email; $sub = $cu->subscriptions->retrieve($sub_id); $object->blog_id = $sub->metadata->blog_id; $object->sub_id = $sub_id; } catch (Exception $e) { $error = $e->getMessage(); } // Evidence -> evidence_from_json() if (isset($data->lines->data[0]->metadata->tax_evidence)) { try { $object->evidence = ProSites_Helper_Transaction::evidence_from_json($data->lines->data[0]->metadata->tax_evidence); $object->billing_country_code = ProSites_Helper_Transaction::country_code_from_data($data->lines->data[0]->metadata->tax_evidence, $object); $object->tax_country_code = $object->billing_country_code; $object->force_country_code = $object->billing_country_code; $object->buyer_ip = ProSites_Helper_Transaction::country_ip_from_data($data->lines->data[0]->metadata->tax_evidence, $object); } catch (Exception $e) { $object->evidence = null; } } else { $object->evidence = null; } if (!isset($object->buyer_ip)) { $object->buyer_ip = $_SERVER['REMOTE_ADDR']; } // General (used for transaction recording) $object->total = $data->total / 100; $object->tax_percent = $data->tax_percent / 100; $object->subtotal = $data->subtotal / 100; // optional $object->tax = $data->tax / 100; // optional $object->gateway = get_class(); return $object; }