|
| 1 | +[](https://github.com/checkout/sdk-samples/actions/workflows/create_python_package.yml) |
| 2 | +[](https://pypi.org/project/checkout-sdk) |
| 3 | + |
| 4 | +This project is an example of how to start an integration with Python and [Checkout SDK](https://github.com/checkout/checkout-sdk-python) |
| 5 | + |
| 6 | +This project uses `pip` as the package manager for all the requirements that a project needs, ensure that you have pip installed before running the project. |
| 7 | + |
| 8 | +# :sparkles: Before Start |
| 9 | + |
| 10 | +To remain comply with PCI regulations, you need to protect your card numbers, fortunately Checkout have [Frames Framework](https://www.checkout.com/docs/integrate/frames#Who_is_Frames_for?) that helps you to tokenize the payment card, this wrapper includes method `submitCard()`. In the below example, we call this when the "Pay Now" button is clicked. |
| 11 | + |
| 12 | +````html |
| 13 | +<form id="payment-form" method="POST"> |
| 14 | + <div class="one-liner"> |
| 15 | + <div class="card-frame"></div> |
| 16 | + <button id="pay-button" disabled> |
| 17 | + PAY GBP 24.99 |
| 18 | + </button> |
| 19 | + </div> |
| 20 | + <p class="error-message"></p> |
| 21 | + <p class="payment-message"></p> |
| 22 | +</form> |
| 23 | + |
| 24 | +<script src="https://cdn.checkout.com/js/framesv2.min.js"></script> |
| 25 | +```` |
| 26 | + |
| 27 | +Then we intercept the `event` and let Frames to tokenize the card |
| 28 | + |
| 29 | +````javascript |
| 30 | +form.addEventListener('submit', function (event) { |
| 31 | + event.preventDefault(); |
| 32 | + Frames.submitCard(); |
| 33 | +}); |
| 34 | +```` |
| 35 | + |
| 36 | +Make sure to provide your correct `Public Key` in `script.js`, which is the key that authorizes to you the access to Checkout API's |
| 37 | + |
| 38 | +````javascript |
| 39 | +Frames.init('pk_sbox_XXX'); |
| 40 | +```` |
| 41 | + |
| 42 | +Once that Frames validates and returns the tokenized token, then you can add another event to handle the request |
| 43 | +to the backed. If you have any questions regarding the Frames events you can visit the [documentation page](https://www.checkout.com/docs/integrate/frames/frames-reference) |
| 44 | + |
| 45 | +On the backed you need to initiate the SDK with the proper `Secret Key` as follows: |
| 46 | + |
| 47 | +```python |
| 48 | +try: |
| 49 | + sdk = CheckoutSdk.builder() \ |
| 50 | + .secret_key('sk_sbox_XXX') \ |
| 51 | + .environment(environment=Environment.sandbox()) \ |
| 52 | + .build() |
| 53 | +except Exception as e: |
| 54 | + return json.dumps({'status': e.http_metadata.status_code, 'error-message': e.http_metadata.reason}) |
| 55 | +``` |
| 56 | + |
| 57 | +If you have any questions regarding SDK usage, please refer to SDK landing [page](https://github.com/checkout/checkout-sdk-python) |
| 58 | + |
| 59 | +Then you need to build your request, in this case is a `payment request` with `token source` and then |
| 60 | +just call the the SDK function to request a payment |
| 61 | + |
| 62 | +```python |
| 63 | +request_token = request.json |
| 64 | + |
| 65 | +token_source = RequestTokenSource() |
| 66 | +token_source.token = request_token['token'] |
| 67 | + |
| 68 | +payment_request = PaymentRequest() |
| 69 | +payment_request.source = token_source |
| 70 | +payment_request.amount = 2499 |
| 71 | +payment_request.currency = Currency.GBP |
| 72 | +payment_request.processing_channel_id = 'pc_XXX' |
| 73 | + |
| 74 | +response = sdk.payments.request_payment(payment_request) |
| 75 | +return json.dumps(response.__dict__, default=lambda o: o.__dict__, indent=4) |
| 76 | +``` |
| 77 | + |
| 78 | +And that's it! Your payment has been processed. |
| 79 | + |
| 80 | +### :book: Checkout our official documentation. |
| 81 | + |
| 82 | +* [Official Docs (Default)](https://docs.checkout.com/) |
| 83 | +* [Official Docs (Previous)](https://docs.checkout.com/previous) |
| 84 | + |
| 85 | +### :books: Check out our official API documentation guide, where you can also find more usage examples. |
| 86 | + |
| 87 | +* [API Reference (Default)](https://api-reference.checkout.com/) |
| 88 | +* [API Reference (Previous)](https://api-reference.checkout.com/previous) |
| 89 | + |
| 90 | + |
| 91 | +# :rocket: Run the project and test it out |
| 92 | + |
| 93 | +```shell |
| 94 | +python -m pip install --upgrade pip |
| 95 | +pip install -r requirements-dev.txt |
| 96 | +flask --app app run |
| 97 | +``` |
0 commit comments