function do_process() { global $ym_sys; $mode = $this->status == 'test' ? TRUE : FALSE; $gc = new GoCardless($this->merchant_id, $this->application_id, $this->application_secret, $this->access_token, $mode, $this->magical_word); if (!$gc->valid) { echo 'An Error Occured. Please contact Site Admin: Invalid Keys'; exit; } $action = ym_request('action'); if ($action == 'go') { // redirecting to gocardless $pack = $_POST; if (isset($pack['num_cycles']) && $pack['num_cycles'] != 1) { // subscription // convert pack values to something gocardless can understand switch ($pack['duration_type']) { case 'y': // convert to month $interval_unit = 'month'; $interval_length = $pack['duration'] * 12; $expire_length = $interval_length * $pack['num_cycles']; $expire = mktime(23, 59, 59, date('n', time()) + $expire_length, date('j', time()), date('Y', time())); break; case 'm': $interval_unit = 'month'; $interval_length = $pack['duration']; $expire_length = $interval_length * $pack['num_cycles']; $expire = mktime(23, 59, 59, date('n', time()) + $expire_length, date('j', time()), date('Y', time())); break; case 'd': $interval_unit = 'day'; $interval_length = $pack['duration']; $expire_length = $interval_length * $pack['num_cycles']; $expire = mktime(23, 59, 59, date('n', time()), date('j', time()) + $expire_length, date('Y', time())); } $subscription = array('amount' => number_format($pack['cost'], 2), 'interval_length' => $interval_length, 'interval_unit' => $interval_unit, 'merchant_id' => $this->merchant_id, 'name' => get_bloginfo() . ' ' . __('Subscription', 'ym'), 'description' => isset($pack['item_name']) && $pack['item_name'] ? $pack['item_name'] : $ym_sys->item_name); if ($subscription['name'] == $subscription['description']) { unset($subscription['description']); } if ($pack['num_cycles'] > 1) { $subscription['expire'] = date('c', $expire); } } else { // single purchase // bill $bill = array('amount' => number_format($pack['cost'], 2), 'merchant_id' => $this->merchant_id, 'name' => get_bloginfo() . ' ' . __('Purchase', 'ym'), 'description' => isset($pack['item_name']) && $pack['item_name'] ? $pack['item_name'] : $ym_sys->item_name); } $data = array(); // common fields $data['redirect_uri'] = site_url('?ym_process=' . $this->code . '&action=confirm'); $data['cancel_uri'] = site_url($this->cancel_url); // state AKA custom if (isset($pack['ppp_pack_id'])) { $data['state'] = 'buy_bundle_' . $pack['ppp_pack_id'] . '_' . $pack['user_id']; } else { if (isset($pack['post_id'])) { $data['state'] = 'buy_post_' . ($pack['post_id'] ? $pack['post_id'] : get_the_ID()) . '_' . $pack['user_id']; } else { $data['state'] = 'buy_subscription_' . $pack['id'] . '_' . $pack['user_id']; } } // user fields $user = array(); if ($first = get_user_meta($pack['user_id'], 'first_name', true)) { $user['first_name'] = $first; } if ($last = get_user_meta($pack['user_id'], 'last_name', true)) { $user['last_name'] = $last; } $user['email'] = get_user_by('id', $pack['user_id']); $user['email'] = $user['email']->user_email; // generate and go to URL if (isset($bill)) { $gc->NewPayment($bill, $data, $user); } else { echo 'sub'; $gc->NewSubscription($subscription, $data, $user); } exit; } if ($action == 'confirm') { // perform confirm and redirect $state = ym_get('state'); if (!$state) { header('HTTP/1.1 400 Bad Request'); echo 'Missing State'; exit; } $r = $gc->catchReturn(); if ($r) { // update the user and set then to pending or grace // cost is 0 as no money yet // deny receipt email $this->nomore_email = TRUE; // process $this->common_process($state, '0', FALSE, FALSE); // technically true and Don't exit list($buy, $what, $id, $user_id) = explode('_', $state); // we need to store the bill/subscription ID in order to track the user // state is not returned with webhooks $key = ym_get('resource_id'); $data = array('state' => $state, 'user_id' => $user_id, 'amount' => ym_get('amount')); update_option('ym_gocardless_states_' . $key, $data); if ($what == 'post') { $pack = array('ppp' => 1, 'post_id' => $id); } else { if ($what == 'bundle') { $pack = array('ppp' => 1, 'ppp_pack_id' => $id); } else { // subscriptiom update_user_meta('ym_gocardless_active_subscription', $key, $user_id); $pack = $id; } } $this->redirectlogic($pack, TRUE); } else { echo 'An Error Occured, you should contact the Site Admin'; exit; } } // assume webhook $data = $gc->catchWebHook(); if (!$data) { header('HTTP/1.1 403 Unauthorised'); echo 'Signature Invalid'; exit; } else { // post or sub? // status // created failed paid cancelled expired withdrawn // abort cases // widthdrawn jsut means money has moved from the GC account to the merchant account. $aborts = array('created', 'withdrawn'); if (in_array($data['action'], $aborts)) { // ignore created packets header('HTTP/1.1 200 OK'); echo 'ok'; exit; } $success_states = array('paid'); $failed_states = array('failed', 'cancelled', 'expired'); foreach ($data['resources'] as $packet) { $id = $packet->id; $status = $packet->status; $uri = $packet->uri; $source_type = isset($packet->source_type) ? $packet->source_type : ''; if ($source_type == 'subscription') { $id = $packet->source_id; } $state_data = get_option('ym_gocardless_states_' . $id, FALSE); if ($state_data) { // packet found $state = $state_data['state']; $user_id = $state_data['user_id']; $amount = $state_data['amount']; // store for trans log $_POST = $state_data; $complete = FALSE; if (in_array($status, $success_states)) { $complete = TRUE; } $this->common_process($state, $amount, $complete, FALSE); } else { $admin = get_userdata(1); $admin_email = $admin->user_email; ym_email($admin_email, 'GC PAYLOAD STATE FAIL', print_r($packet, TRUE)); } } exit; } }