Skip to content

Commit

Permalink
Merge pull request #5 from barakamwakisha/api-ext
Browse files Browse the repository at this point in the history
Add Shop API extensions
  • Loading branch information
barakamwakisha authored Feb 7, 2024
2 parents 55d6829 + b92b83f commit d4ca0e4
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/pretty-hairs-pay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vendure-mpesa-plugin": patch
---

add shop api extensions to check and verify payment status
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
7 changes: 7 additions & 0 deletions src/api/api-extensions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import gql from "graphql-tag"

export const shopApiExtensions = gql`
extend type Mutation {
verifyMpesaTransaction(transactionId: String!): Boolean!
}
`
21 changes: 21 additions & 0 deletions src/api/mpesa-shop.resolver.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
const isSuccessful = await this.mpesaService.verifyMpesaPayment(
ctx,
args.transactionId
)
return isSuccessful
}
}
6 changes: 6 additions & 0 deletions src/mpesa.plugin.ts
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -68,6 +70,10 @@ export interface MpesaPluginOptions {
)
return config
},
shopApiExtensions: {
schema: shopApiExtensions,
resolvers: [MpesaShopResolver]
},
providers: [
{
provide: MPESA_PLUGIN_INIT_OPTIONS,
Expand Down
10 changes: 10 additions & 0 deletions src/service/mpesa.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit d4ca0e4

Please sign in to comment.