public function process_payment($order_id) { global $error; global $woocommerce; global $ss_wc_process; // for add_filter on process_payment call $wc_order = wc_get_order($order_id); $grand_total = $wc_order->order_total; if (in_array($this->stripe_storecurrency, $this->stripe_zerodecimalcurrency)) { $amount = number_format($grand_total, 0, ".", ""); } else { $amount = $grand_total * 100; } $card_number = sanitize_text_field(str_replace(' ', '', $_POST['stripe-card-number'])); $cvc = sanitize_text_field($_POST['stripe-card-cvc']); $expiry = sanitize_text_field($_POST['stripe-card-expiry']); $cardtype = $card_number ? $this->get_card_type($card_number) : ''; $current_user = wp_get_current_user(); $customerID = get_post_meta($current_user->ID, 'customer_id', true); if (!$card_number || !$expiry || !$cvc) { // add check for which field, and add that to the message wc_add_notice('Please fill out all credit card fields.', $notice_type = 'error'); return; } if (!in_array($cardtype, $this->stripe_cardtypes)) { wc_add_notice('Merchant does not support accepting in ' . $cardtype, $notice_type = 'error'); // return; return array('result' => 'success', 'redirect' => WC()->cart->get_checkout_url()); die; } try { $exp_date = explode("/", $expiry); $exp_month = str_replace(' ', '', $exp_date[0]); $exp_year = str_replace(' ', '', $exp_date[1]); if (strlen($exp_year) == 2) { $exp_year += 2000; } // create token for customer/buyer credit card $token = \Stripe\Token::create(array("card" => array('number' => $card_number, 'cvc' => $cvc, 'exp_month' => $exp_month, 'exp_year' => $exp_year, 'name' => $wc_order->billing_first_name . ' ' . $wc_order->billing_last_name, 'address_line1' => $wc_order->billing_address_1, 'address_line2' => $wc_order->billing_address_2, 'address_city' => $wc_order->billing_city, 'address_state' => $wc_order->billing_state, 'address_zip' => $wc_order->billing_postcode, 'address_country' => $wc_order->billing_country))); // if create customer option is true, then create customer if no $customerID has been set - else, charge the card now if ($this->stripe_create_customer) { // check if user already has a customer ID // if they dont, create customer if (empty($customerID)) { // Create a Customer $customer = \Stripe\Customer::create(array('email' => $wc_order->billing_email, 'source' => $token, 'description' => $current_user->first_name . ' ' . $current_user->last_name)); // save customer_id for current user meta update_post_meta($current_user->ID, 'customer_id', $customer->id); // set fingerprint of token/card update_post_meta($current_user->ID, 'fingerprint', $token->card->fingerprint); } else { // add check here if card entered is different than whats tied to the customer, and if it is - add it or replace current card? then update customer on stripe with new card(s) // \Stripe\Stripe::setApiKey("sk_test_2DcoV11I0PQl4ygpFUuuQOMa"); // $customer = \Stripe\Customer::retrieve("cus_79WdM0loJvSuyF"); // $card = $customer->sources->retrieve($customer->default_source); // $token = \Stripe\Token::retrieve("tok_16vDXxDSe6V7KL4aZQuvqR8n"); // sp($token->card->fingerprint); // sp($card->fingerprint); // sp($card); // set fingerprint post meta on user from card/token if ($token->card->fingerprint != get_post_meta($current_user->ID, 'fingerprint', true)) { $cu = \Stripe\Customer::retrieve($customerID); // update customer card to the one just entered $cu->source = $token; $cu->save(); update_post_meta($current_user->ID, 'fingerprint', $token->card->fingerprint); } } } else { $charge = \Stripe\Charge::create(array('amount' => $amount, 'currency' => $this->stripe_storecurrency, 'card' => $token, 'capture' => STRIPE_TRANSACTION_MODE, 'statement_descriptor' => 'Order#' . $wc_order->get_order_number(), 'metadata' => array('Order #' => $wc_order->get_order_number(), 'Total Tax' => $wc_order->get_total_tax(), 'Total Shipping' => $wc_order->get_total_shipping(), 'WP customer #' => $wc_order->user_id, 'Billing Email' => $wc_order->billing_email), 'receipt_email' => $wc_order->billing_email, 'description' => get_bloginfo('blogname') . ' Order #' . $wc_order->get_order_number(), 'shipping' => array('address' => array('line1' => $wc_order->shipping_address_1, 'line2' => $wc_order->shipping_address_2, 'city' => $wc_order->shipping_city, 'state' => $wc_order->shipping_state, 'country' => $wc_order->shipping_country, 'postal_code' => $wc_order->shipping_postcode), 'name' => $wc_order->shipping_first_name . ' ' . $wc_order->shipping_last_name, 'phone' => $wc_order->billing_phone))); } // if card valid, and if charge paid, add note of payment completion, empty cart, and redirect to order summary - else error notice if ($token != '') { // if creating a customer, set payment_complete to customerID and allow for filtering, else set to card charge id if ($this->stripe_create_customer) { // hookable filter to set status of order before payment is triggered on woocommerce_order_status_processing $ss_wc_process_payment = apply_filters('ss_process_payment', $ss_wc_process, $order_id); // checking against null values if no add_filter used to hook in // process charges immediately if no filter if ($ss_wc_process_payment || is_null($ss_wc_process_payment)) { $wc_order->payment_complete($customerID); } else { // set to on-hold rather than default pending status $wc_order->update_status('on-hold'); } } else { $wc_order->payment_complete($charge->id); } WC()->cart->empty_cart(); return array('result' => 'success', 'redirect' => $this->get_return_url($wc_order)); } } catch (Exception $e) { $body = $e->getJsonBody(); $error = $body['error']['message']; $wc_order->add_order_note(__('Stripe payment failed due to.' . $error, 'woocommerce')); wc_add_notice($error, $notice_type = 'error'); } }
protected function getTestToken() { return Token::create(['card' => ['number' => '4242424242424242', 'exp_month' => 5, 'exp_year' => 2020, 'cvc' => '123']], ['api_key' => getenv('STRIPE_SECRET')])->id; }
function card_token($data) { $this->set_api_key(); $token = \Stripe\Token::create(array("card" => array("number" => $data['cc']['number'], "exp_month" => $data['cc']['exp']['month'], "exp_year" => $data['cc']['exp']['year'], "cvc" => $data['cc']['cvc']))); return $token; }
function updateCustomer($custem_id) { try { $result = \Stripe\Token::create(array("card" => array("number" => "4242424242424242", "exp_month" => 10, "exp_year" => 2016, "cvc" => "314"))); $cu = \Stripe\Customer::retrieve($custem_id); $cu->source = $result->id; // obtained with Stripe.js $cu->save(); } catch (Exception $e) { $error = $e->getMessage(); echo $error; } }
public function getStripeToken() { Stripe::setApiKey(\Config::get('stripe.key')); try { $stripeToken = StripeToken::create(["card" => ["number" => "4000056655665556", "exp_month" => 12, "exp_year" => 2016, "cvc" => "314", "currency" => "usd"]]); return $stripeToken->id; } catch (StripeError\Base $e) { return $this->sendBackJsonError($e->getHttpStatus()); } }
/** * create token by using card information, you can also create via stripe.js * * @param array array of credit/debit card information * @return response in json format */ function createToken($myCard) { $this->setApiKey(); // below code for create a token $token = \Stripe\Token::create(array("card" => $myCard)); return $token; }
public function testUserToCustomerConvertion() { $mock = $this->getMockForTrait('Biller\\Behavior\\IsCustomer'); $mock->id = 1; $mock->email = '*****@*****.**'; $token = \Stripe\Token::create(['card' => ['number' => '4242424242424242', 'exp_month' => 5, 'exp_year' => date('Y') + 3, 'cvc' => '314']]); $this->assertInstanceOf('Stripe\\Customer', static::$mock->toCustomer($token->id)); }
/** * {@inheritDoc} */ public function execute($request) { /** @var $request CreateToken */ RequestNotSupportedException::assertSupports($this, $request); $model = ArrayObject::ensureArrayObject($request->getModel()); try { Stripe::setApiKey($this->keys->getSecretKey()); $token = Token::create($model->toUnsafeArrayWithoutLocal()); $model->replace($token->__toArray(true)); } catch (Error\Base $e) { $model->replace($e->getJsonBody()); } }