From a8753be1405be5f68a1d06c6bb78c3e1b8e2a0ff Mon Sep 17 00:00:00 2001 From: barakamwakisha Date: Thu, 8 Feb 2024 00:06:19 +0300 Subject: [PATCH 1/3] add shop api extensions to check and verify transaction status --- src/api/api-extensions.ts | 7 +++++++ src/api/mpesa-shop.resolver.ts | 21 +++++++++++++++++++++ src/mpesa.plugin.ts | 6 ++++++ src/service/mpesa.service.ts | 10 ++++++++++ 4 files changed, 44 insertions(+) create mode 100644 src/api/api-extensions.ts create mode 100644 src/api/mpesa-shop.resolver.ts diff --git a/src/api/api-extensions.ts b/src/api/api-extensions.ts new file mode 100644 index 0000000..9cc2082 --- /dev/null +++ b/src/api/api-extensions.ts @@ -0,0 +1,7 @@ +import gql from "graphql-tag" + +export const shopApiExtensions = gql` + extend type Mutation { + verifyMpesaTransaction(transactionId: String!): Boolean! + } +` diff --git a/src/api/mpesa-shop.resolver.ts b/src/api/mpesa-shop.resolver.ts new file mode 100644 index 0000000..52a7af4 --- /dev/null +++ b/src/api/mpesa-shop.resolver.ts @@ -0,0 +1,21 @@ +import { Args, Mutation, Resolver } from "@nestjs/graphql" +import { Ctx, RequestContext } from "@vendure/core" + +import { MpesaService } from "../service/mpesa.service" + +@Resolver() +export class MpesaShopResolver { + constructor(private readonly mpesaService: MpesaService) {} + + @Mutation() + async verifyMpesaTransaction( + @Ctx() ctx: RequestContext, + @Args() args: { transactionId: string } + ): Promise { + const isSuccessful = await this.mpesaService.verifyMpesaPayment( + ctx, + args.transactionId + ) + return isSuccessful + } +} diff --git a/src/mpesa.plugin.ts b/src/mpesa.plugin.ts index 3225798..135f1a7 100644 --- a/src/mpesa.plugin.ts +++ b/src/mpesa.plugin.ts @@ -1,6 +1,8 @@ import { PluginCommonModule, VendurePlugin } from "@vendure/core" +import { shopApiExtensions } from "./api/api-extensions" import { CallbackWebhookController } from "./api/callback-webhook.controller" +import { MpesaShopResolver } from "./api/mpesa-shop.resolver" import { mpesaEligibilityChecker } from "./config/mpesa-eligibility-checker" import { mpesaPaymentMethodHandler } from "./config/mpesa.handler" import { MPESA_PLUGIN_INIT_OPTIONS } from "./constants" @@ -68,6 +70,10 @@ export interface MpesaPluginOptions { ) return config }, + shopApiExtensions: { + schema: shopApiExtensions, + resolvers: [MpesaShopResolver] + }, providers: [ { provide: MPESA_PLUGIN_INIT_OPTIONS, diff --git a/src/service/mpesa.service.ts b/src/service/mpesa.service.ts index 5e79b6b..9d952e4 100644 --- a/src/service/mpesa.service.ts +++ b/src/service/mpesa.service.ts @@ -99,6 +99,16 @@ export class MpesaService { } } + async verifyMpesaPayment(ctx: RequestContext, transactionId: string) { + const payment = await this.getPaymentByTransactionId(ctx, transactionId) + + if (!payment) { + return false + } + + return payment.state === "Settled" + } + async settlePayment(ctx: RequestContext, transactionId: string) { const isTransactionSuccessful = await this.checkTransactionStatus(transactionId) From 888b5a2f643b9dd5e6ee5c78b4d792680e78697a Mon Sep 17 00:00:00 2001 From: barakamwakisha Date: Thu, 8 Feb 2024 00:07:43 +0300 Subject: [PATCH 2/3] add changeset --- .changeset/pretty-hairs-pay.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/pretty-hairs-pay.md diff --git a/.changeset/pretty-hairs-pay.md b/.changeset/pretty-hairs-pay.md new file mode 100644 index 0000000..411fd39 --- /dev/null +++ b/.changeset/pretty-hairs-pay.md @@ -0,0 +1,5 @@ +--- +"vendure-mpesa-plugin": patch +--- + +add shop api extensions to check and verify payment status From b92b83f0121ca4bf4d34880d8bd40b934811a3cb Mon Sep 17 00:00:00 2001 From: barakamwakisha Date: Thu, 8 Feb 2024 00:09:24 +0300 Subject: [PATCH 3/3] update readme --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 0319a88..989a40d 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,8 @@ export const config: VendureConfig = { 3. Calling the `addPaymentToOrder` mutation on the storefront with the corresponing payment method code will initiate an STK push to the customer's phone. +4. Call the `verifyMpesaTransaction` mutation periodically on the storefront to verify the transaction status. + ## Reference - [Mpesa Daraja API Docs](https://developer.safaricom.co.ke/Documentation)