/** * Stripe TLS requirement. * https://support.stripe.com/questions/how-do-i-upgrade-my-stripe-integration-from-tls-1-0-to-tls-1-2 */ private static function stripe_tls_check($for_export) { global $sc_options; // Set Stripe API key. Force test key. Stripe_Checkout_Functions::set_key('true'); $test_key = $sc_options->get_setting_value('test_secret_key'); // If test key isn't set... if (empty($test_key)) { if ($for_export) { return __('Cannot test TLS 1.2 support until your Stripe Test Secret Key is entered.', 'stripe'); } else { return '<mark class="error">' . __('Cannot test TLS 1.2 support until your Stripe Test Secret Key is entered.', 'stripe') . '</mark>'; } } \Stripe\Stripe::$apiBase = 'https://api-tls12.stripe.com'; try { \Stripe\Charge::all(); if ($for_export) { return __('TLS 1.2 supported, no action required.', 'stripe'); } else { return '<mark class="ok">' . __('TLS 1.2 supported, no action required.', 'stripe') . '</mark>'; } } catch (\Stripe\Error\ApiConnection $e) { if ($for_export) { return sprintf(__('TLS 1.2 is not supported. You will need to upgrade your integration. See %1$s.', 'stripe'), 'https://stripe.com/blog/upgrading-tls'); } else { return '<mark class="error">' . sprintf(__('TLS 1.2 is not supported. You will need to upgrade your integration. <a href="%1$s">Please read this</a> for more information.', 'stripe'), 'https://stripe.com/blog/upgrading-tls') . '</mark>'; } } }
/** * [listCharges Returns a list of charges you've previously created. The charges are * returned in sorted order, with the most recent charges appearing first.] * * @param array $options [ 'created' A filter on the list based on the object created field. The value can be a * string with an integer Unix timestamp, or * it can be a dictionary with the following options: * child arguments * 'gt' where the created field is after this timestamp. * 'gte' where the created field is after or equal to this timestamp. * 'lt' where the created field is before this timestamp. * 'lte' where the created field is before or equal to this timestamp. * * 'customer' * * 'ending_before' is an object ID that defines your place in the list. For instance, if you * make a list request and receive 100 objects, starting with obj_bar, your * subsequent call can include ending_before=obj_bar in order to fetch the * previous page of the list. * * 'limit' default is 10, Limit can range between 1 and 100 items. * * 'starting_after' starting_after is an object ID that defines your place in the list. For instance, * if you make a list request and receive 100 objects, ending with obj_foo, your subsequent * call can include starting_after=obj_foo in order to fetch the next page of the list.] * * @return [A associative array with a data property that contains an array * of up to limit charges, starting after charge starting_after.] */ public function listCharges($options = []) { return \Stripe\Charge::all($options); }
<?php \Stripe\Stripe::setApiKey("sk_your_api_key"); $charges = \Stripe\Charge::all(array("limit" => 100)); foreach ($charges as $charge) { echo $charge->id; } while ($charges->has_more) { $charges = \Stripe\Charge::all(array("limit" => 100, "starting_after" => end($charges->data->id))); foreach ($charges as $charge) { echo $charge->id; } }
/** * Gets all charges for a customer. * * @return array */ public function charges() { $this->info(); if (!$this->stripe_customer) { return array(); } $charges = Stripe_Charge::all(array('customer' => $this->id, 'limit' => 100)); $charges_array = array(); foreach ($charges->data as $charge) { $charges_array[] = $this->charge($charge); } return $charges_array; }
public function submitOrder($short_name) { $university = $this->university->where('short_name', $short_name)->first(); $firstName = $this->request->input('first_name', ''); $lastName = $this->request->input('last_name', ''); $emainAddress = $this->request->input('email_address', ''); $customer = $this->customer->where('first_name', $firstName)->where('last_name', $lastName)->where('email_address', $emainAddress)->first(); $customerId; if ($customer === null) { //This customer is new $this->customer->fill($this->request->all())->save(); $customerId = $this->customer->id; } else { $this->customer->find($customer->id)->fill($this->request->all())->save(); $customerId = $customer->id; } $totalPrice = $this->request->input('totalPrice', ''); $orderId = $this->order->insertGetId(['customer_id' => $customerId, 'payment' => $this->request->input('payment', ''), 'order_status' => "initialized", 'street_address1' => $this->request->input('street_address1', ''), 'street_address2' => $this->request->input('street_address2', ''), 'city' => $this->request->input('city', ''), 'post_code' => $this->request->input('post_code', ''), 'country' => $this->request->input('country', ''), 'created_at' => \Carbon\Carbon::now()->toDateTimeString(), 'total_price' => $totalPrice]); $itemsInCart = $this->cart->where('customer_id', $this->request->cookie('customer_id'))->get(); foreach ($itemsInCart as $item) { $this->orderitem->insert(['order_id' => $orderId, 'product_id' => $item->product_id, 'created_at' => \Carbon\Carbon::now()->toDateTimeString(), 'price' => $item->price]); $cart = $this->cart->find($item->id); $cart->order_id = $orderId; $cart->save(); } $temporaryCustomerId = $this->request->cookie('customer_id'); $allProductsInCart = $this->getAllProductsInCart($temporaryCustomerId); $allProductsInCart = $this->convertProductsForCheckout($allProductsInCart); \Stripe\Stripe::setApiKey("sk_test_I561ILtdEXsIVDoAc9169tcC"); $transactionInformation = \Stripe\Charge::all(); $transactionId = $transactionInformation->data[0]->id; $token = $this->request->input('stripeToken', ''); $tokenInformation = \Stripe\Token::retrieve($token); $chargeInformation = \Stripe\Charge::retrieve($transactionId); //dd($transactionInformation); try { $charge = \Stripe\Charge::create(array("amount" => bcmul($totalPrice, 100), "currency" => "aud", "source" => $token, "description" => "Example charge")); $type = $tokenInformation->type; $clientIp = $tokenInformation->client_ip; $createdTime = $chargeInformation->created; $country = $chargeInformation->source->country; $expMonth = $chargeInformation->source->exp_month; $expYear = $chargeInformation->source->exp_year; $brand = $chargeInformation->source->brand; $last4CardNumber = $chargeInformation->source->last4; $paymentResponse = json_encode(array('type' => $type, 'client_ip' => $clientIp, 'created' => $createdTime, 'country' => $country, 'expMonth' => $expMonth, 'expYear' => $expYear, 'brand' => $brand, 'last4CardNumber' => $last4CardNumber)); $transactionId = \Stripe\Charge::all()->data[0]->id; $order = $this->order->find($orderId); $order->transaction_id = $transactionId; $order->order_status = "paid"; $order->payment_response = $paymentResponse; $order->save(); $shippingFee = $this->request->input('shipping_fee', ''); $this->sendConfirmationMail($customerId, $allProductsInCart, $totalPrice, $shippingFee); foreach ($allProductsInCart as $productInCart) { $product = $this->product->find($productInCart['id']); $product->stock -= $productInCart['quantity']; $product->save(); if ($product->stock < 0) { $this->sendStockShortageNotification($product, $orderId); $order->order_status = "out_of_stock"; $order->save(); } } foreach ($itemsInCart as $item) { $this->cart->destroy($item->id); } return view('www.orderSubmitted', ['template' => $university->template, 'basket' => $this->getTheNumberOfItems(), 'products' => $allProductsInCart, 'totalPrice' => $totalPrice, 'shippingFee' => $shippingFee, 'headerLinks' => $this->headerPages, 'footerLinks' => $this->footerPages]); } catch (\Stripe\Error\Card $e) { $type = $tokenInformation->type; $clientIp = $tokenInformation->client_ip; $createdTime = $chargeInformation->created; $country = $chargeInformation->source->country; $expMonth = $chargeInformation->source->exp_month; $expYear = $chargeInformation->source->exp_year; $brand = $chargeInformation->source->brand; $last4CardNumber = $chargeInformation->source->last4; $failureCode = $chargeInformation->failure_code; $failureMessage = $chargeInformation->failure_message; $paymentResponse = json_encode(array('type' => $type, 'client_ip' => $clientIp, 'created' => $createdTime, 'country' => $country, 'expMonth' => $expMonth, 'expYear' => $expYear, 'brand' => $brand, 'last4CardNumber' => $last4CardNumber, 'failureCode' => $failureCode, 'failureMessage' => $failureMessage)); $order = $this->order->find($orderId); $order->order_status = "failed"; $order->payment_response = $paymentResponse; $order->save(); include 'countries.php'; $isShipping = false; foreach ($allProductsInCart as $productInCart) { if ($productInCart['is_shipping'] === "Yes" && $isShipping === false) { $isShipping = true; } } $domesticShippingFee = $this->setting->where('key', 'domesticShippingFee')->first(); $domesticShippingFee = json_decode($domesticShippingFee->json)->data; $overseasShippingFee = $this->setting->where('key', 'overseasShippingFee')->first(); $overseasShippingFee = json_decode($overseasShippingFee->json)->data; $message = "Unfortunately, the submission has been failed :" . $failureCode; return view('www.checkout', ['template' => $university->template, 'universityShortName' => $short_name, 'basket' => $this->getTheNumberOfItems(), 'products' => $allProductsInCart, 'totalPrice' => $totalPrice, 'countries' => $countries, 'is_shipping' => $isShipping, 'domesticShippingFee' => $domesticShippingFee, 'overseasShippingFee' => $overseasShippingFee, 'message' => $message, 'headerLinks' => $this->headerPages, 'footerLinks' => $this->footerPages]); } }
/** * request method * * @param string $method * @param array $data * * @return array - containing 'status', 'message' and 'data' keys * if response was successful, keys will be 'success', 'Success' and the stripe response as associated array respectively, * if request failed, keys will be 'error', the card error message if it was card_error, boolen false otherwise, and * error data as an array respectively */ private function request($method = null, $data = null) { if (!$method) { throw new Exception(__('Request method is missing')); } if (is_null($data)) { throw new Exception(__('Request Data is not provided')); } Stripe::setApiKey($this->key); $success = null; $error = null; $message = false; $log = null; try { switch ($method) { /** * * CHARGES * */ case 'charge': $success = $this->fetch(Charge::create($data)); break; case 'retrieveCharge': $success = $this->fetch(Charge::retrieve($data['charge_id'])); if (!empty($success['refunds'])) { foreach ($success['refunds'] as &$refund) { $refund = $this->fetch($refund); } } break; case 'updateCharge': $charge = Charge::retrieve($data['charge_id']); foreach ($data['fields'] as $field => $value) { $charge->{$field} = $value; } $success = $this->fetch($charge->save()); break; case 'refundCharge': $charge = Charge::retrieve($data['charge_id']); // to prevent unknown param error unset($data['charge_id']); $success = $this->fetch($charge->refund($data)); foreach ($success['refunds']['data'] as &$refund) { $refund = $this->fetch($refund); } break; case 'captureCharge': $charge = Charge::retrieve($data['charge_id']); unset($data['charge_id']); $success = $this->fetch($charge->capture($data)); if (!empty($success['refunds']['data'])) { foreach ($success['refunds']['data'] as &$refund) { $refund = $this->fetch($refund); } } break; case 'listCharges': $charges = Charge::all(); $success = $this->fetch($charges); foreach ($success['data'] as &$charge) { $charge = $this->fetch($charge); if (isset($charge['refunds']['data']) && !empty($charge['refunds']['data'])) { foreach ($charge['refunds']['data'] as &$refund) { $refund = $this->fetch($refund); } unset($refund); } } break; /** * CUSTOMERS */ /** * CUSTOMERS */ case 'createCustomer': $customer = Customer::create($data); $success = $this->fetch($customer); if (!empty($success['cards']['data'])) { foreach ($success['cards']['data'] as &$card) { $card = $this->fetch($card); } unset($card); } if (!empty($success['subscriptions']['data'])) { foreach ($success['subscriptions']['data'] as &$subscription) { $subscription = $this->fetch($subscription); } unset($subscription); } break; case 'retrieveCustomer': $customer = Customer::retrieve($data['customer_id']); $success = $this->fetch($customer); if (!empty($success['cards']['data'])) { foreach ($success['cards']['data'] as &$card) { $card = $this->fetch($card); } unset($card); } if (!empty($success['subscriptions']['data'])) { foreach ($success['subscriptions']['data'] as &$subscription) { $subscription = $this->fetch($subscription); } unset($subscription); } break; case 'updateCustomer': $cu = Customer::retrieve($data['customer_id']); foreach ($data['fields'] as $field => $value) { $cu->{$field} = $value; } $success = $this->fetch($cu->save()); if (!empty($success['cards']['data'])) { foreach ($success['cards']['data'] as &$card) { $card = $this->fetch($card); } unset($card); } if (!empty($success['subscriptions']['data'])) { foreach ($success['subscriptions']['data'] as &$subscription) { $subscription = $this->fetch($subscription); } unset($subscription); } break; case 'deleteCustomer': $cu = Customer::retrieve($data['customer_id']); $success = $this->fetch($cu->delete()); break; case 'listCustomers': $customers = Customer::all($data['options']); $success = $this->fetch($customers); foreach ($success['data'] as &$customer) { $customer = $this->fetch($customer); if (!empty($customer['cards']['data'])) { foreach ($customer['cards']['data'] as &$card) { $card = $this->fetch($card); } unset($card); } if (!empty($customer['subscriptions']['data'])) { foreach ($customer['subscriptions']['data'] as &$subscription) { $subscription = $this->fetch($subscription); } unset($subscription); } } break; /** * CARDS * */ /** * CARDS * */ case 'createCard': $cu = Customer::retrieve($data['customer_id']); $validCardFields = ['object', 'address_zip', 'address_city', 'address_state', 'address_country', 'address_line1', 'address_line2', 'number', 'exp_month', 'exp_year', 'cvc', 'name', 'metadata']; // unset not valid keys to prevent unknown parameter stripe error unset($data['customer_id']); foreach ($data['source'] as $k => $v) { if (!in_array($k, $validCardFields)) { unset($data['source'][$k]); } } $card = $cu->sources->create($data); $success = $this->fetch($card); break; case 'retrieveCard': $cu = Customer::retrieve($data['customer_id']); $card = $cu->sources->retrieve($data['card_id']); $success = $this->fetch($card); break; case 'updateCard': $cu = Customer::retrieve($data['customer_id']); $cuCard = $cu->sources->retrieve($data['card_id']); foreach ($data['fields'] as $field => $value) { $cuCard->{$field} = $value; } $card = $cuCard->save(); $success = $this->fetch($card); break; case 'deleteCard': $cu = Customer::retrieve($data['customer_id']); $card = $cu->sources->retrieve($data['card_id'])->delete(); $success = $this->fetch($card); break; case 'listCards': $cu = Customer::retrieve($data['customer_id']); $cards = $cu->sources->all($data['options']); $success = $this->fetch($cards); foreach ($success['data'] as &$card) { $card = $this->fetch($card); } break; /** * SUBSCRIPTIONS * */ /** * SUBSCRIPTIONS * */ case 'createSubscription': $cu = Customer::retrieve($data['customer_id']); // unset customer_id to prevent unknown parameter stripe error unset($data['customer_id']); $subscription = $cu->subscriptions->create($data['subscription']); $success = $this->fetch($subscription); break; case 'retrieveSubscription': $cu = Customer::retrieve($data['customer_id']); $subscription = $cu->subscriptions->retrieve($data['subscription_id']); $success = $this->fetch($subscription); break; case 'updateSubscription': $cu = Customer::retrieve($data['customer_id']); $cuSubscription = $cu->subscriptions->retrieve($data['subscription_id']); foreach ($data['fields'] as $field => $value) { $cuSubscription->{$field} = $value; } $subscription = $cuSubscription->save(); $success = $this->fetch($subscription); break; case 'cancelSubscription': $cu = Customer::retrieve($data['customer_id']); $subscription = $cu->subscriptions->retrieve($data['subscription_id'])->cancel($data['at_period_end']); $success = $this->fetch($subscription); break; case 'listSubscriptions': $cu = Customer::retrieve($data['customer_id']); $subscriptions = $cu->subscriptions->all($data['options']); $success = $this->fetch($subscriptions); foreach ($success['data'] as &$subscription) { $subscription = $this->fetch($subscription); } break; /** * PLANS * */ /** * PLANS * */ case 'createPlan': $plan = Plan::create($data); $success = $this->fetch($plan); break; case 'retrievePlan': $plan = Plan::retrieve($data['plan_id']); $success = $this->fetch($plan); break; case 'updatePlan': $p = Plan::retrieve($data['plan_id']); foreach ($data['fields'] as $field => $value) { $p->{$field} = $value; } $plan = $p->save(); $success = $this->fetch($plan); break; case 'deletePlan': $p = Plan::retrieve($data['plan_id']); $plan = $p->delete(); $success = $this->fetch($plan); break; case 'listPlans': $plans = Plan::all($data['options']); $success = $this->fetch($plans); foreach ($success['data'] as &$plan) { $plan = $this->fetch($plan); } break; /** * COUPONS * */ /** * COUPONS * */ case 'createCoupon': $coupon = Coupon::create($data); $success = $this->fetch($coupon); break; case 'retrieveCoupon': $coupon = Coupon::retrieve($data['coupon_id']); $success = $this->fetch($coupon); break; case 'deleteCoupon': $c = Coupon::retrieve($data['coupon_id']); $coupon = $c->delete(); $success = $this->fetch($coupon); break; case 'listCoupons': $coupons = Coupon::all($data['options']); $success = $this->fetch($coupons); foreach ($success['data'] as &$coupon) { $coupon = $this->fetch($coupon); } break; /** * * EVENTS * */ /** * * EVENTS * */ case 'retrieveEvent': $event = Event::retrieve($data['event_id']); $success = $this->fetch($event); // cards if (isset($success['data']['object']['cards']['data']) && !empty($success['data']['object']['cards']['data'])) { foreach ($success['data']['object']['cards']['data'] as &$card) { $card = $this->fetch($card); } unset($refund); } break; case 'listEvents': $events = Event::all($data['options']); $success = $this->fetch($events); foreach ($success['data'] as &$event) { $event = $this->fetch($event); // refunds if (isset($event['data']['object']['refunds']) && !empty($event['data']['object']['refunds'])) { foreach ($event['data']['object']['refunds'] as &$refund) { $refund = $this->fetch($refund); } unset($refund); } // cards if (isset($event['data']['object']['cards']['data']) && !empty($event['data']['object']['cards']['data'])) { foreach ($event['data']['object']['cards']['data'] as &$card) { $card = $this->fetch($card); } unset($refund); } } break; } } catch (Card $e) { $body = $e->getJsonBody(); $error = $body['error']; $error['http_status'] = $e->getHttpStatus(); $message = $error['message']; } catch (InvalidRequest $e) { $body = $e->getJsonBody(); $error = $body['error']; $error['http_status'] = $e->getHttpStatus(); } catch (Authentication $e) { $error = $e->getJsonBody(); $error['http_status'] = $e->getHttpStatus(); } catch (ApiConnection $e) { $body = $e->getJsonBody(); $error['http_status'] = $e->getHttpStatus(); } catch (Base $e) { $body = $e->getJsonBody(); $error['http_status'] = $e->getHttpStatus(); } catch (\Exception $e) { $body = $e->getJsonBody(); $error['http_status'] = $e->getHttpStatus(); } if ($success) { // if ($this->logFile && in_array($this->logType, ['both', 'success'])) { // CakeLog::write('Success', $method, $this->logFile); // } return ['status' => 'success', 'message' => 'Success', 'response' => $success]; } $str = ''; $str .= $method . ", type:" . (!empty($error['type']) ? $error['type'] : ''); $str .= ", type:" . (!empty($error['type']) ? $error['type'] : ''); $str .= ", http_status:" . (!empty($error['http_status']) ? $error['http_status'] : ''); $str .= ", param:" . (!empty($error['param']) ? $error['param'] : ''); $str .= ", message:" . (!empty($error['message']) ? $error['message'] : ''); // if ($this->logFile && in_array($this->logType, array('both', 'error'))) { // CakeLog::write('Error', $str, $this->logFile ); // } return ['status' => 'error', 'message' => $message, 'response' => $error]; }
public function retrieveCharges() { return Charge::all(); }
function getLatestCharges() { $this->setApiKey(); $charge = \Stripe\Charge::all(array("limit" => 3)); return $charge; }