Package: bSecure
bSecure CheckoutPakistan's first universal checkout solution that is easy and simple to integrate on your e-commerce store.About bSecure CheckoutIt gives you an option to enableuniversal-login,two-click checkoutand accept multiple payment method for your customers, as well as run your e-commerce store hassle free.It is built fordesktop,tablet, andmobile devicesand is continuously tested and updated to offer a frictionless payment experience for your e-commerce store.bSecurebSecure is a utility library for two-click checkout custom integration. bSecure library simplifies communication between builder and bSecure server and processing tasks for builder's ease.InstallationUse the package managerpipto installbsecure.pipinstallbsecurePrerequisitesbSecure requires Python 3.3.0 or newerConfiguration SetupBy following a few simple steps, you can set up yourbSecure CheckoutandSingle-Sign-On.Getting Your CredentialsGo toBuilder PortalApp Integration>> Sandbox / LiveSelect Environment Type (Custom Integration)Fill following fields:a.Store URLits required in any caseb.Login Redirect URLRequired for featureLogin with bSecurec.Checkout Redirect URLRequired for featurePay with bSecured.Checkout Order Status webhookRequired for featurePay with bSecureSave your client credentials (Client ID and Client Secret)Please make sure to keep credentials at safe place in your codeUsageIntegration Steps:Import the packageimportbSecureasbsecureCreate OrderIn order to create an order you should follow below mentioned steps:Authenticate ClientTo call create order function of bSecure, you first have to authenticate your client.bsecure.authenticatewill be used to authenticate your client as shown below:bsecure.authenticate(client_id,client_secret)client_idandclient_secretcan be obtained fromBuilder Portalfor your application.This is time limited activation, and must be considered to implement in a way that traffic may not disrupt your
application.
We suggest to call this function before each of the following to keep authentication updated:create_orderorder_statusget_customer_profile2. Set OrderTo create an order you should have anorder_detailsobject parameters that are to be set before creating an order.Before proceeding furtherorder_detailsobject has some pre-condition rules that are mentioned below:Shipment Object RuleShipment object should be in below mentioned format:1- If the merchant want his pre-specified shipment method then he should pass shipment method detail in order detail object.Customer Object RuleCustomer object mentioned in order detail object should be in below mentioned format:1- If the customer has already signed-in via bSecure into your system and you have auth_code for the customer you can
just pass that code in the customer object no need for the rest of the fields.2- Since all the fields in Customer object are optional, if you don’t have any information about customer just pass the
empty object, or if you have customer details then your customer object should be in below mentioned format:'customer': { 
    'name': 'string',
    'email': 'string',
    'country_code': 'string',
    'phone_number': 'string',
}Order Detail Request Params:Here you can see sample order formatDictionary format shared below is necessary to activate your bSecure Universal Checkout create order function.order_details = {
    "discount_amount": 0,
    "sub_total_amount": 300.78,
    "total_amount": 300.78,
    "currency_code": "PKR",
    "order_id": "order-id-in-your-system-0001",
    "customer": {
        "name": "your customer name",
        "email": "your.customer.email@yourapplication.com",
        "country_code": "92",
        "phone_number": "345-6789012"
    },
    "products": {
        "0": {
            "id": "product-id-in-your-system-0001",,
            "name": "BAZIC Asst. Size Paint Brush Set (15/Pack)",
            "sku": "product-sku-in-your-system-123",
            "quantity": 1,
            "price": 25.5,
            "sale_price": 35.5,
            "image": "https://sc01.alicdn.com/kf/UT8ChMOXjBbXXagOFbXU.jpg",
            "description": "product headline/short description",
            "short_description": "Detailed description for product. Here you will share all that you know about the product."
        }
    }
}After you have created the dictionary in above mentioned format, call:bsecure.set_order(order_details)should always be called whenever want to create an order with bSecure.bsecure.set_order(order_details=order_details)3. Create OrderNow you are good to go to proceed with create order request. Once order_details are set in your order then, callorder_created = bsecure.create_order()In responsebsecure.created_order(), will returnexpiry,checkout_url,order_referenceandmerchant_order_idabout current order.>>> print(order_created)
>>> {
>>>   'expiry': '2020-11-27 10:55:14',
>>>   'checkout_url': '<bSecure-checkout-url>', # Redirect URL
>>>   'order_reference': 'bsecure-reference',
>>>   'merchant_order_id': 'your-order-id',
>>> }In webview, redirect the tocheckout_urlfromorder_createdvariable as shown above.if order_created.__contains__('checkout_url') and order_created.get('checkout_url', None):
    return redirect(order_created.get('checkout_url')Callback on Order PlacementOnce the order is successfully placed, bSecure will redirect the customer to the url you mentioned in “Checkout
redirect url” in yourenvironment settingsin Builder Portal, with one additional paramorder_refin the query string.Order UpdatesBy usingorder_refyou received in the "Callback on Order Placement" you can call below method to get order details.order_status = bsecure.order_status(order_reference=order_ref)This will give you detailed status of your orderNote: We suggest to storeorder_refwith your order to use it in future for order statusbSecure Single Sign On (SSO)Once your account is set up you are ready to authenticate users! You will need two functions: one for redirecting the user to the OAuth provider, and another for receiving the customer profile from the provider after authentication.Authenticate ClientHere is what you have to do:1: setup data as below:sso_data = {
  "client_id": "<your client_id for bsecure>",
  "state": "string"
}stateshould be unique to each user and stored in your system against each user.We also support Single Sign On Feature for your applicationHere is what you have to do:Step 1: Redirecting Customer to bSecuresso_data = {
 "client_id": "<your client_id for bsecure>",
 "state": "string"
}In above data,stateshould be uniquestrand be stored for later usage.write the code as shown belowbsecure.single_sign_on_set_values(state=sso_data.get('state'), client_id=sso_data.get('client_id'))
redirect_url = bsecure.customer_authenticator()Above code with create and return redirect url to variableredirect_url.You should redirect your application toredirect_urlStep 2: Redirected back to your web application:When User/customer has completed their registration/login flow with us, we will redirect to your application.You will receive acodeandstateas query-params to your application as a get request.You should now:Verify thestateyou received matches with the one you created duringStep 1Ifstatematches:
call functionbsecure.get_customer_profileas with the value ofcodethat you received.:customer_profile = bsecure.get_customer_profile(code=code)You will get customer profile from bSecure in variablecustomer_profileThen based on the data in customer_data, you will register/login customer in your system.