├── Stripe ├── ApiError.php ├── ApiConnectionError.php ├── AuthenticationError.php ├── RateLimitError.php ├── InvalidRequestError.php ├── CardError.php ├── Account.php ├── Balance.php ├── Error.php ├── AttachedObject.php ├── Util │ └── Set.php ├── Event.php ├── Token.php ├── SingletonApiResource.php ├── Refund.php ├── BalanceTransaction.php ├── List.php ├── Coupon.php ├── Plan.php ├── InvoiceItem.php ├── ApplicationFee.php ├── Transfer.php ├── Subscription.php ├── Stripe.php ├── Card.php ├── Recipient.php ├── Invoice.php ├── Util.php ├── Charge.php ├── Customer.php ├── ApiResource.php ├── Object.php └── ApiRequestor.php ├── README.md ├── Stripe.php └── PaymentStripe.module /Stripe/ApiError.php: -------------------------------------------------------------------------------- 1 | param = $param; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Stripe/CardError.php: -------------------------------------------------------------------------------- 1 | param = $param; 11 | $this->code = $code; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Stripe/Account.php: -------------------------------------------------------------------------------- 1 | httpStatus = $httpStatus; 11 | $this->httpBody = $httpBody; 12 | $this->jsonBody = $jsonBody; 13 | } 14 | 15 | public function getHttpStatus() 16 | { 17 | return $this->httpStatus; 18 | } 19 | 20 | public function getHttpBody() 21 | { 22 | return $this->httpBody; 23 | } 24 | 25 | public function getJsonBody() 26 | { 27 | return $this->jsonBody; 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Stripe/AttachedObject.php: -------------------------------------------------------------------------------- 1 | _values), array_keys($properties)); 14 | // Don't unset, but rather set to null so we send up '' for deletion. 15 | foreach ($removed as $k) { 16 | $this->$k = null; 17 | } 18 | 19 | foreach ($properties as $k => $v) { 20 | $this->$k = $v; 21 | } 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /Stripe/Util/Set.php: -------------------------------------------------------------------------------- 1 | _elts = array(); 10 | foreach ($members as $item) 11 | $this->_elts[$item] = true; 12 | } 13 | 14 | public function includes($elt) 15 | { 16 | return isset($this->_elts[$elt]); 17 | } 18 | 19 | public function add($elt) 20 | { 21 | $this->_elts[$elt] = true; 22 | } 23 | 24 | public function discard($elt) 25 | { 26 | unset($this->_elts[$elt]); 27 | } 28 | 29 | // TODO: make Set support foreach 30 | public function toArray() 31 | { 32 | return array_keys($this->_elts); 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /Stripe/Event.php: -------------------------------------------------------------------------------- 1 | refresh(); 9 | return $instance; 10 | } 11 | 12 | /** 13 | * @param Stripe_SingletonApiResource $class 14 | * @return string The endpoint associated with this singleton class. 15 | */ 16 | public static function classUrl($class) 17 | { 18 | $base = self::className($class); 19 | return "/v1/${base}"; 20 | } 21 | 22 | /** 23 | * @return string The endpoint associated with this singleton API resource. 24 | */ 25 | public function instanceUrl() 26 | { 27 | $class = get_class($this); 28 | $base = self::classUrl($class); 29 | return "$base"; 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /Stripe/Refund.php: -------------------------------------------------------------------------------- 1 | _apiKey); 8 | list($response, $apiKey) = $requestor->request( 9 | 'get', 10 | $this['url'], 11 | $params 12 | ); 13 | return Stripe_Util::convertToStripeObject($response, $apiKey); 14 | } 15 | 16 | public function create($params=null) 17 | { 18 | $requestor = new Stripe_ApiRequestor($this->_apiKey); 19 | list($response, $apiKey) = $requestor->request( 20 | 'post', $this['url'], $params 21 | ); 22 | return Stripe_Util::convertToStripeObject($response, $apiKey); 23 | } 24 | 25 | public function retrieve($id, $params=null) 26 | { 27 | $requestor = new Stripe_ApiRequestor($this->_apiKey); 28 | $base = $this['url']; 29 | $id = Stripe_ApiRequestor::utf8($id); 30 | $extn = urlencode($id); 31 | list($response, $apiKey) = $requestor->request( 32 | 'get', "$base/$extn", $params 33 | ); 34 | return Stripe_Util::convertToStripeObject($response, $apiKey); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /Stripe/Coupon.php: -------------------------------------------------------------------------------- 1 | get("PaymentStripe"); 13 | $payment->setCurrency("EUR"); 14 | $payment->setId(123456789); 15 | 16 | $customer = Array(); 17 | $customer['email'] = "antti.peisa@gmail.com"; 18 | $payment->setCustomerData($customer); 19 | 20 | $amount = 1000; // Amount in payment modules always in cents 21 | $payment->addProduct("My product", $amount); 22 | 23 | // In this example we are going to do all in same page 24 | $url = $page->httpUrl; 25 | $payment->setProcessUrl($url . "?step=process"); 26 | $payment->setFailureUrl($url . "?step=fail"); 27 | $payment->setCancelUrl($url . "?step=cancel"); 28 | 29 | switch ($input->get->step) { 30 | case 'process': 31 | if ($payment->processPayment()) { 32 | echo "Thanks, payment successful!"; 33 | } else { 34 | echo "Are you kidding me?"; 35 | } 36 | break; 37 | 38 | case 'fail': 39 | echo "Something went wrong"; 40 | break; 41 | 42 | case 'cancel': 43 | echo "I think you cancelled?"; 44 | break; 45 | 46 | default: 47 | echo $payment->embed(); // Here you could look if instance is PaymentEmbed or PaymentRedirect and choose method based on that 48 | break; 49 | } 50 | ``` 51 | 52 | ## License 53 | GPL 2.0 54 | -------------------------------------------------------------------------------- /Stripe/Plan.php: -------------------------------------------------------------------------------- 1 | _apiKey); 48 | $url = $this->instanceUrl() . '/refund'; 49 | list($response, $apiKey) = $requestor->request('post', $url, $params); 50 | $this->refreshFrom($response, $apiKey); 51 | return $this; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Stripe/Transfer.php: -------------------------------------------------------------------------------- 1 | _apiKey); 47 | $url = $this->instanceUrl() . '/cancel'; 48 | list($response, $apiKey) = $requestor->request('post', $url); 49 | $this->refreshFrom($response, $apiKey); 50 | return $this; 51 | } 52 | 53 | /** 54 | * @return Stripe_Transfer The saved transfer. 55 | */ 56 | public function save() 57 | { 58 | $class = get_class(); 59 | return self::_scopedSave($class); 60 | } 61 | 62 | } 63 | -------------------------------------------------------------------------------- /Stripe/Subscription.php: -------------------------------------------------------------------------------- 1 | _apiKey); 54 | $url = $this->instanceUrl() . '/discount'; 55 | list($response, $apiKey) = $requestor->request('delete', $url); 56 | $this->refreshFrom(array('discount' => null), $apiKey, true); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /Stripe/Stripe.php: -------------------------------------------------------------------------------- 1 | id; 72 | $transfers = Stripe_Transfer::all($params, $this->_apiKey); 73 | return $transfers; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /Stripe/Invoice.php: -------------------------------------------------------------------------------- 1 | request('get', $url, $params); 52 | return Stripe_Util::convertToStripeObject($response, $apiKey); 53 | } 54 | 55 | /** 56 | * @return Stripe_Invoice The saved invoice. 57 | */ 58 | public function save() 59 | { 60 | $class = get_class(); 61 | return self::_scopedSave($class); 62 | } 63 | 64 | /** 65 | * @return Stripe_Invoice The paid invoice. 66 | */ 67 | public function pay() 68 | { 69 | $requestor = new Stripe_ApiRequestor($this->_apiKey); 70 | $url = $this->instanceUrl() . '/pay'; 71 | list($response, $apiKey) = $requestor->request('post', $url); 72 | $this->refreshFrom($response, $apiKey); 73 | return $this; 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /Stripe.php: -------------------------------------------------------------------------------- 1 | $v) { 34 | // FIXME: this is an encapsulation violation 35 | if ($k[0] == '_') { 36 | continue; 37 | } 38 | if ($v instanceof Stripe_Object) { 39 | $results[$k] = $v->__toArray(true); 40 | } else if (is_array($v)) { 41 | $results[$k] = self::convertStripeObjectToArray($v); 42 | } else { 43 | $results[$k] = $v; 44 | } 45 | } 46 | return $results; 47 | } 48 | 49 | /** 50 | * Converts a response from the Stripe API to the corresponding PHP object. 51 | * 52 | * @param array $resp The response from the Stripe API. 53 | * @param string $apiKey 54 | * @return Stripe_Object|array 55 | */ 56 | public static function convertToStripeObject($resp, $apiKey) 57 | { 58 | $types = array( 59 | 'card' => 'Stripe_Card', 60 | 'charge' => 'Stripe_Charge', 61 | 'customer' => 'Stripe_Customer', 62 | 'list' => 'Stripe_List', 63 | 'invoice' => 'Stripe_Invoice', 64 | 'invoiceitem' => 'Stripe_InvoiceItem', 65 | 'event' => 'Stripe_Event', 66 | 'transfer' => 'Stripe_Transfer', 67 | 'plan' => 'Stripe_Plan', 68 | 'recipient' => 'Stripe_Recipient', 69 | 'refund' => 'Stripe_Refund', 70 | 'subscription' => 'Stripe_Subscription' 71 | ); 72 | if (self::isList($resp)) { 73 | $mapped = array(); 74 | foreach ($resp as $i) 75 | array_push($mapped, self::convertToStripeObject($i, $apiKey)); 76 | return $mapped; 77 | } else if (is_array($resp)) { 78 | if (isset($resp['object']) 79 | && is_string($resp['object']) 80 | && isset($types[$resp['object']])) { 81 | $class = $types[$resp['object']]; 82 | } else { 83 | $class = 'Stripe_Object'; 84 | } 85 | return Stripe_Object::scopedConstructFrom($class, $resp, $apiKey); 86 | } else { 87 | return $resp; 88 | } 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /Stripe/Charge.php: -------------------------------------------------------------------------------- 1 | _apiKey); 58 | $url = $this->instanceUrl() . '/refund'; 59 | list($response, $apiKey) = $requestor->request('post', $url, $params); 60 | $this->refreshFrom($response, $apiKey); 61 | return $this; 62 | } 63 | 64 | /** 65 | * @param array|null $params 66 | * 67 | * @return Stripe_Charge The captured charge. 68 | */ 69 | public function capture($params=null) 70 | { 71 | $requestor = new Stripe_ApiRequestor($this->_apiKey); 72 | $url = $this->instanceUrl() . '/capture'; 73 | list($response, $apiKey) = $requestor->request('post', $url, $params); 74 | $this->refreshFrom($response, $apiKey); 75 | return $this; 76 | } 77 | 78 | /** 79 | * @param array|null $params 80 | * 81 | * @return array The updated dispute. 82 | */ 83 | public function updateDispute($params=null) 84 | { 85 | $requestor = new Stripe_ApiRequestor($this->_apiKey); 86 | $url = $this->instanceUrl() . '/dispute'; 87 | list($response, $apiKey) = $requestor->request('post', $url, $params); 88 | $this->refreshFrom(array('dispute' => $response), $apiKey, true); 89 | return $this->dispute; 90 | } 91 | 92 | /** 93 | * @return Stripe_Charge The updated charge. 94 | */ 95 | public function closeDispute() 96 | { 97 | $requestor = new Stripe_ApiRequestor($this->_apiKey); 98 | $url = $this->instanceUrl() . '/dispute/close'; 99 | list($response, $apiKey) = $requestor->request('post', $url); 100 | $this->refreshFrom($response, $apiKey); 101 | return $this; 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /PaymentStripe.module: -------------------------------------------------------------------------------- 1 | 'PaymentStripe', 10 | 'version' => 001, 11 | 'summary' => 'PaymentStripe', 12 | 'singular' => false, 13 | 'requires' => 'PaymentModule', 14 | ); 15 | } 16 | 17 | public function init() { 18 | $this->currenct = $this->defaultCurrency; 19 | } 20 | 21 | public function getTitle() { 22 | return $this->_("Credit Card (Stripe)"); 23 | } 24 | 25 | public function processPayment() { 26 | // Set your secret key: remember to change this to your live secret key in production 27 | // See your keys here https://dashboard.stripe.com/account 28 | Stripe::setApiKey($this->secretKey); 29 | 30 | // Get the credit card details submitted by the form 31 | $token = $this->input->post->stripeToken; 32 | 33 | // Create the charge on Stripe's servers - this will charge the user's card 34 | try { 35 | $charge = Stripe_Charge::create(array( 36 | "amount" => $this->getTotalAmount(), // amount in cents, again 37 | "currency" => $this->currency, 38 | "card" => $token, 39 | "description" => $this->customer->email) 40 | ); 41 | } catch(Stripe_CardError $e) { 42 | $this->setFailureReason($e->getMessage()); 43 | return false; 44 | } 45 | return true; 46 | } 47 | 48 | public function render() { 49 | 50 | if ($this->getTotalAmount() <= 0) throw new WireException("Products are not set"); 51 | if ($this->processUrl == '') throw new WireException("processUrl is not set"); 52 | 53 | 54 | $out = ' 55 |
'; 72 | return $out; 73 | } 74 | 75 | public static function getModuleConfigInputfields(array $data) { 76 | $inputfields = new InputfieldWrapper(); 77 | 78 | $field = wire('modules')->get('InputfieldText'); 79 | $field->name = 'seller'; 80 | $field->label = __("Seller name"); 81 | $field->notes = __("Company or seller of the goods - this is shown on stripe payment form"); 82 | if(isset($data['seller'])) $field->value = $data['seller']; 83 | $inputfields->add($field); 84 | 85 | $field = wire('modules')->get('InputfieldText'); 86 | $field->name = 'defaultCurrency'; 87 | $field->label = __("Default currency"); 88 | $field->notes = __("Use this currency by default (always possible to overwrite when using this module from API)"); 89 | if(isset($data['defaultCurrency'])) $field->value = $data['defaultCurrency']; 90 | $inputfields->add($field); 91 | 92 | $field = wire('modules')->get('InputfieldText'); 93 | $field->name = 'secretKey'; 94 | $field->label = __("Secret key"); 95 | $field->notes = __("Your secret key: remember to change this to your live secret key in production. See your keys [here](https://dashboard.stripe.com/account)"); 96 | if(isset($data['secretKey'])) $field->value = $data['secretKey']; 97 | $inputfields->add($field); 98 | 99 | $field = wire('modules')->get('InputfieldText'); 100 | $field->name = 'publicKey'; 101 | $field->label = __("Public key"); 102 | $field->notes = __("Your public key: remember to change this to your live public key in production. See your keys [here](https://dashboard.stripe.com/account)"); 103 | if(isset($data['publicKey'])) $field->value = $data['publicKey']; 104 | $inputfields->add($field); 105 | 106 | $field = wire('modules')->get('InputfieldText'); 107 | $field->name = 'imageUrl'; 108 | $field->label = __("Url to logo"); 109 | $field->notes = __("Company logo or other image to show in Stripe payment form. Relative url from root, like /site/templates/images/logo.gif"); 110 | if(isset($data['imageUrl'])) $field->value = $data['imageUrl']; 111 | $inputfields->add($field); 112 | 113 | return $inputfields; 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /Stripe/Customer.php: -------------------------------------------------------------------------------- 1 | id; 71 | $ii = Stripe_InvoiceItem::create($params, $this->_apiKey); 72 | return $ii; 73 | } 74 | 75 | /** 76 | * @param array|null $params 77 | * 78 | * @returns array An array of the customer's Stripe_Invoices. 79 | */ 80 | public function invoices($params=null) 81 | { 82 | if (!$params) 83 | $params = array(); 84 | $params['customer'] = $this->id; 85 | $invoices = Stripe_Invoice::all($params, $this->_apiKey); 86 | return $invoices; 87 | } 88 | 89 | /** 90 | * @param array|null $params 91 | * 92 | * @returns array An array of the customer's Stripe_InvoiceItems. 93 | */ 94 | public function invoiceItems($params=null) 95 | { 96 | if (!$params) 97 | $params = array(); 98 | $params['customer'] = $this->id; 99 | $iis = Stripe_InvoiceItem::all($params, $this->_apiKey); 100 | return $iis; 101 | } 102 | 103 | /** 104 | * @param array|null $params 105 | * 106 | * @returns array An array of the customer's Stripe_Charges. 107 | */ 108 | public function charges($params=null) 109 | { 110 | if (!$params) 111 | $params = array(); 112 | $params['customer'] = $this->id; 113 | $charges = Stripe_Charge::all($params, $this->_apiKey); 114 | return $charges; 115 | } 116 | 117 | /** 118 | * @param array|null $params 119 | * 120 | * @returns Stripe_Subscription The updated subscription. 121 | */ 122 | public function updateSubscription($params=null) 123 | { 124 | $requestor = new Stripe_ApiRequestor($this->_apiKey); 125 | $url = $this->instanceUrl() . '/subscription'; 126 | list($response, $apiKey) = $requestor->request('post', $url, $params); 127 | $this->refreshFrom(array('subscription' => $response), $apiKey, true); 128 | return $this->subscription; 129 | } 130 | 131 | /** 132 | * @param array|null $params 133 | * 134 | * @returns Stripe_Subscription The cancelled subscription. 135 | */ 136 | public function cancelSubscription($params=null) 137 | { 138 | $requestor = new Stripe_ApiRequestor($this->_apiKey); 139 | $url = $this->instanceUrl() . '/subscription'; 140 | list($response, $apiKey) = $requestor->request('delete', $url, $params); 141 | $this->refreshFrom(array('subscription' => $response), $apiKey, true); 142 | return $this->subscription; 143 | } 144 | 145 | /** 146 | * @param array|null $params 147 | * 148 | * @returns Stripe_Customer The updated customer. 149 | */ 150 | public function deleteDiscount() 151 | { 152 | $requestor = new Stripe_ApiRequestor($this->_apiKey); 153 | $url = $this->instanceUrl() . '/discount'; 154 | list($response, $apiKey) = $requestor->request('delete', $url); 155 | $this->refreshFrom(array('discount' => null), $apiKey, true); 156 | } 157 | } 158 | -------------------------------------------------------------------------------- /Stripe/ApiResource.php: -------------------------------------------------------------------------------- 1 | refresh(); 9 | return $instance; 10 | } 11 | 12 | /** 13 | * @returns Stripe_ApiResource The refreshed resource. 14 | */ 15 | public function refresh() 16 | { 17 | $requestor = new Stripe_ApiRequestor($this->_apiKey); 18 | $url = $this->instanceUrl(); 19 | 20 | list($response, $apiKey) = $requestor->request( 21 | 'get', 22 | $url, 23 | $this->_retrieveOptions 24 | ); 25 | $this->refreshFrom($response, $apiKey); 26 | return $this; 27 | } 28 | 29 | /** 30 | * @param string $class 31 | * 32 | * @returns string The name of the class, with namespacing and underscores 33 | * stripped. 34 | */ 35 | public static function className($class) 36 | { 37 | // Useful for namespaces: Foo\Stripe_Charge 38 | if ($postfix = strrchr($class, '\\')) { 39 | $class = substr($postfix, 1); 40 | } 41 | if (substr($class, 0, strlen('Stripe')) == 'Stripe') { 42 | $class = substr($class, strlen('Stripe')); 43 | } 44 | $class = str_replace('_', '', $class); 45 | $name = urlencode($class); 46 | $name = strtolower($name); 47 | return $name; 48 | } 49 | 50 | /** 51 | * @param string $class 52 | * 53 | * @returns string The endpoint URL for the given class. 54 | */ 55 | public static function classUrl($class) 56 | { 57 | $base = self::_scopedLsb($class, 'className', $class); 58 | return "/v1/${base}s"; 59 | } 60 | 61 | /** 62 | * @returns string The full API URL for this API resource. 63 | */ 64 | public function instanceUrl() 65 | { 66 | $id = $this['id']; 67 | $class = get_class($this); 68 | if ($id === null) { 69 | $message = "Could not determine which URL to request: " 70 | . "$class instance has invalid ID: $id"; 71 | throw new Stripe_InvalidRequestError($message, null); 72 | } 73 | $id = Stripe_ApiRequestor::utf8($id); 74 | $base = $this->_lsb('classUrl', $class); 75 | $extn = urlencode($id); 76 | return "$base/$extn"; 77 | } 78 | 79 | private static function _validateCall($method, $params=null, $apiKey=null) 80 | { 81 | if ($params && !is_array($params)) { 82 | $message = "You must pass an array as the first argument to Stripe API " 83 | . "method calls. (HINT: an example call to create a charge " 84 | . "would be: \"StripeCharge::create(array('amount' => 100, " 85 | . "'currency' => 'usd', 'card' => array('number' => " 86 | . "4242424242424242, 'exp_month' => 5, 'exp_year' => 2015)))\")"; 87 | throw new Stripe_Error($message); 88 | } 89 | 90 | if ($apiKey && !is_string($apiKey)) { 91 | $message = 'The second argument to Stripe API method calls is an ' 92 | . 'optional per-request apiKey, which must be a string. ' 93 | . '(HINT: you can set a global apiKey by ' 94 | . '"Stripe::setApiKey(