Stripe - get subscription for specific email - stripe-payments

I have customer email. How to check does customer have valid subscription?
I'm using cUrl.
Thank you.

Stripe allows expand responses. Fortunately subscriptions property of customer object is expandable. Thus, you can reduce the number of API requests and lines of intermediate code.
curl https://api.stripe.com/v1/customers/search \
-u $API_KEY: \
--data-urlencode query="email:'test#example.com'" \
-d "expand[]"="data.subscriptions" \
-G

This would be a two-step process:
First, find the ID of the customer by using their email like this:
curl https://api.stripe.com/v1/customers \
-u sk_xxx: \
-d email=foo#bar.com \
-G
Second, use the customer ID to list all their subscriptions like this:
curl https://api.stripe.com/v1/subscriptions \
-u sk_xxx: \
-d customer=cu_xxx \
-G

Related

How does urllib.request differ from curl or httpx in behaviour? Getting a 401 in a request to the Google Container Registry

I am currently working on some code to interact with images on the Google Container Registry. I have working code both using plain curl and also httpx. I am trying to build a package without 3rd party dependencies. My curiosity is around a particular endpoint from which I get a successful response in curl and httpx but a 401 Unauthorized using urllib.request.
The bash script that demonstrates what I'm trying to achieve is the following. It retrieves an access token from the registry API, then uses that token to verify that the API indeed runs version 2 and tries to access a particular Docker image configuration. I'm afraid that in order to test this, you will need access to a private GCR image and a digest for one of the tags.
#!/usr/bin/env bash
set -eu
token=$(gcloud auth print-access-token)
image=...
digest=sha256:...
get_token() {
curl -sSL \
-G \
--http1.1 \
-H "Authorization: Bearer ${token}" \
-H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
--data-urlencode "scope=repository:$1:pull" \
--data-urlencode "service=gcr.io" \
"https://gcr.io/v2/token" | jq -r '.token'
}
echo "---"
echo "Retrieving access token."
access_token=$(get_token ${image})
echo
echo "---"
echo "Testing version 2 capability with access token."
curl -sSL \
--http1.1 \
-o /dev/null \
-w "%{http_code}" \
-H "Authorization: Bearer ${access_token}" \
-H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
https://gcr.io/v2/
echo
echo "---"
echo "Retrieving image configuration with access token."
curl -vL \
--http1.1 \
-o /dev/null \
-w "%{http_code}" \
-H "Authorization: Bearer ${access_token}" \
-H "Accept: application/vnd.docker.distribution.manifest.v2+json" \
"https://gcr.io/v2/${image}/blobs/${digest}"
I additionally created two Jupyter notebooks demonstrating my solutions in httpx and bare urllib.request. The httpx one works perfectly while somehow urllib fails on the image configuration request. I'm running out of ideas trying to spot the difference. If you run the notebook yourself, you will see that the called URL contains a token as a query parameter (is this a security issue?). When I open that link I can actually successfully download the data myself. Maybe urllib still passes along the Authorization header with the Bearer token making that last call fail with 401 Unauthorized?
Any insights are greatly appreciated.
I did some investigation and I believe the difference is that the last call to "https://gcr.io/v2/${image}/blobs/${digest}" actually contains a redirect. Inspecting the curl and httpx calls showed me that both do not include the Authorization header in the second, redirected request, whereas in the way that I set up the urllib.request in the notebook, this header is always included. It's a bit odd that this leads to a 401 but now I know how to address it.
Edit: I can now confirm that by building a urllib.request.Request instance and unlike in the linked notebook, add the authorization header with the request's add_unredirected_header method, everything works as expected.

Microsoft Graph REST API invalid client secret

I have the following POST call I need to make. However, even if I provided the right client id and secret id, my call is getting rejected.
curl POST https://login.microsoftonline.com/f02....e3/oauth2/token
-H 'Content-Type: application/x-www-form-urlencoded' --data 'grant_type=authorization_code&redirect_uri=https://requestb.in/ac&
source=https://graph.microsoft.com&client_id=1e1....-913d9
&client_secret=YmbSFYz.....4Uk=&scope=mail.read&code=AaAAA........on0a569'
This is the error I receive:
curl: (6) Could not resolve host: POST
{"error":"invalid_client","error_description":"AADSTS70002:
Error validating credentials. AADSTS50012: Invalid client secret is
provided.\r\nTrace ID: 78d...a2b\r\nCorrelation ID:
01....ab2\r\nTimestamp: 2016-12-14 01:46:47Z","error_codes":[70002,50012],"timestamp":"2016-12-14 01:46:47Z","trace_id":"78d....a2b","correlation_id":"018.....ab2"}
How could I resolve this ?
EDIT: I am trying to achieve the second section(i.e getting token) in this documentation
The post you provided is leveraging AAD V2 endpoint. But according your code snippet, you were using V1 endpoint https://login.microsoftonline.com/f02....e3/oauth2/token. For acquire access token via V1 endpoint, you can refer to https://graph.microsoft.io/en-us/docs/authorization/app_authorization for more details.
For the V2 authorization endpoint, you may check out the endpoints you are using:
GET https://login.microsoftonline.com/common/oauth2/v2.0/authorize?...
POST https://login.microsoftonline.com/common/oauth2/v2.0/token
And also it is required a v2.0 ad application:
This article assumes a v2.0 registration, so you'll register your app on the Application Registration Portal.
It was due to client_secret. It may contain special characters.
The encodeURIComponent() function encodes a URI component.
This function encodes special characters. In addition, it encodes the following characters: , / ? : # & = + $ #
Use the below one:
encodeURIComponent(client_secret);
In my case:
in configs You have to use the client secret "Value", not ID.
This Value is visible only when it is generated. So you can copy/paste it, in this moment.
See: https://learn.microsoft.com/en-us/answers/questions/370508/getting-34invalid-client-secret-is-provided34-erro.html
I am using the npm package:
Microsoft Azure Active Directory Passport.js Plug-In
with the v1 tenant-specific endpoint.
I was initially receiving the same error message after successfully logging into Microsoft: "Invalid Client Secret".
I was using the "Application ID" as the Client Secret. Then I discovered that you need to go onto the "Settings" tab in the Microsoft Azure Portal
Microsoft Azure
and create a new "Key". It doesn't matter what you name the key. When you click the "Save" button, the value of the key will be filled in the web form. Copy this right away, since it will not be displayed again if the web page is refreshed.
This is the "Client Secret" that you need to add to your configuration.
I hade the same problem today and with help of #muthu I sorted it out.
When client secret is generated with app registration in azure. The secret is plain. But when to use it in the rest call you must urlencode it.
Use an encoder like visual code or other. But it must be encoded else you ge the error
AADSTS7000215: Invalid client secret is provided.
You need to base 64 encode client_secret in the POST body.
For example (using curl):
# Authn details
LoginURL='https://login.microsoftonline.com'
TenantDomain='********.onmicrosoft.com'
ClientID='********'
ClientSecret='********'
# Endpoint details
Resource='https://graph.microsoft.com'
TenantGUID="********"
# Authenticate with OAuth v1
URL="$LoginURL/$TenantDomain/oauth2/token?api-version=1.0"
json=`
curl \
-s \
-k \
-X POST \
-H "Content-Type: application/x-www-form-urlencoded" \
-u "$ClientID:$ClientSecret" \
-d "grant_type=client_credentials" \
-d "resource=$Resource" \
-d "client_id=$ClientID" \
--data-urlencode "client_secret=$ClientSecret" \
$URL \
| python -m json.tool
`
access_token=`echo $json | python -c 'import sys, json; print json.load(sys.stdin)["access_token"]'`
token_type=`echo $json | python -c 'import sys, json; print json.load(sys.stdin)["token_type"]'`
#echo "access_token:$access_token"
#echo "token_type:$token_type:"
# Access resource
URL="$Resource/v1.0/directoryRoles/$TenantGUID/members"
curl \
-s \
-k \
-X GET \
-H "Content-Type: application/x-www-form-urlencoded" \
-H "Authorization: $token_type $access_token" \
$URL \
| python -m json.tool

Posting a Tweet with Unification Engine

When adding a connection using the Twitter connector offered by the Unification Engine, what are the parameters that need to be used and how are they to be passed in the URI?
To send tweet use
curl -XPOST https://apiv2.unificationengine.com/v2/message/send \
--data "{ \"message\": { \"receivers\": [{\"name\": \"name\", \"address\": \"TWITTER_HANDLE\" , \"Connector\": \"UNIQUE_CONNECTION_IDENTIFIER\"}],\"parts\": [{\"id\": \"1\",\"contentType\": \"text/plain\", \"data\":\"MESSAGE_CONTENT\" ,\"size\": MESSAGE_CONTENT_SIZE,\"type\": \"body\",\"sort\":0}]}}" \
-u USER_ACCESSKEY:USER_ACCESSSECRET -k
Where USER_ACCESSKEY:USER_ACCESSSECRET is got when you add the user using UE_APPKEY:UE_APPSECRET
curl -XPOST https://apiv2.unificationengine.com/v2/user/create -u UE_APPKEY:UE_APPSECRET \
--data '{}' -k
Response data:
{"status":200,"info":"200 OK","uri":"user://USER_ACCESSKEY:USER_ACCESSSECRET"}
Let me explain the commands used to add a twitter connection in #UnificationEngine
To add twitter connection in #UnificationEngine use
curl -XPOST https://apiv2.unificationengine.com/v2/connection/add \
-u USER_ACCESSKEY:USER_ACCESSSECRET \
--data '{"uri":"twitter://ACCESS_TOKEN:SECRET#twitter.com","name":"UNIQUE_CONNECTION_IDENTIFIER"}' \
-k
ACCESS_TOKEN:SECRET - is the one got by authentication the twitter connection in the user application.
UNIQUE_CONNECTION_IDENTIFIER - specified here will be further used to address this connection in UE.
f.e to send a tweet the user will have to use the variable specified under UNIQUE_CONNECTION_IDENTIFIER

Pausing a Kill Bill subscription

We've been using Kill Bill to manage a delivery subscription service. You can sign up to a subscription to receive the items you choose, and be billed monthly.
We want to implement a pause feature so that customers could delay their subscriptions if they went on holiday, for example.
The problem is that we can't figure out how to do that with the API. There's a method to cancel the entitlement (another word for subscription, I think) and one to update it, but nothing obviously related to pausing.
Maybe there's a parameter we need to pass in the update method that we haven't found yet?
You can find the API here, and the majority of the mentions of pausing we could locate in the documentation are on this page.
Any help would be greatly appreciated!
There is indeed a pause/resume capability in Kill Bill. This is specified at the bundle level (meaning if you have a bundle with multiple subscriptions they would all be paused/resumed). There is also another mechanism with a lower granularity, but let's start with the basics:
Assuming the following:
a tenant 'bob'/'lazar'
a bundle with subscriptions whose bundle_id = '627a0b2a-82ef-4d7f-b1c7-a5a94be705bf'
Pause on 2016-05-14 (interpreted in account timezone):
curl -v \
-X PUT \
-u admin:password \
-H "X-Killbill-ApiKey: bob" \
-H "X-Killbill-ApiSecret: lazar" \
-H "Content-Type: application/json" \
-H "X-Killbill-CreatedBy: stephane" \
'http://127.0.0.1:8080/1.0/kb/bundles/627a0b2a-82ef-4d7f-b1c7-a5a94be705bf/pause?requestedDate=2016-05-14'
Resume on 2016-05-18 (interpreted in account timezone):
curl -v \
-X PUT \
-u admin:password \
-H "X-Killbill-ApiKey: bob" \
-H "X-Killbill-ApiSecret: lazar" \
-H "Content-Type: application/json" \
-H "X-Killbill-CreatedBy: stephane" \
'http://127.0.0.1:8080/1.0/kb/bundles/627a0b2a-82ef-4d7f-b1c7-a5a94be705bf/resume?requestedDate=2016-05-18'

Fiware: How to create lazy attributes through IDAS UltraLight

I'm using the IoT Agent Ultra-Light module to communicate with the Orion context broker. I can create services and devices and I have checked that the observations reach the context broker too.
curl -X POST XXX.XXX.XXX.XXX:8090/iot/services \
-i \
-H "Content-Type: application/json" \
-H "Fiware-Service: sanitysrv " \
-H "Fiware-ServicePath: / sanitysspath " \
-d '{"services": [{"apikey": "", "cbroker": "http://127.0.0.1:1026", "entity_type": "Dispositivo_tmp", "resource": "/iot/d"}]}'
curl -X POST XXX.XXX.XXX.XXX:8090/iot/devices \
-i \
-H "Content-Type: application/json" \
-H "Fiware-Service: sanitysrv" \
-H "Fiware-ServicePath: /sanitysspath" \
-d '{"devices":[{"device_id":"CE_BDM_3","protocol":"PDI-IoTA-UltraLight", "commands": [], "attributes": [{"type":"int","name":"temperature","object_id":"t"}]}]}'
My problem is that I don´t know how to register a device that contains lazy attributes, and I haven´t found any documentation with related examples. The examples from other IoT Agents that I have tried are not working here.
¿How can it be done?
Lazy attributes are not supported in the UL2.0/MQTT Agent so far but in IoT Agents developed with node.js.
We'll let you know as soon as this feature is available.
Cheers,

Resources