# การซื้อจากข้อความในแอป iOS

เพื่อให้ผู้ใช้ของคุณสามารถทำการซื้อได้โดยตรงจากข้อความในแอปที่แสดงในแอป iOS ของคุณ คุณสามารถใช้งานโซลูชันสำเร็จรูปสำหรับ Rich Media ของคุณและเพิ่ม callbacks เพื่อรับข้อมูลเพิ่มเติมเกี่ยวกับการซื้อในแอป

<Aside type="note">
ใช้งานได้สำหรับ **iOS SDK เวอร์ชัน 6.4.5 และใหม่กว่า**
</Aside>

## การใช้งาน JavaScript ของ Rich Media

ในการใช้งานการซื้อในแอปใน Rich Media ของคุณ ให้ใช้ **การเรียกฟังก์ชัน JavaScript** แบบง่าย:

```javascript
pushwooshImpl.makePurchaseWithIdentifier("Premium"); // product identifier specified in your App Store product information
```

## เมธอด AppDelegate สำหรับ callbacks

หากคุณต้องการให้ callbacks ทำงานเมื่อมีการซื้อ ให้ใช้งาน **โปรโตคอล PWPurchaseDelegate** ดังนี้:

1. ทำให้ AppDelegate ของโปรเจกต์ของคุณเป็นไปตามโปรโตคอล PWPurchaseDelegate:

```objective-c
@interface AppDelegate : PWAppDelegate <PushNotificationDelegate, PWPurchaseDelegate>
```

2. ใช้งานเมธอดของ PWPurchaseDelegate ใน AppDelegate ของคุณ:

```objective-c
#pragma mark - Purchase delegate methods
- (void) onPWInAppPurchaseHelperPaymentComplete:(NSString*) identifier {
    NSLog(@“Custom purchase delegate -- payment complete for identifier: %@“, identifier);
}
-(void)onPWInAppPurchaseHelperProducts:(NSArray<SKProduct *>* _Nullable)products {
    NSLog(@“Custom purchase delegate -- retrieved list of products”);
}
-(void)onPWInAppPurchaseHelperPaymentFailedProductIdentifier:(NSString* _Nullable)identifier error:(NSError* _Nullable)error {
    NSLog(@“Custom purchase delegate -- payment failed with identifier %@ and error %@“, identifier, error.description);
}
```

### รายการเมธอด PWPurchaseDelegate ที่มีให้ใช้งาน

#### รายการผลิตภัณฑ์

```objective-c
@protocol PWPurchaseDelegate <NSObject>
// ข้อมูลโดยละเอียดเกี่ยวกับรายการผลิตภัณฑ์ในแอปของคุณ
-(void)onPWInAppPurchaseHelperProducts:(NSArray<SKProduct *>* _Nullable)products;
@end
```

#### การทำธุรกรรมสำเร็จ

```objective-c
@protocol PWPurchaseDelegate &#x3C;NSObject>
// การทำธุรกรรมที่ประมวลผลสำเร็จ
<strong>-(void)onPWInAppPurchaseHelperPaymentComplete:(NSString* _Nullable)identifier;
</strong>
@end
```

#### การทำธุรกรรมล้มเหลว

```objective-c
@protocol PWPurchaseDelegate &#x3C;NSObject>
<strong>// การทำธุรกรรมที่ล้มเหลว
</strong>-(void)onPWInAppPurchaseHelperPaymentFailedProductIdentifier:(NSString* _Nullable)identifier error:(NSError* _Nullable)error;
@end
```

#### โปรโมทจาก App Store

```objective-c
@protocol PWPurchaseDelegate <NSObject>
// การซื้อในแอปเริ่มต้นจาก App Store และการทำธุรกรรมจะดำเนินต่อไปในแอปของคุณ
-(void)onPWInAppPurchaseHelperCallPromotedPurchase:(NSString* _Nullable)identifier;
@end
```

#### การกู้คืนธุรกรรมที่เสร็จสมบูรณ์ล้มเหลว

```objective-c
@protocol PWPurchaseDelegate <NSObject>
// เกิดข้อผิดพลาดขณะกู้คืนธุรกรรม
-(void)onPWInAppPurchaseHelperRestoreCompletedTransactionsFailed:(NSError * _Nullable)error;
@end
```

<Aside type="note">
หากความต้องการของคุณไม่ครอบคลุมด้วยเมธอดที่ระบุในคู่มือนี้ โปรดติดต่อ [ทีมสนับสนุนลูกค้า](https://help.pushwoosh.com/hc/en-us/requests/new?utm\_content=pwwebsite\&utm\_term=footer\&utm\_campaign=support) ของเรา และเรายินดีที่จะให้คำแนะนำเพิ่มเติมแก่คุณ
</Aside>