def test_global_api_key(self): # test for error if global key is undefined self.assertRaises(RuntimeError, sift.Client) sift.api_key = "a_test_global_api_key" local_api_key = "a_test_local_api_key" client1 = sift.Client() client2 = sift.Client(local_api_key) # test that global api key is assigned assert (client1.api_key == sift.api_key) # test that local api key is assigned assert (client2.api_key == local_api_key) client2 = sift.Client() # test that client2 is assigned a new object with global api_key assert (client2.api_key == sift.api_key)
def make_order(): # Creates a new Order with items that the user selected. data = json.loads(request.data) try: order = Inventory.create_order(currency=data['currency'], items=data['items'], email=data['email'], shipping=data['shipping'], create_intent=data['createIntent']) return jsonify({'order': order}) except Exception as e: return jsonify(e), 403 client = sift.Client("e81bf5f38f4ce6c0")
def test_provided_session(self): session = mock.Mock() client = sift.Client(api_key=self.test_key, account_id=self.account_id, session=session) mock_response = mock.Mock() mock_response.content = '{"status": 0, "error_message": "OK"}' mock_response.json.return_value = json.loads(mock_response.content) mock_response.status_code = 200 mock_response.headers = response_with_data_header() session.post.return_value = mock_response event = '$transaction' client.track(event, valid_transaction_properties()) session.post.assert_called_once()
def webhook_received(): # You can use webhooks to receive information about asynchronous payment events. # For more about our webhook events check out https://stripe.com/docs/webhooks. webhook_secret = os.getenv('STRIPE_WEBHOOK_SECRET') request_data = json.loads(request.data) if webhook_secret: # Retrieve the event by verifying the signature using the raw body and secret if webhook signing is configured. signature = request.headers.get('stripe-signature') try: event = stripe.Webhook.construct_event(payload=request.data, sig_header=signature, secret=webhook_secret) data = event['data'] except Exception as e: return e # Get the type of webhook event sent - used to check the status of PaymentIntents. event_type = event['type'] else: data = request_data['data'] event_type = request_data['type'] data_object = data['object'] # PaymentIntent Beta, see https://stripe.com/docs/payments/payment-intents # Monitor payment_intent.succeeded & payment_intent.payment_failed events. if data_object['object'] == 'payment_intent': payment_intent = data_object if event_type == 'payment_intent.succeeded': print('🔔 Webhook received! Payment for PaymentIntent ' + payment_intent['id'] + ' succeeded') elif event_type == 'payment_intent.payment_failed': print('🔔 Webhook received! Payment on source ' + payment_intent['last_payment_error']['source']['id'] + ' for PaymentIntent ' + payment_intent['id'] + ' failed.') # Monitor `source.chargeable` events. if data_object['object'] == 'source' \ and data_object['status'] == 'chargeable' \ and 'paymentIntent' in data_object['metadata']: source = data_object print( f'🔔 Webhook received! The source {source["id"]} is chargeable') # Find the corresponding PaymentIntent this Source is for by looking in its metadata. payment_intent = stripe.PaymentIntent.retrieve( source['metadata']['paymentIntent']) # Verify that this PaymentIntent actually needs to be paid. if payment_intent['status'] != 'requires_payment_method': return jsonify({ 'error': f'PaymentIntent already has a status of {payment_intent["status"]}' }), 403 # INTEGRATION POINT to make sift request client = sift.Client(api_key='11105a0ef0ae6b81', account_id='5c8affc84f0c93d33c7c4d6d') # Sample $create_order event properties = { # Required Fields "$user_id": "billy_jones_301", # Supported Fields "$session_id": "gigtleqddo84l8cm15qe4il", "$order_id": "ORDER-28168441", "$user_email": "*****@*****.**", "$amount": 115940000, # $115.94 "$currency_code": "USD", "$billing_address": { "$name": "Bill Jones", "$phone": "1-415-555-6041", "$address_1": "2100 Main Street", "$address_2": "Apt 3B", "$city": "New London", "$region": "New Hampshire", "$country": "US", "$zipcode": "03257" }, "$payment_methods": [{ "$payment_type": "$credit_card", "$payment_gateway": "$braintree", "$card_bin": "542486", "$card_last4": "4444" }], "$shipping_address": { "$name": "Bill Jones", "$phone": "1-415-555-6041", "$address_1": "2100 Main Street", "$address_2": "Apt 3B", "$city": "New London", "$region": "New Hampshire", "$country": "US", "$zipcode": "03257" }, "$expedited_shipping": True, "$shipping_method": "$physical", "$items": [ { "$item_id": "12344321", "$product_title": "Microwavable Kettle Corn: Original Flavor", "$price": 4990000, # $4.99 "$upc": "097564307560", "$sku": "03586005", "$brand": "Peters Kettle Corn", "$manufacturer": "Peters Kettle Corn", "$category": "Food and Grocery", "$tags": ["Popcorn", "Snacks", "On Sale"], "$quantity": 4 }, { "$item_id": "B004834GQO", "$product_title": "The Slanket Blanket-Texas Tea", "$price": 39990000, # $39.99 "$upc": "6786211451001", "$sku": "004834GQ", "$brand": "Slanket", "$manufacturer": "Slanket", "$category": "Blankets & Throws", "$tags": ["Awesome", "Wintertime specials"], "$color": "Texas Tea", "$quantity": 2 } ], # For marketplaces, use $seller_user_id to identify the seller "$seller_user_id": "slinkys_emporium", "$promotions": [{ "$promotion_id": "FirstTimeBuyer", "$status": "$success", "$description": "$5 off", "$discount": { "$amount": 5000000, # $5.00 "$currency_code": "USD", "$minimum_purchase_amount": 25000000 # $25.00 } }], # Sample Custom Fields "digital_wallet": "apple_pay", # "google_wallet", etc. "coupon_code": "dollarMadness", "shipping_choice": "FedEx Ground Courier", "is_first_time_buyer": False, # Send this information from a BROWSER client. "$browser": { "$user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36" }, # ========================================= # Send this information from an APP client. "$app": { # Example for the iOS Calculator app. "$os": "iOS", "$os_version": "10.1.3", "$device_manufacturer": "Apple", "$device_model": "iPhone 4,2", "$device_unique_id": "A3D261E4-DE0A-470B-9E4A-720F3D3D22E6", "$app_name": "Calculator", "$app_version": "3.2.7" } } response = client.track("$create_order", properties) # https://stripe.com/docs/api/payment_intents/confirm?disable_syntax_highlighting=1&expand_all_subspecs=1 payment_intent.confirm(source=source['id']) # payment_request_valid = False # payment_request_valid = response.is_ok() && response.api_status == 0 # # if payment_request_valid: # print('payment request passed fraud check') # # Confirm the PaymentIntent with the chargeable source. # payment_intent.confirm(source=source['id']) # else: # print('fraud detected') # Monitor `source.failed` and `source.canceled` events. if data_object['object'] == 'source' and data_object['status'] in [ 'failed', 'canceled' ]: # Cancel the PaymentIntent. source = data_object intent = stripe.PaymentIntent.retrieve( source['metadata']['paymentIntent']) intent.cancel() return jsonify({'status': 'success'})
def test_constructor_api_key(self): client = sift.Client(self.test_key) self.assertEqual(client.api_key, self.test_key)
def setUpClass(self): self.test_key = 'a_fake_test_api_key' self.sift_client = sift.Client(self.test_key)
def setUp(self): self.test_key = 'a_fake_test_api_key' self.account_id = 'ACCT' self.sift_client = sift.Client(api_key=self.test_key, account_id=self.account_id)
def setUp(self): self.test_key = 'a_fake_test_api_key' self.sift_client = sift.Client(self.test_key, version='203') self.sift_client_v204 = sift.Client(self.test_key)
import sift client = sift.Client(api_key='{apiKey}', account_id='{accountId}') # Sample $create_order event for Take-Home Test properties = { # Required Fields "$user_id": "emilylansky", # Supported Fields "$session_id": "gigtleqddo84l8cm15qe4il", "$order_id": "ORDER-28168441", "$user_email": "*****@*****.**", "$amount": 37990000, # $37.99 "$currency_code": "USD", "$billing_address": { "$name": "Emily Lansky", "$phone": "1-510-345-6789", "$address_1": "525 Market St", "$address_2": "N/A", "$city": "San Francisco", "$region": "California", "$country": "US", "$zipcode": "94105" }, "$payment_methods": [{